/*
 * Gallery Class
 * @author: Daniel Auener <daniel@internetavdelningen.se>
 */
var Gallery = Class.create({ 

	initialize: function(galleryElement,galleryConfig) { 	
		this.galleryElement = galleryElement;	
		this.galleryConfig = galleryConfig;
		this.list = this.galleryElement.select("li");
		this.iterator = 0;
		this.effect = galleryConfig.down("input#idGalleryEffect") ? galleryConfig.down("input#idGalleryEffect").value : "fade";
		this.effects = new Array("fade","blind","slide","fold","drop","puff","shrink","squish");
	},
	
	change: function() {				

		// get direction		
		var direction = ($A(arguments).size() > 1) ? $A(arguments)[1] : null;

		// save current index
		var current = this.iterator;

		// adjust layer level
		this.list[current].setStyle({zIndex:"2"});

		// increase iterator
		if (direction == "prev") this.iterator = (this.iterator-1 < 0) ? this.list.size()-1 : this.iterator-1;
		else this.iterator = (this.iterator+1 >= this.list.size()) ? 0 : this.iterator+1;

		this.list[this.iterator].setStyle({display:"block",zIndex:"1"});

		var effect = (this.effect != "random") ? this.effect : this.effects[Math.ceil(Math.random()*this.effects.size())-1];

		// use effect to change the image
		switch (effect) {
			case "fade": new Effect.Fade(this.list[current]); break;
			case "blind": new Effect.BlindUp(this.list[current]); break;
			case "slide": new Effect.SlideUp(this.list[current]); break;
			case "fold": new Effect.Fold(this.list[current]); break;			
			case "drop": new Effect.DropOut(this.list[current]); break;
			case "puff": new Effect.Puff(this.list[current]); break;						
			case "shrink": new Effect.Shrink(this.list[current]); break;						
			case "squish": new Effect.Squish(this.list[current]); break;						
			default: new Effect.Fade(this.list[current]); break;
		}		
	}
	
});



/*
 * GalleryAutoChange Class
 * @author: Daniel Auener <daniel@internetavdelningen.se>
 */
var GalleryAutoChange = Class.create(Gallery, { 

	initialize: function($super,galleryElement,galleryConfig) { 			
		$super(galleryElement,galleryConfig);
		
		this.startIntervall = this.galleryConfig.down("input#idGalleryStartIntervall") ? this.galleryConfig.down("input#idGalleryStartIntervall").value : 2;
		this.changeIntervall = this.galleryConfig.down("input#idGalleryChangeIntervall") ? this.galleryConfig.down("input#idGalleryChangeIntervall").value : 2;
			
		// position settings for picturefader
		if (this.list.size() > 1) {
			new PeriodicalExecuter(function(pe) {
				new PeriodicalExecuter(this.change.bind(this), this.changeIntervall);
				this.change();
				pe.stop();
			}.bind(this), this.startIntervall);
		}
	}
	
});


/*
 * GalleryClickChange Class
 * @author: Daniel Auener <daniel@internetavdelningen.se>
 */
var GalleryClickChange = Class.create(Gallery, { 

	initialize: function($super,galleryElement,galleryConfig) { 			
		
		$super(galleryElement,galleryConfig);
		this.nextElement = this.galleryElement.up(".clsGalleryWrapper").down(".clsNext");
		this.prevElement = this.galleryElement.up(".clsGalleryWrapper").down(".clsPrev");
		
		// position settings for picturefader
		if (this.list.size() > 1 && this.nextElement && this.prevElement) {
			this.nextElement.observe("click",this.change.bindAsEventListener(this,"next"));		
			this.prevElement.observe("click",this.change.bindAsEventListener(this,"prev"));		
		}
	}
	
});


/*
 * Inits and controlls all galleries on the website.
 * @author: Daniel Auener <daniel@internetavdelningen.se>
 */
var GalleryController = Class.create({ 

	initialize: function(selectorClass) { 		
		// get all galleries with selectorClass
		this.galleryElements = $$("ul."+selectorClass);
		this.galleries = new Array();
			
		// initialize all galleries
		this.galleryElements.each(function(gallery) {
			var galleryConfig = gallery.previous(".clsGalleryConfig");
			var galleryInstance = null;
			var galleryType = galleryConfig.down("input#idGalleryType") ? galleryConfig.down("input#idGalleryType").value : "";
			switch (galleryType) {
				case "autoChange" : galleryInstance = new GalleryAutoChange(gallery,galleryConfig); break;
				case "clickChange" : galleryInstance = new GalleryClickChange(gallery,galleryConfig); break;
				default: galleryInstance = new GalleryAutoChange(gallery,galleryConfig); break;
			}
			this.galleries.push(galleryInstance);
		}.bind(this));
		
	}

});

