/**
 * Nyitólapi slideshow
 **/
var MainSlideShow = {         //(name,width,height,pause,dir)
	width      : 342,
	height     : 232,
	pause      : 5000,
	dir        : 'images/mainslide',
	container  : 'mainslideshow',
	fadeimages : Array(),
	preimages  : Array(),

	curpos     : 10,
	degree     : 10,
	curcanvas  : "canvas0",
	curimgidx  : 0,
	nextimgidx : 1,

	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){
			// kepek elotoltese
			for (p=0;p<this.fadeimages.length;p++){
				this.preimages[p]=new Image();
				this.preimages[p].src=this.fadeimages[p];
			}

			this.printMe();
			this.start();
		}
	},

	add : function(img){
		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);
	},

	setContainer : function(id){
		this.container=id;
	},

	start : function(){
		var curcanvas=(this.ie4)? eval("document.all."+this.curcanvas) : document.getElementById(this.curcanvas);
		curcanvas.innerHTML=this.insertimage(this.curimgidx);
		if(this.fadeimages.length-1>this.curimgidx) this.nextimgidx=this.curimgidx+1; else this.nextimgidx=this.curimgidx;

		this.nextcanvas=(this.curcanvas=="canvas0")? "canvas1" : "canvas0";

		if(this.fadeimages.length>1) setTimeout("MainSlideShow.rotateimage()",this.pause);
	},

	insertimage : function(idx){
		return '<img src="'+this.dir+'/'+this.fadeimages[idx]+'" border="0" />';
	},

	rotateimage : function(){
		var crossobj=(this.ie4)? eval("document.all."+this.nextcanvas) : document.getElementById(this.nextcanvas)
		if(this.fadeimages.length-1>this.curimgidx) this.curimgidx++; else this.curimgidx=0;

		crossobj.innerHTML=this.insertimage(this.curimgidx);
		setTimeout("MainSlideShow.fade(0)",100);
	},

	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+=4;
			if(value>100) value=100;
			setTimeout("MainSlideShow.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;

			setTimeout("MainSlideShow.rotateimage()",this.pause);
		}
	},

	zoomImage : function(){
		if(ImageBox) ImageBox.show(this.directory+'/'+this.fadeimages[this.curimgidx]);
	},

	printMe : function(){
		if (this.ie4||this.dom){
			var container = document.getElementById(this.container);
			if(container==null) return false;
			var div       = document.createElement('div');
			var canvas0   = document.createElement('div');
			var canvas1   = document.createElement('div');

			div.style.position = "relative";
			div.style.width    = this.width+"px";
			div.style.height   = this.height+"px";
			div.style.overflow = "hidden";

			canvas0.setAttribute('id','canvas0');
			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;

			canvas1.setAttribute('id','canvas1');
			canvas1.style.position   = "absolute";
			canvas1.style.width      = this.width+"px";
			canvas1.style.height     = this.height+"px";
			canvas1.style.top        = 0;
			canvas1.style.left       = 0;
			canvas1.style.filter     = "alpha(opacity=0)";
			canvas1.style.MozOpacity = 0;
			canvas1.style.zIndex     = 50;

			div.appendChild(canvas0);
			div.appendChild(canvas1);

			container.appendChild(div);

			if(this.needZoom&&this.fadeimages.length>1){
				container.setAttribute('onclick','MainSlideShow.zoomImage();');
				container.style.cursor=(this.isIE())?'hand':'pointer';
			}
		}
	},

	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("MainSlideShow.init()");
	        oldQueue();
	    }
	}
}

MainSlideShow.addInit();