// JavaScript Document
var theSlideShow=new Array();
var slideCountObj=0;

function _slideShow(id,bgColor,duration,autoReplay,heightDef){
	var xWidth=0;xHeight=0;
	this.idName=id;
	this.element=document.getElementById(id);
	this.interval=5;
	this.maxHeight=0;this.maxWidth=0;
	this.selectedElement=0;
	this.inopacity=100;
	this.opacity=0;
	this.inFading=0;
	this.timer=0;
	this.number=0;
	this.bgColor=bgColor;
	
	this.autoReplay=(autoReplay==1);
	(duration!=null) ? this.duration=duration : this.duration=20000;	
	
	if(!this.element){alert('slide document not found');return false;}
	/*reading all element was added*/
	this.elCount=this.element.childNodes.length;
	this.chlElement=new Array();
	//only className=='slideShow'

	if(this.elCount>1)
		{
			var btnLayer=document.createElement("div");
			with(btnLayer){
				id="buttonLayer";
				style.height="25px";
				style.backgroundColor="#FFFFFF";
				style.borderTop="1px solid #999999";
				style.opacity=.2;
				style.zIndex="800";
				style.filter="alpha(opacity='30')";
				style.clear='both';
			}
			this.buttonLayer=btnLayer;
			
			var buttonBlock=document.createElement("span");
			buttonBlock.className="buttonBlock";
			buttonBlock.innerHTML="&nbsp";
			this.buttonBlock=buttonBlock;
			
			this.element.parentNode.insertBefore(btnLayer,this.element.parentNode.lastChild);
			this.element.parentNode.insertBefore(buttonBlock,this.element.parentNode.lastChild);
		}
		
	for(i=0;i<this.elCount;i++){
		this.chlElement[i]=this.element.childNodes[i];

		//get heightElement
		//(this.element.childNodes[i].offsetHeight) ? xHeight=this.element.childNodes[i].offsetHeight : xHeight=this.element.childNodes[i].clientHeight;
		//(this.element.childNodes[i].offsetWidth) ? xWidth=this.element.childNodes[i].offsetWidth : xWidth=this.element.childNodes[i].clientWidth;
		if(this.chlElement[i].offsetHeight)
			{
				 xHeight=this.chlElement[i].offsetHeight;
				 xWidth=this.chlElement[i].offsetWidth
			}
		else
			{
				xHeight=this.chlElement[i].clientHeight;
				xWidth=this.chlElement[i].clientWidth;
			}
			
		
		//maximum
		(this.maxHeight>xHeight) ? this.maxHeight=this.maxHeight : this.maxHeight=xHeight;
		(this.maxWidth>xWidth) ? this.maxWidth=this.maxWidth : this.maxWidth=xWidth;
		
		if(heightDef==0)
			{
				this.element.style.height=this.maxHeight+"px";
				this.element.style.position="relative";
			}
		else
			{
				this.element.style.height=heightDef+"px";
				this.element.style.position="relative";
			}
		
		this.chlElement[i].style.position='absolute';
		this.chlElement[i].style.display="none";
		this.chlElement[i].style.height=this.maxHeight+"px";
		this.chlElement[i].style.width=this.maxWidth+"px";
		this.chlElement[i].style.backgroundColor=this.bgColor;
	
		this.selectedElement=i;
		
		if(this.elCount>1)
			{
				this.adButton(i,this.chlElement[i]);
				document.getElementById(this.idName+"button-"+this.selectedElement).className="buttonBox-selected";
			}
	}
	//this.setDimension();
}

_slideShow.prototype.setDimension=function(){
	this.changeImage(0);
	this.play();
}

_slideShow.prototype.adButton=function(number,el){
	var theButton=document.createElement("div");
	var obj=this;
	with(theButton){
		id=this.idName+"button-"+number;
		innerHTML=number+1;
		className="buttonBox";
		
		onmouseover=function(){obj.mosover(this,number);}
		onmouseout=function(){obj.mosout(this,number);}
		onclick=function(){obj.onclick(this,number);}
	}
	this.buttonBlock.insertBefore(theButton,this.buttonBlock.lastChild);
}

_slideShow.prototype.onclick=function(el,number){
	clearInterval(this.timer);
	if(number==this.selectedElement){return false;}
	document.getElementById(this.idName+"button-"+this.selectedElement).className="buttonBox";
	
	el.className="buttonBox-selected";
	this.changeImage(number);
	this.selectedElement=number;
}

_slideShow.prototype.mosover=function(el,number){
	if(number==this.selectedElement){return false;}
	el.className='buttonBox-over';
}

_slideShow.prototype.mosout=function(el,number){
	if(number==this.selectedElement){return false;}
	el.className="buttonBox";
}

_slideShow.prototype.changeImage=function(number){
	document.getElementById(this.idName+"button-"+this.selectedElement) ? document.getElementById(this.idName+"button-"+this.selectedElement).className="buttonBox" :false;
	document.getElementById(this.idName+"button-"+number) ? document.getElementById(this.idName+"button-"+number).className="buttonBox-selected" : false;
	this.fadein(this.element.childNodes[this.selectedElement],this.element.childNodes[number]);
	this.selectedElement=number;
}

_slideShow.prototype.fadein=function(currentLayer,layerSelected){
//".$this->id."-rollover-".$this->number." current div selected
//	document.getElementById("controlView").innerHTML=this.opacity;
	with(this){
		if(opacity>96)
		 {
			opacity=0;
			this.inopacity=100
			return;
		 }
		 
  	 	(inopacity>=0) ? this.inopacity-=interval : this.inopacity=0;
		
		if(currentLayer)
			{
				currentLayer.style.opacity=inopacity/100;
				currentLayer.style.filter="alpha(opacity='"+inopacity+"')";
				if(inopacity<=0){currentLayer.style.display='none';}
			}
		 
		if(layerSelected)
			{
				layerSelected.style.opacity=opacity/100;
				layerSelected.style.filter="alpha(opacity='"+opacity+"')";
				layerSelected.style.display='block';
			}
			
		opacity+=interval;
		clearTimeout(inFading);
		
		inFading=setTimeout(function(){fadein(currentLayer,layerSelected)},40);
	}
}

_slideShow.prototype.play=function(){
	if(this.chlElement.length==1){return false;}	
	with(this){
		if(this.selectedElement==this.chlElement.length-1)
			{
				this.number=0;
				if(!autoReplay)
					{clearInterval(timer);return;}
			}
		
		clearInterval(timer);	
		
		timer=setInterval(function(){play()},duration);
		
		changeImage(this.number);
		this.number++;
	}		
}

function setupSlideShow(){
	var slideElement=0;
	for(slideElement=0;slideElement<theSlideShow.length;slideElement++)
		{
			theSlideShow[ slideElement ].setDimension();
		}
	clearInterval(slideTimeOut);
}