/**
 * Beágyazott slideshow
 *
 * Üres slideshow beillesztése: var myInSlide1=new IncSlideShow(1,'slideshow',320,140,[],[],myImageBox);
 **/

function IncSlideShow(id,container,width,height,images,desc,imageBox,initAjax,preview){
	this.id                 = id;
	this.name				= 'myInSlide'+id;
	this.width				= (width)?width:'auto';
	this.height				= (height)?height:'auto';
	this.contID				= (container)?container:null;
	this.imageBox			= (imageBox)?imageBox:null;
	this.preview			= preview;
	this.container;
	this.canvas;
	this.roller;
	this.leftBtn;
	this.rightBtn;
	this.images				= new Array;
	this.loaded				= new Array;
	this.imgleft			= new Array;
	this.description		= new Array;
	this.loadThumb			= true;
	this.relPath			= '';
	this.incdir				= 'inc';
	this.directory			= this.relPath+'images/inslideshow';
	this.thumbsdir			= this.relPath+'images/inslideshow/thumbs';
	this.suffix				= '_thumb';
	this.margin				= 5;
	this.rollwidth			= 0;
	this.pos				= 0;
	this.tmppos				= 0;
	this.maxpos				= 0;
	this.shadowColor		= 'gray';
	this.btnWidth           = 25;
	this.borderWidth		= 1;
	this.borderColor		= 'black';
	this.borderOverColor	= 'blue';
	this.backColor			= '#F2F2F2';
	this.canvasColor		= '#EEEEEE';
	this.current			= -1;
	this.autoTimeout;
	this.autodir			= 1;
	this.autoDelay			= 3000;
	this.doAuto				= true;
	this.timeout;
	this.xmlHttp            = null;
	this.zIndex				= 0;

	this.init=function(){
		this.container=document.getElementById(this.contID);
		if(this.width=='auto') this.width=this.container.parentNode.offsetWidth;
		this.container.style.border='solid 1px #E6E6E6';
		this.container.style.position='relative';
		this.container.style.zIndex=this.zIndex;

		if(this.isIE())
			this.leftBtn=document.createElement('<div onclick="'+this.name+'.scrollLeft()" onmouseover="'+this.name+'.btnOver(this,\'left\')" onmouseout="'+this.name+'.btnOut(this,\'left\')">');
		else {
			this.leftBtn=document.createElement('div');
			this.leftBtn.setAttribute('onclick',this.name+'.scrollLeft()');
			this.leftBtn.setAttribute('onmouseover',this.name+'.btnOver(this,"left")');
			this.leftBtn.setAttribute('onmouseout',this.name+'.btnOut(this,"left")');
		}
		this.leftBtn.style.background='url('+this.relPath+'images/inslideshow/leftarrow.gif) no-repeat 50% 50%';
		this.leftBtn.style.borderRight='solid 1px #E6E6E6';
		this.leftBtn.style.cursor=(this.isIE())?'hand':'pointer';
		this.leftBtn.style.position='absolute';
		this.leftBtn.style.zIndex=this.zIndex+5;
		this.container.appendChild(this.leftBtn);

		this.canvas=document.createElement('div');
		this.canvas.style.position='absolute';
		this.canvas.style.background=this.canvasColor;
		this.canvas.style.overflow='hidden';
		this.canvas.style.zIndex=this.zIndex+5;
		this.container.appendChild(this.canvas);

		if(this.isIE())
			this.rightBtn=document.createElement('<div onclick="'+this.name+'.scrollRight()" onmouseover="'+this.name+'.btnOver(this,\'right\')" onmouseout="'+this.name+'.btnOut(this,\'right\')">');
		else {
			this.rightBtn=document.createElement('div');
			this.rightBtn.setAttribute('onclick',this.name+'.scrollRight()');
			this.rightBtn.setAttribute('onmouseover',this.name+'.btnOver(this,"right")');
			this.rightBtn.setAttribute('onmouseout',this.name+'.btnOut(this,"right")');
		}
		this.rightBtn.style.background='url('+this.relPath+'images/inslideshow/rightarrow.gif) no-repeat 50% 50%';
		this.rightBtn.style.borderLeft='solid 1px #E6E6E6';
		this.rightBtn.style.cursor=(this.isIE())?'hand':'pointer';
		this.rightBtn.style.position='absolute';
		this.rightBtn.style.zIndex=this.zIndex+5;
		this.container.appendChild(this.rightBtn);

		this.roller=document.createElement('div');
		this.roller.style.position='relative';
		this.roller.style.textAlign='left';
		this.roller.style.zIndex=this.zIndex+1;
		this.canvas.appendChild(this.roller);

		this.setSize(this.width,this.height);
		this.loadThubnails();

		if(initAjax){
			this.xmlHttp=this.getXmlHttpObject();
			if(this.xmlHttp!=null){
				var	url=this.incdir+'/'+"InSlideShow.php?id="+this.id+"&do=getparams&rnd="+Math.random();
				if(this.preview) url+='&preview';

				this.xmlHttp.onreadystatechange=this.stateChanged;
				this.xmlHttp.open("GET",url,true);
				this.xmlHttp.send(null);

				if(!document.inSlideXmlObject) document.inSlideXmlObject=new Array;
				document.inSlideXmlObject[this.id]=this.xmlHttp;
			}
		}
	}

	this.stateChanged=function(){
		var obj;
		if(!document.inSlideXmlObject) return true;
		for(var i=0;i<document.inSlideXmlObject.length; i++){
			if(typeof document.inSlideXmlObject[i]=='undefined') continue;
			obj=document.inSlideXmlObject[i];
			if (obj&&(obj.readyState==4)){
				//alert(obj.responseText);
				eval(obj.responseText);
			}
		}
	}

	this.setParams=function(){
		this.margin			= parseInt(arguments[0]);
		this.borderWidth	= parseInt(arguments[1]);
		this.borderColor	= arguments[2];
		this.borderOverColor= arguments[3];
		this.shadowColor	= arguments[4];
		this.backColor		= arguments[5];
		this.canvasColor	= arguments[6];
		this.btnWidth		= parseInt(arguments[7]);
		this.doAuto			= (arguments[8]=='true')?true:false;
		this.autoDelay		= parseInt(arguments[9]);
		if(arguments[10]) this.incdir=arguments[10];

		if(this.contID&&document.getElementById(this.contID)) document.getElementById(this.contID).style.background=this.backColor;
	}

	this.setRelPath=function(rel){
		this.relPath = rel;
		this.directory = this.relPath+'images/inslideshow';
		this.thumbsdir = this.relPath+'images/inslideshow/thumbs';
	}

	this.setSize=function(w,h){
		if(w) this.width=(w!='auto')?w:this.container.parentNode.offsetWidth;
		if(h) this.height=h;

		this.container.style.width=this.width+'px';
		this.container.style.height=(this.height!='auto')?this.height+'px':'10px';
		this.canvas.style.width=(this.width-2*this.btnWidth)+'px';
		this.canvas.style.height='100%';
		this.canvas.style.left=this.btnWidth+1+'px';
		this.canvas.style.top='0px';
		this.leftBtn.style.width=this.btnWidth+'px';
		this.leftBtn.style.height='100%';
		this.leftBtn.style.left='0px';
		this.leftBtn.style.top='0px';
		this.rightBtn.style.width=this.btnWidth+'px';
		this.rightBtn.style.height='100%';
		this.rightBtn.style.left=(this.width-this.btnWidth)+'px';
		this.rightBtn.style.top='0px';
		this.roller.style.height=(this.height!='auto')?this.height+'px':'100px';
		this.roller.style.left=this.pos+'px';
	}

	this.btnOver=function(btn,lr){
		this.doAuto=false;
		btn.style.background='url('+this.relPath+'images/inslideshow/'+lr+'arrow_over.gif) no-repeat 50% 50%';
	}
	this.btnOut=function(btn,lr){
		this.doAuto=true;
		btn.style.background='url('+this.relPath+'images/inslideshow/'+lr+'arrow.gif) no-repeat 50% 50%';
	}

	this.scrollLeft=function(){
		clearTimeout(this.timeout);
		if(this.current>0) this.current--;
		this.scrollTo(this.imgleft[this.current]);
		this.autodir=-1;
	}

	this.scrollRight=function(){
		clearTimeout(this.timeout);
		if(this.current<this.images.length-1) this.current++;
		this.scrollTo(this.imgleft[this.current]);
		this.autodir=1;
	}

	this.autoscroll=function(){
		if(this.doAuto){
			if(this.current==this.images.length-1) this.autodir=-1;
			this.current+=this.autodir;
			this.scrollTo(this.imgleft[this.current]);
		}
		this.autoTimeout=setTimeout(this.name+'.autoscroll()',this.autoDelay);
	}

	this.pauseAutoScroll=function(){ this.doAuto=false; }
	this.resumeAutoScroll=function(){ this.doAuto=true; }

	this.imgOver=function(img){
		this.pauseAutoScroll();
		img.style.borderColor=this.borderOverColor;
	}
	this.imgOut=function(img){
		this.resumeAutoScroll();
		img.style.borderColor=this.borderColor;
	}

	this.zoomImage=function(src){
		if(this.imageBox){
			var j=-1;
			for(var i=0;i<this.images.length; i++) if(this.images[i]==src) j=i;
			if(j>-1) this.imageBox.show(this.directory+'/'+src,this.description[j]);
			else this.imageBox.show(this.directory+'/'+src);
		} else {
			var imgBox=document.createElement('div');
			imgBox.setAttribute('id','imgBoxLite');
			imgBox.setAttribute('onclick','document.body.removeChild(document.getElementById("imgBoxLite"));document.body.removeChild(document.getElementById("bigImg"));');
			imgBox.style.position='absolute';
			imgBox.style.zIndex='10000';
			imgBox.style.top='0px';
			imgBox.style.left='0px';
			imgBox.style.width='100%';
			imgBox.style.height='100%';
			imgBox.style.background='black';
			this.opacity(imgBox,80);

			var img=document.createElement('img');
			img.setAttribute('id','bigImg');
			img.setAttribute('src',this.directory+'/'+src);

			img.style.position='absolute';
			img.style.zIndex='10001';


			document.body.appendChild(img);
			document.body.appendChild(imgBox);

			img.style.width='600px';
			img.style.top='25%';
			img.style.left=((imgBox.offsetWidth-600)/2)+'px';
		}
	}

	this.scrollTo=function(x){
		this.tmppos=this.pos;
		if(x>=this.maxpos){
			this.pos=this.maxpos;
			this.autodir=-1;
		} else if(x<=0){
			this.pos=0;
			this.autodir=1;
		} else this.pos=x;
		this.scroll();
	}

	this.scroll=function(){
		if(Math.abs(this.tmppos-this.pos)>0.2){
			this.tmppos=this.tmppos+((this.pos-this.tmppos)/6);
			this.roller.style.left=-this.tmppos+'px';
			this.timeout=setTimeout(this.name+'.scroll()',50);
		}
	}

	this.refresh=function(){
		for(var i=0; i<this.roller.childNodes.length; i++) this.roller.removeChild(this.roller.childNodes[i]);
		this.loadThumb=true;
		this.loadThubnails();
	}

	this.loadThubnails=function(){
		if(!this.loadThumb) return true; else this.loadThumb=false;

		var thumbCont,thumb,src;
		for(var i=0; i<this.images.length; i++){
			thumbCont=document.createElement('div');
			thumbCont.style.margin=(this.margin-this.borderWidth)+'px';
			thumbCont.style.zIndex=this.zIndex+2;
			if(this.isIE()) thumbCont.style.styleFloat='left';
			else thumbCont.style.cssFloat='left';

			if(this.isIE()){
				src=this.thumbsdir+'/'+this.prepareSuffix(this.images[i],this.suffix);
				thumb=document.createElement('<img src="'+src+'" onclick="'+this.name+'.zoomImage(\''+this.images[i]+'\')" onload="'+this.name+'.imgOnLoad(this)" onmouseover="'+this.name+'.imgOver(this)" onmouseout="'+this.name+'.imgOut(this)">');
			} else {
				thumb=document.createElement('img');
				thumb.setAttribute('src',this.thumbsdir+'/'+this.prepareSuffix(this.images[i],this.suffix));
				thumb.setAttribute('onclick',this.name+'.zoomImage("'+this.images[i]+'")');
				thumb.setAttribute('onload',this.name+'.imgOnLoad(this)');
				thumb.setAttribute('onmouseover',this.name+'.imgOver(this)');
				thumb.setAttribute('onmouseout',this.name+'.imgOut(this)');
			}
			thumb.style.cursor=(this.isIE())?'hand':'pointer';
			thumb.style.display='none';
			thumb.style.position='relative';
			thumb.style.border='solid '+this.borderWidth+'px '+this.borderColor;
			thumb.style.zIndex=this.zIndex+4;

			thumbCont.appendChild(thumb);
			this.roller.appendChild(thumbCont);
		}
	}

	this.imgOnLoad=function(img){
		img.style.display='block';
		this.rollwidth+=img.width+2*this.margin;
		this.roller.style.width=this.rollwidth+'px';

		var i,src,testsrc;
		for(i=0; i<this.images.length; i++){
			src=this.thumbsdir+'/'+this.prepareSuffix(this.images[i],this.suffix);
			if(this.isIE()){
				testsrc=img.getAttribute('src').substring(this.strpos(img.getAttribute('src'),'images/'));
				src=src.substring(this.strpos(src,'images/'));
			} else testsrc=img.getAttribute('src');
			if(testsrc==src) this.loaded[i]=true;
		}

		var allLoaded=true;
		for(i=0; i<this.loaded.length; i++) allLoaded=(allLoaded&&this.loaded[i]);

		if(allLoaded){
			var shadow,width=0,height=0;
			var arImages=this.container.getElementsByTagName('img');
			for(i=0;i<arImages.length;i++){
				this.imgleft[i]=arImages[i].offsetLeft-this.margin;

				shadow=document.createElement('div');
				shadow.style.position="absolute";
				shadow.style.top=(arImages[i].offsetTop+3)+"px";
				shadow.style.left=(arImages[i].offsetLeft+3)+"px";
				shadow.style.width=(arImages[i].offsetWidth)+"px";
				shadow.style.height=(arImages[i].offsetHeight)+"px";
				shadow.style.zIndex=Number(this.zIndex)+3;
				shadow.style.background=this.shadowColor;

				arImages[i].parentNode.appendChild(shadow);

				width+=arImages[i].parentNode.offsetWidth+2*this.margin;
				if(arImages[i].parentNode.offsetHeight>height) height=arImages[i].parentNode.offsetHeight;
			}

			if(this.width=='auto'){
				this.width=(width<=this.container.parentNode.offsetWidth)?width:this.container.parentNode.offsetWidth;
				this.container.style.width=this.width+'px';
			}

			if(this.height=='auto'){
				this.height=height;
				this.container.style.height=(this.height+2*this.margin)+'px';
			}

			this.maxpos=this.rollwidth-this.canvas.offsetWidth+this.margin;
			if(this.rollwidth>this.canvas.offsetWidth)
				this.timeout=setTimeout(this.name+'.autoscroll()',10);
		}
	}

	this.add=function(imgs,desc){
		if(imgs instanceof Array){
			for(var i=0;i<imgs.length;i++){
				if(!this.in_array(imgs[i],this.images)){
					this.images[this.images.length]=imgs[i];
					this.loaded[this.loaded.length]=false;
					if(desc[i]||(desc[i]=='')) this.description[this.description.length]=desc[i];
				}
			}
		} else {
			this.images[this.images.length]=imgs;
			this.loaded[this.loaded.length]=false;
			if(desc) this.description[this.description.length]=desc;
		}
	}

	this.remove=function(img){
		img.parentNode.parentNode.removeChild(img.parentNode);
	}

	this.prepareSuffix=function(name,suffix){
		return 'thumb_'+name;
		var pos=this.strpos(name,'.');
		return name.substring(0,pos)+suffix+name.substring(pos);
	}

	this.mouseCoords=function(ev){
		if(ev.pageX || ev.pageY) return {x:ev.pageX, y:ev.pageY};
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	}

	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"){
			if(value>1){value/=100;}
			element.style.opacity=value;
			return value;
		} else if(typeof element.style.filter!="undefined"){
			if(value<=1 && value>0){value*=100;}
			element.style.filter="alpha(opacity="+value+")";
			return value;
		}
		return null;
	}

	this.getXmlHttpObject=function(){
		var xmlHttp=null;
		try { xmlHttp=new XMLHttpRequest(); }
		catch (e) {
			try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		return xmlHttp;
	}

	this.isIE=function(){
		return (this.strpos(navigator.userAgent.toLowerCase(),"msie",0)>0);
	}

	this.strpos=function( haystack, needle, offset){
	    var i = haystack.indexOf( needle, offset ); // returns -1
	    return i >= 0 ? i : false;
	}

	this.in_array=function(needle, haystack, strict) {
		var found = false, key, strict = !!strict;

		for (key=0; key<haystack.length; key++) {
			if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
				found = true;
				break;
			}
		}
		return found;
	}

	this.addInit=function(name) {
	    var oldQueue = window.onload? window.onload: function() {};
	    window.onload = function() {
	    	eval(name+".init()");
	        oldQueue();
	    }
	}

	if(images) this.add(images,desc);
	this.addInit(this.name);
}
