var faderController = Class.create();

faderController.prototype = {
	initialize: function (container) {
		this.container = $(container);
		if(this.container) {
			//tmpimg = this.container.descendants();
			tmpimg = this.container.getElementsByTagName('IMG');
			this.images = new Array();
			for(x = 0; x < tmpimg.length; ++x){
				this.images.push(Element.extend(tmpimg[x]));
			}
			
			tmplinks = this.container.getElementsByTagName('A');
			this.links = new Array();
			for(x = 0; x < tmplinks.length; ++x){
				this.links.push(tmplinks[x].toString());
			}
			
			this.timeoutRef = null;
			this.timeoutRef2 = null;
			this.interval = 6000;
			this.faderPosition = 0;
			this.nextFade = 0;
			this.canSwitch = true;
			this.bufferClick = '';
			
			this.setLinks(0);

			if(this.images.length > 1) {
				this.container.setStyle({
					overflow: 'hidden',
					backgroundImage: 'url("'+this.images[0].src+'")',
					backgroundPosition: 'top left',
					backgroundRepeat: 'no-repeat'
				});

				this.images.each(function(s){
					s.setStyle({
					position: 'absolute',
					opacity: 0
					});
				});

				window.faderControllerReference = this;
				this.setFade();
			}
		}
	},

	setFade: function () {
		this.timeoutRef = window.setTimeout('window.faderControllerReference.fade()', this.interval);
	},
	
	stopFade: function() {
		window.clearTimeout(this.timeoutRef);
	},
	
	getShowedPicture: function() {
		cssValue = this.container.getStyle('backgroundImage');
		cssValue = cssValue.replace('url(', '');
		cssValue = cssValue.replace(')', '');
		cssValue = cssValue.replace('"', '');
		cssValue = cssValue.replace('\'', '');
		return cssValue;
	},

	fade: function () {
		this.prepareNext();
	
		//ma kam fadovat?
		if(this.faderPosition + 1 < this.images.length) {
			this.nextFade = this.faderPosition + 1;
		} else {
			this.nextFade = 0;
		}
		
		this.faderPosition = this.nextFade;
		this.setLinks(this.faderPosition);

		//zobrazit dalsi a po dokonceni ho hodit na pozadi
		this.canSwitch = false;
		new Effect.Opacity(this.images[this.faderPosition], {duration:1, from:0, to:1});
		//this.timeoutRef = window.setTimeout('window.faderControllerReference.prepareNext()', 1100);
		this.timeoutRef = window.setTimeout('window.faderControllerReference.canSwitchOn()', 1100);
		this.setFade();
	},
	
	fadePrevious: function() {
		//aktivni na pozadi
		aimg = this.images[this.faderPosition];

		this.container.setStyle({
			backgroundImage: 'url("'+aimg.src+'")'
		});

		aimg.setStyle({
			opacity: 0
		});
				
		if(this.faderPosition - 1 >= 0) {
			prevPic = this.faderPosition - 1;
		} else {
			prevPic = this.images.length - 1;
		}
		
		this.nextFade = prevPic;
		this.faderPosition = prevPic;
		
		//zobrazit predchozi
		this.canSwitch = false;
		new Effect.Opacity(this.images[this.faderPosition], {duration:1, from:0, to:1});
		this.timeoutRef = window.setTimeout('window.faderControllerReference.canSwitchOn()', 1100);
		this.setFade();
	},

	prepareNext: function () {
		aimg = this.images[this.nextFade];

		this.container.setStyle({
			backgroundImage: 'url("'+aimg.src+'")'
		});

		aimg.setStyle({
			opacity: 0
		});

		//this.setFade();
	},
	
	setLinks: function(index) {
		tmplinks = this.container.getElementsByTagName('A');
		for(x = 0; x < tmplinks.length; ++x){
			tmplinks[x].href = this.links[index];
		}
	},
	
	canSwitchOn: function() {
		this.canSwitch = true;
		if(this.bufferClick == 'next')this.nextImage();
		else if(this.bufferClick == 'previous')this.previousImage();
	},
	
	nextImage: function() {
		if(this.canSwitch == true) {
			this.stopFade();
			this.fade();
			this.bufferClick = '';
		} else {
			this.bufferClick = 'next';
		}
	},
	
	previousImage: function() {
		if(this.canSwitch == true) {
			this.stopFade();
			this.fadePrevious();
			this.bufferClick = '';
		} else {
			this.bufferClick = 'previous';
		}
	}
}
