var FadeGallery = Class.create({

	nextSpeed : 7000, // time in ms after which the next fading starts
	fadeSpeed : 25, // time in ms for 1% fading step (lower value means faster fading)
	// be careful to always keep this formular: fadeSpeed * 100 < nextSpeed
	fadeContainer : null,
	fadeElements : null,
	fadeCount : null,
	fadeIndex : null,
	nextTimer : null,
	fadeTimer : null,
	fadeOpacity : null,
	fadeNavigation : null,
	fadeControls : null,
	foundPlayStop : false,
	fadeActive : false,
	fading : false,
	
	initialize : function(fadeContainer) {
		this.fadeContainer = fadeContainer;
		if (this.fadeContainer) {
			var fCcnArray = this.fadeContainer.className.split(" ");
			if (fCcnArray.length > 1) {
				for (var i = 0; i < fCcnArray.length; i++) {
					if (fCcnArray[i].search(/nextSpeed/) != -1) {
						var nS = parseInt(fCcnArray[i].substr(10));
						if (!isNaN(nS))
							this.nextSpeed = nS;
					} else if (fCcnArray[i].search(/fadeSpeed/) != -1) {
						var fS = parseInt(fCcnArray[i].substr(10));
						if (!isNaN(fS))
							this.fadeSpeed = fS;
					}
				}
				if (this.nextSpeed < ((this.fadeSpeed*100)+500))
					this.nextSpeed = ((this.fadeSpeed*100)+500);
			}
//			PeriodicalExecuter uses seconds;
			this.nextSpeed = this.nextSpeed / 1000;
			this.fadeSpeed = this.fadeSpeed / 1000;
			this.fadeElements = new Array();
			this.fadeElements = this.fadeContainer.select('.fadeEl');
			this.fadeCount = this.fadeElements.length;
			this.fadeIndex = 0;
			this.fadeOpacity = 100;
			if (this.fadeCount > 1) {
				if (!this.fadeContainer.style.position || this.fadeContainer.style.position == "")
					this.fadeContainer.style.position = "relative";
				for (var i = 0; i < this.fadeCount; i++) {
					this.fadeElements[i].style.position = "absolute";
					this.fadeElements[i].style.top = "0";
					this.fadeElements[i].style.left = "0";
					this.fadeElements[i].style.display = "";
					this.fadeElements[i].style.zIndex = this.fadeCount-i-1;
				}
//				this.nextTimer = window.setInterval(this.nextImage.bind(this), this.nextSpeed);
				this.nextTimer = new PeriodicalExecuter(this.nextImage.bind(this), this.nextSpeed);

				this.fadeActive = true;
				this.fadeNavigation = this.fadeContainer.select('.fadeNavi')[0];
				if (this.fadeNavigation) {
					this.fadeNavigation.style.position = "absolute";
					this.fadeNavigation.style.zIndex = "99";
					this.fadeControls =  new Array();
					this.fadeControls = this.fadeNavigation.select('.fadeCtrl');
					var foundPlay = false;
					var foundStop = false;
					this.fadeControls.each(function(elem, i) {
						elem.ctrl = elem.className.replace("fadeCtrl","").replace(/^\s+|\s+$/,"");
						if (elem.ctrl == "fadePlay")
							foundPlay = true;
						else if (elem.ctrl == "fadeStop")
							foundStop = true;
						elem.onclick = function() {
							this.control(this.fadeControls[i]);
						}.bind(this);
					}.bind(this));
					this.foundPlayStop = (foundPlay && foundStop);
					if (this.foundPlayStop == false)
						this.fadeNavigation.style.display = "none";
				}
			}
		}
	},
	
	nextImage : function() {
//		this.fadeTimer = window.setInterval(this.fadeImage.bind(this), this.fadeSpeed);
		this.fadeTimer = new PeriodicalExecuter(this.fadeImage.bind(this), this.fadeSpeed);
	},
	
	fadeImage : function() {
		if (this.fadeOpacity > 0) {
			this.fadeElements[this.fadeIndex].style.opacity = this.fadeOpacity / 100;
			this.fadeElements[this.fadeIndex].style.filter = 'alpha(opacity='+this.fadeOpacity+')';
			this.fadeOpacity--;
			this.fading = true;
		} else {
//			window.clearInterval(this.fadeTimer);
			this.fadeTimer.stop();
			for (var i = 0; i < this.fadeCount; i++) {
				this.fadeElements[i].style.zIndex = (2*this.fadeCount+this.fadeIndex-i)%this.fadeCount;
			}
			this.fadeOpacity = 100;
			this.fadeElements[this.fadeIndex].style.opacity = this.fadeOpacity / 100;
			this.fadeElements[this.fadeIndex].style.filter = 'alpha(opacity='+this.fadeOpacity+')';
			this.fadeIndex = (this.fadeIndex+1+this.fadeCount)%this.fadeCount;
			this.fading = false;
		}
	},
	
	control : function(elem) {
		switch (elem.ctrl) {
			case "fadePrev":
//				window.clearInterval(this.fadeTimer);
				this.fadeTimer.stop();
				if (this.fading == true) {
					this.fadeOpacity = 100;
					this.fadeElements[this.fadeIndex].style.opacity = this.fadeOpacity / 100;
					this.fadeElements[this.fadeIndex].style.filter = 'alpha(opacity='+this.fadeOpacity+')';
				} else {
					this.fadeIndex = (2*this.fadeCount+this.fadeIndex-1)%this.fadeCount;
					for (var i = 0; i < this.fadeCount; i++) {
						this.fadeElements[i].style.zIndex = (2*this.fadeCount+this.fadeIndex-i-1)%this.fadeCount;
					}
				}
				if (this.fadeActive == true) {
//					window.clearInterval(this.nextTimer);
//					this.nextTimer = window.setInterval(this.nextImage.bind(this), this.nextSpeed);
					this.nextTimer.stop();
					this.nextTimer = new PeriodicalExecuter(this.nextImage.bind(this), this.nextSpeed);
				}
				this.fading = false;
				break;
			case "fadeStop":
				if (this.fadeActive == true) {
//					window.clearInterval(this.fadeTimer);
//					window.clearInterval(this.nextTimer);
					this.fadeTimer.stop();
					this.nextTimer.stop();
					this.fadeOpacity = 100;
					this.fadeElements[this.fadeIndex].style.opacity = this.fadeOpacity / 100;
					this.fadeElements[this.fadeIndex].style.filter = 'alpha(opacity='+this.fadeOpacity+')';
					this.fadeActive = false;
					this.fading = false;
				}
				break;
			case "fadePlay":
				if (this.fadeActive == false) {
					this.nextImage();
//					this.nextTimer = window.setInterval(this.nextImage.bind(this), this.nextSpeed);
					this.nextTimer = new PeriodicalExecuter(this.nextImage.bind(this), this.nextSpeed);
					this.fadeActive = true;
				}
				break;
			case "fadeNext":
//				window.clearInterval(this.fadeTimer);
				this.fadeTimer.stop();
				for (var i = 0; i < this.fadeCount; i++) {
					this.fadeElements[i].style.zIndex = (2*this.fadeCount+this.fadeIndex-i)%this.fadeCount;
				}
				this.fadeOpacity = 100;
				this.fadeElements[this.fadeIndex].style.opacity = this.fadeOpacity / 100;
				this.fadeElements[this.fadeIndex].style.filter = 'alpha(opacity='+this.fadeOpacity+')';
				this.fadeIndex = (this.fadeIndex+1+this.fadeCount)%this.fadeCount;
				if (this.fadeActive == true) {
//					window.clearInterval(this.nextTimer);
//					this.nextTimer = window.setInterval(this.nextImage.bind(this), this.nextSpeed);
					this.nextTimer.stop();
					this.nextTimer = new PeriodicalExecuter(this.nextImage.bind(this), this.nextSpeed);
				}
				this.fading = false;
				break;
			default:
				break;
		}
	}
	
});

function faderGalleryInit() {
	$$('.fadeCont').each(function(element) {
		new FadeGallery(element);
	});
}

if ('observe' in document) {
	document.observe('dom:loaded', function() {
		faderGalleryInit();
	});
}

