Element.extend({
	getProperty: function(property){
    	var index = Element.Properties[property];
        if (index) return this[index];
        var flag = Element.PropertiesIFlag[property] || 0;
  
        // Commented old line
    	//if (!window.ie || flag) return this.getAttribute(property, flag);
  
        	// Two new lines: put MSIE version number in var msie and check if this is 8 or higher
    	var msie = navigator.userAgent.toLowerCase().match(/msie\s+(\d)/);
    	if (!window.ie || flag || msie && msie[1]>=8) return this.getAttribute(property, flag); 
        var node = this.attributes[property];
        return (node) ? node.nodeValue : null;
	}
});

var dmsGallery = new Class({

	init:function(gallery){

			// add events 
		this.current_number = 0;
		this.gallery = gallery;
		this.display = $E( '.dms_gallery_display .dms_gallery_screen', gallery); 		
		this.controller = $E( '.dms_gallery_control .dms_gallery_control_text', gallery);
		
			// wait animation
		if (this.display){	
			this.waitsrc =  this.display.getProperty('src');
		}	
		
			// next		 		
		var next = $E( '.dms_gallery_control_next', gallery); 
		if (next){
			next.addEvent('click',this.next.bindWithEvent(this) );
			next.setStyle('cursor','pointer');
		}
		
			// prev				
		var prev = $E( '.dms_gallery_control_prev', gallery);
		if (prev){
			prev.addEvent('click',this.prev.bindWithEvent(this) );
			prev.setStyle('cursor','pointer');
		}
		
			// thumbs		
		this.thumbs = $ES( '.dms_gallery_thumbnails img', gallery);
		for (var k=0 ; k< this.thumbs.length; k++){
			var thumb = new Element(this.thumbs[k]);
			thumb.addEvent('click',this.thumbnail_click.bindWithEvent(this, k) );
			thumb.setStyle('cursor','pointer');
		}
		
			// start gallery with first image
		if (this.thumbs.length > 0){
			this.show(0);
		}
		
			// hide thumbmails amd control if only one image is found
		if (this.thumbs.length == 1){
			var control_area = $E( '.dms_gallery_control', gallery);
			if (control_area) control_area.setStyle('display','none');
			var thumb_area = $ES( '.dms_gallery_thumbnails',gallery);
			if (thumb_area)  thumb_area.setStyle('display','none');
		} 	

	},
	
	next:function (e){
		if (this.thumbs.length >1){
			var num = (this.thumbs.length + this.current_number + 1) % this.thumbs.length
			this.show(num);
		}
	},
	
	prev:function (e){
		if (this.thumbs.length >1){
			var num = (this.thumbs.length + this.current_number - 1) % this.thumbs.length
			this.show(num);
		}
	},
	
	thumbnail_click:function (event, number ) {
		this.show(number);
	},
	
	show:function ( number){

		this.current_number = number;
		var curr = new Element( this.thumbs[number] ); 
		if (this.display && curr){
				// wait 		
			this.display.setProperty('src',  this.waitsrc  );
				// show new image
			this.display.setProperty('src',   curr.getProperty('dms:gallery_display')  );
			
			var alt = curr.getProperty('alt')
			if(alt == null){
				this.display.setProperty('alt',   "");
			}else {
				this.display.setProperty('alt',  alt);
			}
			
			var title = curr.getProperty('title')
			if(title == null){
				this.display.setProperty('title',   "");
			}else {
				this.display.setProperty('title',   title);
			}
		}
		if (this.controller && curr){
			
			var title = curr.getProperty('title')
			if(title == null){
				title = "";
			}
			
			this.controller.setText( title );
		}
	}
});

window.addEvent('load', function()	{	
	$$('#dms_content div.dms_gallery').each(function(gallery){
		gallery.dms_gallery = new dmsGallery();
		gallery.dms_gallery.init(gallery);
	});	
});