if(!Utils) throw('FadeShow betoltesehez hianyzik: Utils');

var FadeShow = {
	container : 'fadeshow_container',
	buttonContainer : null,
	descContainer : null,
	descContent : null,
	descHeader : null,
	descText : null,
	imageContainer: null,
	borderContainer: null,

	width : 0,
	height : 0,
	buttonSize : 14,
	buttonMargin : 6,
	buttonBg : 'rgb(50,50,50)',
	//buttonBg : 'url(images/three_0.gif) repeat-x center -5px',
	descHeight : 0,
	descPadding : 0,
	descBg : '#0F7820',

	images : [],
	location : 'images/fadeshow',

	current : 0,
	previous : -1,
	autorot : true,
	delay : 5000,

	timeout : null,
	timeoutShow : null,
	timeoutHide : null,
	timeoutDescShow : null,
	timeoutDescHide : null,
	timeoutImage : null,
	buttonOpac : 0,
	buttonOpacMax : 80,
	buttonOpacDif : 10,
	descOpac : 0,
	descOpacMax : (Utils.isIE)?70:80,
	descOpacDif : 10,
	imageOpac : 0,
	imageOpacMax : 100,
	imageOpacDif : 10,

	init : function(){
		this.container=$(this.container);
		this.width=this.container.offsetWidth;
		this.height=this.container.offsetHeight;

		Utils.addListener(this.container,'mouseover',this.mouseOver);
		Utils.addListener(this.container,'mouseout',this.mouseOut);

		if(ImgPreloader){
			var images=[];
			for(var i=0;i<this.images.length;i++) if(this.images[i].image) images[images.length]=this.images[i].image;
			ImgPreloader.preLoad(images);
		}

		var defImage=(typeof(this.container.firstChild.getAttribute)=='function')?this.container.firstChild.getAttribute('src'):false;
		if(defImage){
			defImage=defImage.split('/');
			for(var i=0;i<this.images.length;i++) if(defImage[defImage.length-1]==this.images[i].image) this.current=i;
		}

		this.create();
	},

	create : function(){
		if(this.container){
			Utils.removeChildren(this.container);
			//Utils.setStyle(this.container,{'position':'relative'});

			this.imageContainer = Ł('div',{'id':'fs_imageContainer'},{
				'width'		: this.width+'px',
				'height'	: this.height+'px'
			});

			this.borderContainer = Ł('div',{'id':'fs_borderContainer'},{
				'width'		: this.width+'px',
				'height'	: this.height+'px',
				'position'	: 'absolute',
				'top'		: '0px',
				'left'		: '0px',
				'background': 'url(images/fadeshow/border.png)'
			});

			this.buttonContainer = Ł('div',{'id':'fs_buttonContainer'},{
				'width'		: this.width+'px',
				'height'	: (this.buttonSize+2*this.buttonMargin+2)+'px',
				'position'	: 'absolute',
				'top'		: '0px',
				//'top'		: '143px',
				//'top'		: '15px',
				'left'		: '0px',
				'background': this.buttonBg,
				'clear'		: 'left'
			});
			Utils.changeOpac(this.buttonContainer,this.buttonOpac);

			this.descContainer = Ł('div',{'id':'fs_descContainer'},{
				'width'		: (this.width-2*this.descPadding)+'px',
				'height'	: (this.descHeight-2*this.descPadding)+'px',
				'position'	: 'absolute',
				'top'		: (this.height-this.descHeight)+'px',
				'left'		: '0px',
				'background': this.descBg,
				'padding'	: this.descPadding+'px'
			});
			Utils.changeOpac(this.descContainer,this.descOpac);

			this.descContent = Ł('div',{'id':'fs_descContent'},{
				'width'		: (this.width-2*this.descPadding)+'px',
				'height'	: (this.descHeight-2*this.descPadding)+'px',
				'position'	: 'relative',
				'top'		: '0px',
				'left'		: '0px',
			});

			this.descHeader = Ł('h1',{'id':'fs_descHeader'},{
				'width'		: (this.width-2*this.descPadding-20)+'px',
				'margin'	: '5px 10px 10px 10px',
				'fontSize'	: '18px'
			});
			this.descText = Ł('p',{'id':'fs_descText'},{
				'width'		: (this.width-2*this.descPadding-10)+'px',
				'margin'	: '5px',
				'fontSize'	: '14px'
			});

			Utils.appendChild(this.container,
				this.imageContainer,
				this.borderContainer,
				this.buttonContainer,
				Utils.appendChild(this.descContainer,
					Utils.appendChild(this.descContent,
						this.descHeader,
						this.descText
					)
				)
			);

			this.displayButtons();
			this.loadImages();
			this.loadImage(this.current);
		}
	},

	displayButtons : function(){
		for(var i=0;i<this.images.length;i++){
			Utils.appendChild(this.buttonContainer,this.createButton(i))
		}
	},

	loadImage : function(i){
		Utils.removeChildren(this.descHeader);
		Utils.removeChildren(this.descText);

		if(this.images[i].title){
			Utils.appendChild(this.descHeader,
				document.createTextNode(this.images[i].title)
			);
		}
		if(this.images[i].desc){
			Utils.appendChild(this.descText,
				document.createTextNode(this.images[i].desc)
			);
		}

		Utils.setStyle($('btn_FadeShow_'+i),{'background':'#662D91'});
		this.showDesc();
		this.changeImage();
	},

	loadImages : function(){
		var image;
		for(var i=0;i<this.images.length;i++){
			image=Ł('img',{
				'id'		: 'fs_image_'+i,
				'src'		: this.location+'/'+this.images[i].image
			},{
				'position'	: 'absolute',
				'top'		: '0px',
				'left'		: '0px',
				'cursor'	: 'pointer',
				'display'	: 'none',
				'width'		: this.width+'px',
				'height'	: this.height+'px'
			});

			Utils.appendChild(this.imageContainer,image);
		}
	},

	change : function(e){
		var ed=Utils.getEventDetails(e);
		var idx=ed.target.getAttribute('idx');

		if(idx&&(idx!=FadeShow.current)){
			Utils.setStyle($('btn_FadeShow_'+FadeShow.current),{'background':'#fff'});
			FadeShow.previous=FadeShow.current;
			FadeShow.current=idx;
			FadeShow.hideDesc();
		}
	},

	randomImage : function(){
		var idx=parseInt(Math.random()*this.images.length);
		if(idx&&(idx!=FadeShow.current)){
			Utils.setStyle($('btn_FadeShow_'+FadeShow.current),{'background':'#fff'});
			FadeShow.previous=FadeShow.current;
			FadeShow.current=idx;
			FadeShow.hideDesc();
		}
	},

	rotateImage : function(){
		Utils.setStyle($('btn_FadeShow_'+this.current),{'background':'#fff'});
		this.previous=this.current;
		this.current++;
		if(this.current>=this.images.length) this.current=0;
		this.hideDesc();
	},

	add : function(images){
		if(images instanceof Array) for(var i=0;i<images.length;i++) this.images[this.images.length]=images[i];
		else this.images[this.images.length]=images;
	},

	createButton : function(idx){
		var button=Ł('div',{'id':'btn_FadeShow_'+idx,'idx':idx},{
			'width'		: this.buttonSize+'px',
			'height'	: this.buttonSize+'px',
			'margin'	: this.buttonMargin+'px',
			'float'		: 'left',
			'background': '#fff',
			'border'	: 'solid 1px #000',
			'color'		: '#000',
			'textAlign'	: 'center',
			'fontWeight': 'bold',
			'fontSize'	: '11px',
			'cursor'	: 'pointer'
		});
		Utils.appendChild(button,document.createTextNode(idx+1));
		Utils.addListener(button,'click',this.change);
		Utils.addListener(button,'mouseover',function(e){
			var ed=Utils.getEventDetails(e);
			Utils.setStyle(ed.target,{'borderColor':'red'});
		});
		Utils.addListener(button,'mouseout',function(e){
			var ed=Utils.getEventDetails(e);
			Utils.setStyle(ed.target,{'borderColor':'#000'});
		});
		return button;
	},

	showDesc : function(){
		if(this.descOpac+this.descOpacDif<this.descOpacMax) this.descOpac+=this.descOpacDif;
		else this.descOpac=this.descOpacMax;

		Utils.changeOpac(this.descContainer,this.descOpac);

		if(this.descOpac<this.descOpacMax) this.timeoutDescShow=setTimeout('FadeShow.showDesc();',20);
		else if(this.autorot) this.timeout=setTimeout('FadeShow.rotateImage();',this.delay);
	},

	hideDesc : function(){
		if(this.timeoutDescShow) clearTimeout(this.timeoutDescShow);
		if(this.descOpac-this.descOpacDif>0) this.descOpac-=this.descOpacDif;
		else this.descOpac=0;

		Utils.changeOpac(this.descContainer,this.descOpac);

		if(this.descOpac>0) this.timeoutDescHide=setTimeout('FadeShow.hideDesc();',20);
		else this.loadImage(this.current);
	},

	showButtons : function(){
		if(this.buttonOpac+this.buttonOpacDif<this.buttonOpacMax) this.buttonOpac+=this.buttonOpacDif;
		else this.buttonOpac=this.buttonOpacMax;

		Utils.changeOpac(this.buttonContainer,this.buttonOpac);

		if(this.buttonOpac<this.buttonOpacMax) this.timeoutShow=setTimeout('FadeShow.showButtons();',20);
	},

	hideButtons : function(){
		if(this.timeoutShow) clearTimeout(this.timeoutShow);
		if(this.buttonOpac-this.buttonOpacDif>0) this.buttonOpac-=this.buttonOpacDif;
		else this.buttonOpac=0;

		Utils.changeOpac(this.buttonContainer,this.buttonOpac);

		if(this.buttonOpac>0) this.timeoutHide=setTimeout('FadeShow.hideButtons();',20);
	},

	changeImage : function(){
		if(this.imageOpac+this.imageOpacDif<this.imageOpacMax) this.imageOpac+=this.imageOpacDif;
		else this.imageOpac=this.imageOpacMax;

		if(this.previous>-1){
			var imgPrevious=$('fs_image_'+this.previous);
			if(imgPrevious) Utils.changeOpac(imgPrevious,this.imageOpacMax-this.imageOpac);
		}

		var imgCurrent=$('fs_image_'+this.current);
		if(imgCurrent){
			if(imgCurrent.style.display!='block') Utils.setStyle(imgCurrent,{'display':'block'});
			Utils.changeOpac(imgCurrent,this.imageOpac);
		}

		if(this.imageOpac<this.imageOpacMax) this.timeoutImage=setTimeout('FadeShow.changeImage();',50);
		else {
			this.imageOpac=0;
			if(imgPrevious) Utils.setStyle(imgPrevious,{'display':'none'});
		}
	},

	isMouseOver : function(e){
		var ed=Utils.getEventDetails(e);
		var pos=Utils.getPosition(this.container);

		return ((ed.mouseX>pos.left)&&(ed.mouseX<(pos.left+this.width))&&(ed.mouseY>pos.top)&&(ed.mouseY<(pos.top+this.height-100)));
	},

	mouseOver : function(e){
		if(FadeShow.timeout) clearTimeout(FadeShow.timeout);
		FadeShow.showButtons();
	},

	mouseOut : function(e){
		if(!FadeShow.isMouseOver(e)){
			FadeShow.hideButtons();
			if(FadeShow.autorot) FadeShow.timeout=setTimeout('FadeShow.rotateImage();',FadeShow.delay);
		}
	}
}

Utils.addInit('FadeShow.init');
