/**
 * Képgaléria slideshow
 **/
var GallerySlideShow={
	width		: 200,
	height		: 200,
	pause		: 3000,
	dir			: null,
	container	: 'galleryslide',
	fadeimages	: Array(),
	subscripts	: Array(),

	curpos     	: 10,
	degree    	: 10,
	curcanvas	:'canvas0',
	curimgidx	: 0,
	nextimgidx	: 1,
	toHandler	: null,
	autorot		: false,

	ie4			: document.all,
	dom			: document.getElementById,
	needButton	: false,
	needZoom	: false,
	directory	: 'images/gallery',

	init : function(){
		if(this.fadeimages.length==0) this.fadeimages.push('default.jpg');
		var cont=document.getElementById(this.container);
		if(cont!=null){
			if(ImgPreloader){
				ImgPreloader.dir=this.dir;
				ImgPreloader.preLoad(this.fadeimages);
			}

			this.printMe();
			this.start();
		}
	},

	setParams : function(dir,cont,w,h,nBtn,nZm){
		this.dir=dir;
		this.container=cont;
		this.width=w;
		this.height=h;
		this.needButton=nBtn;
		this.needZoom=nZm;
	},

	add : function(img,desc){
		var i=0;
		if( img instanceof Array ) { for(i in img) if(typeof img[i]=='string') this.fadeimages.push(img[i]); }
		else if(typeof img=='string') this.fadeimages.push(img);
		if( desc instanceof Array ) { for(i in desc) if(typeof desc[i]=='string') this.subscripts.push(desc[i]); }
		else if(typeof desc=='string') this.subscripts.push(desc);
	},

	setContainer : function(id){ this.container=id; },

	start : function(){
		this.insertimage(this.curcanvas,this.curimgidx);
		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		if(this.autorot&&this.fadeimages.length>1) this.toHandler=setTimeout("GallerySlideShow.rotateimage()",this.pause);
		document.getElementById('descPanel').innerHTML=this.subscripts[this.curimgidx];
	},

	insertimage : function(cnv,idx){
		document.getElementById(cnv).firstChild.setAttribute('src',this.dir+'/'+this.fadeimages[idx]);
		//document.getElementById('descPanel').innerHTML=this.subscripts[idx];
	},

	rotateimage : function(){
		if(this.fadeimages.length-1>this.curimgidx) this.curimgidx++; else this.curimgidx=0;
		this.insertimage(this.nextcanvas,this.curimgidx);
		this.curcanvas=(this.nextcanvas=="canvas0")? "canvas1" : "canvas0";
		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		this.toHandler=setTimeout("GallerySlideShow.fade(0)",this.pause);
	},

	fade : function(value){
		if(value<100){
			var crossobj=(this.ie4)? eval("document.all."+this.curcanvas) : document.getElementById(this.curcanvas);
			this.opacity(crossobj,100-value);
			crossobj=(this.ie4)? eval("document.all."+this.nextcanvas) : document.getElementById(this.nextcanvas);
			this.opacity(crossobj,value);

			value+=8;
			if(value>=70) document.getElementById('descPanel').innerHTML=this.subscripts[this.curimgidx];
			if(value>100) value=100;
			this.toHandler=setTimeout("GallerySlideShow.fade("+value+")",50);
		} else {
			var crossobj=(this.ie4)? eval("document.all."+this.curcanvas) : document.getElementById(this.curcanvas);
			this.opacity(crossobj,0);
			var tmp=this.curcanvas;
			this.curcanvas=this.nextcanvas;
			this.nextcanvas=tmp;

			if(this.autorot) this.toHandler=setTimeout("GallerySlideShow.rotateimage()",100);
			else clearTimeout(this.toHandler);
		}
	},

	zoomImage : function(){
		if(ImageBox) ImageBox.show(this.directory+'/'+this.fadeimages[this.curimgidx]);
	},

	dofirst : function(){
		if(this.toHandler) clearTimeout(this.toHandler);
		this.curimgidx=0;
		this.insertimage(this.nextcanvas,this.curimgidx);
		this.curcanvas=(this.nextcanvas=="canvas0")? "canvas1" : "canvas0";
		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		this.toHandler=setTimeout("GallerySlideShow.fade(0)",100);
	},

	doprev : function(){
		if(this.toHandler) clearTimeout(this.toHandler);
		if(this.curimgidx>0) this.curimgidx--; else this.curimgidx=this.fadeimages.length-1;
		this.insertimage(this.nextcanvas,this.curimgidx);
		this.curcanvas=(this.nextcanvas=="canvas0")? "canvas1" : "canvas0";
		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		this.toHandler=setTimeout("GallerySlideShow.fade(0)",100);
	},

	dopause : function(btn){
		this.autorot=!this.autorot;
		if(!this.autorot){
			//clearTimeout(this.toHandler);
			btn.style.backgroundPosition='18px 0px';
		} else {
			this.toHandler=setTimeout("GallerySlideShow.rotateimage()",100);
			btn.style.backgroundPosition='0px 0px';
		}
	},

	donext : function(){
		if(this.toHandler) clearTimeout(this.toHandler);
		if(this.fadeimages.length-1>this.curimgidx) this.curimgidx++; else this.curimgidx=0;
		this.insertimage(this.nextcanvas,this.curimgidx);
		this.curcanvas=(this.nextcanvas=="canvas0")? "canvas1" : "canvas0";
		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		this.toHandler=setTimeout("GallerySlideShow.fade(0)",100);
	},

	dolast : function(){
		if(this.toHandler) clearTimeout(this.toHandler);
		this.curimgidx=this.fadeimages.length-1;
		this.insertimage(this.nextcanvas,this.curimgidx);
		this.curcanvas=(this.nextcanvas=="canvas0")? "canvas1" : "canvas0";
		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		this.toHandler=setTimeout("GallerySlideShow.fade(0)",100);
	},

	printMe : function(){
		if (this.ie4||this.dom){
			var container = document.getElementById(this.container);
			if(container==null) return false;
			if(this.isIE()){
				onclick=(this.needZoom&&this.fadeimages.length>1)?"GallerySlideShow.zoomImage();":'javascript:;';

				var div       = document.createElement('<div>');
				var canvas0   = document.createElement('<div id="canvas0" onclick="'+onclick+'">');
				var canvas1   = document.createElement('<div id="canvas1">');
				var img0      = document.createElement('<img>');
				var img1      = document.createElement('<img>');
				var descPanel = document.createElement('<div id="descPanel">');

				if(this.needZoom&&this.fadeimages.length>1) canvas0.style.cursor=(this.isIE())?'hand':'pointer';
			} else {
				var div       = document.createElement('div');
				var canvas0   = document.createElement('div');
				var canvas1   = document.createElement('div');
				var img0      = document.createElement('img');
				var img1      = document.createElement('img');
				var descPanel = document.createElement('div');

				canvas0.setAttribute('id','canvas0');
				canvas1.setAttribute('id','canvas1');
				descPanel.setAttribute('id','descPanel');

				if(this.needZoom&&this.fadeimages.length>1){
					canvas0.setAttribute('onclick','GallerySlideShow.zoomImage();');
					canvas0.style.cursor=(this.isIE())?'hand':'pointer';
				}
			}

			div.style.position = "relative";
			div.style.width    = this.width+"px";
			div.style.height   = this.height+"px";
			div.style.overflow = "";
			div.style.margin   = "15px 0px 0px 10px";//"0px auto 0px auto";

			canvas0.style.position   = "absolute";
			canvas0.style.width      = this.width+"px";
			canvas0.style.height     = this.height+"px";
			canvas0.style.top        = 0;
			canvas0.style.left       = 0;
			canvas0.style.filter     = "alpha(opacity=100)";
			canvas0.style.MozOpacity = 100;
			canvas0.style.zIndex     = 51;
			canvas0.style.background = '#000000';

			canvas1.style.position   = "absolute";
			canvas1.style.width      = this.width+"px";
			canvas1.style.height     = this.height+"px";
			canvas1.style.top        = '0px';
			canvas1.style.left       = '0px';
			canvas1.style.filter     = "alpha(opacity=0)";
			canvas1.style.MozOpacity = 0;
			canvas1.style.zIndex     = 50;
			canvas1.style.background = '#000000';

			descPanel.style.textAlign= "center";
			descPanel.style.width    = this.width+'px';
			descPanel.style.height   = '20px';
			descPanel.style.padding   = '5px';


			canvas0.appendChild(img0);
			canvas1.appendChild(img1);

			div.appendChild(canvas0);
			div.appendChild(canvas1);

			container.appendChild(div);
			container.appendChild(descPanel);

			if(this.needButton&&this.fadeimages.length>1){
				var buttonBar=document.createElement('div');
				var btns=new Array('first','prev','pause','next','last');
				var btnw=new Array(19,28,20,28,19);
				var btn=null;
				var l=43;
				for(var i=0;i<btns.length;i++){
					if(this.isIE()){
						if(btns[i]=='pause'){
							over="this.style.backgroundPosition=(GallerySlideShow.autorot)?'0px 18px':'18px 18px'";
							out="this.style.backgroundPosition=(GallerySlideShow.autorot)?'0px 0px':'18px 0px'";
						} else {
							over="this.style.backgroundPosition='0px 18px'";
							out="this.style.backgroundPosition='0px 0px'";
						}

						btn=document.createElement('<div onclick="GallerySlideShow.do'+btns[i]+'(this);" onmouseover="'+over+'" onmouseout="'+out+'">');
					} else {
						btn=document.createElement('div');
						if(btns[i]=='pause'){
							btn.setAttribute('onmouseover',"this.style.backgroundPosition=(GallerySlideShow.autorot)?'0px 18px':'18px 18px'");
							btn.setAttribute('onmouseout',"this.style.backgroundPosition=(GallerySlideShow.autorot)?'0px 0px':'18px 0px'");
						} else {
							btn.setAttribute('onmouseover',"this.style.backgroundPosition='0px 18px'");
							btn.setAttribute('onmouseout',"this.style.backgroundPosition='0px 0px'");
						}
						btn.setAttribute('onclick','GallerySlideShow.do'+btns[i]+'(this);');
					}

					btn.style.height='18px';
					btn.style.width=btnw[i]+'px';
					btn.style.background='url(images/controller-'+btns[i]+'.gif)';
					btn.style.position='absolute';
					btn.style.top='12px';
					btn.style.left=l+'px';
					btn.style.cursor=(this.isIE())?'hand':'pointer';
					if(btns[i]=='pause') btn.style.backgroundPosition=(GallerySlideShow.autorot)?'0px 0px':'18px 0px';
					buttonBar.appendChild(btn);

					l+=btnw[i]+10;
				}

				buttonBar.style.position='relative';
				buttonBar.style.border='none';
				buttonBar.style.width='238px';
				buttonBar.style.height='42px';
				buttonBar.style.margin='10px 0px 10px 90px';
				buttonBar.style.background='url(images/controller.png)';

				container.appendChild(buttonBar);
			}
		}
	},

	opacity : function(element, value){
		if(typeof element=="string"){element=document.getElementById(element);}
		if(!element){return null}
		value=parseFloat(value);
		if(isNaN(value)){return null;};
		if(typeof element.style.opacity!="undefined"){/*value>=0, <=1*/
			if(value>1){value/=100;};
			element.style.opacity=value;
			return value;
		} else if(typeof element.style.filter!="undefined"){/*value >=0, <=100*/
			if(value<=1 && value>0){value*=100;};
			element.style.filter="alpha(opacity="+value+")";
			return value;
		};
		return null;
	},

	isIE : function(){
		return (this.strpos(navigator.userAgent.toLowerCase(),"msie",0)>0);
	},

	strpos : function( haystack, needle, offset){
	    var i = haystack.indexOf( needle, offset ); // returns -1
	    return i >= 0 ? i : false;
	},

	addInit : function(){
	    var oldQueue = window.onload? window.onload: function() {};
	    window.onload = function() {
	    	eval("GallerySlideShow.init()");
	        oldQueue();
	    }
	}
}

GallerySlideShow.addInit();