/**
 * Nyitólapi slideshow
 **/
function MainSlideShow(name,width,height,pause,dir){
	this.name       = name;
	this.width      = width;
	this.height     = height;
	this.pause      = pause;
	this.dir        = dir;
	this.container  = 'mainslideshow';
	this.fadeimages = Array();
	this.preimages  = Array();

	this.curpos     = 10;
	this.degree     = 10;
	this.curcanvas  = "canvas0";
	this.curimgidx  = 0;
	this.nextimgidx = 1;

	this.ie4=document.all;
	this.dom=document.getElementById;

	this.init=function(){
		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();
		}
	}

	this.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);
	}

	this.setContainer=function(id){
		this.container=id;
	}

	this.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";

		setTimeout(this.name+".rotateimage()",this.pause);
	}

	this.insertimage=function(idx){
		return '<img src="'+this.dir+'/'+this.fadeimages[idx]+'" border="0" />';
	}

	this.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(this.name+".fade(0)",100);
	}

	this.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>100) value=100;
			setTimeout(this.name+".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(this.name+".rotateimage()",this.pause);
		}
	}

	this.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);
		}
	}

	this.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;
	}

	this.strpos=function( haystack, needle, offset){
	    var i = haystack.indexOf( needle, offset ); // returns -1
	    return i >= 0 ? i : false;
	}

	this.addInit(this.name);
}

