/*
** FileName:	slideshow.js
** Author:		Jia Yu
** Date:		07-04-2006
** Ver:			0.2.1
** Path:		
*/

var messageStr = "Yes";


function SlideShow( parent, thedata , width, height,  frontpageindex,  ptp , ts )
{
	var _this = this;
	
	
	var picTimePeriod = ptp || 3000;
	var tranSteps = ts || 10;
	var intervalTime = 50;
	
	var runing = false;
	
	var mainintervalID = 0;
	var picchintervalID = 0;
	
	var picindex = frontpageindex || 0;
	var op = tranSteps;
	
	var imgindex = 0;
	var picshowimg = Array(2);
	var showingpichref = "";
	
	var picshowlinkdata = null;
	
	
//public porp	
	this.parent = parent;

//ini --
	if( typeof parent == "string" ) this.parent = document.getElementById( parent );
	
	this.parent.innerHTML = "";
	
	this.parent.style.position= "relative";
	this.parent.style.overflow = "hidden";
	this.parent.style.width = width +"px" || "770px";
	this.parent.style.height = height+"px" || "250px";
	
	picshowlinkdata = thedata;
	
	if( typeof thedata == "string" ) 
	{
		thedata = document.getElementById( thedata );
		picshowlinkdata = thedata.getElementsByTagName("img");
	}
	//alert( picshowlinkdata.length );

	//-----------------------
	if( picindex >= picshowlinkdata.length ) picindex = 0;
	
	
	for( var i=0; i<2; i++ )
	{
		picshowimg[i] = document.createElement("IMG");
		this.parent.appendChild( picshowimg[i] );
		picshowimg[i].className = "picshow";
		picshowimg[i].style.zIndex = 2 - i;
		picshowimg[i].style.position= "absolute";
		picshowimg[i].style.cursor= "pointer";
		picshowimg[i].border="0";
		picshowimg[i].style.visibility = "visible";
	}
	
	
	if( picshowlinkdata.length > 1 )
	{		
	
		showingpichref = picshowlinkdata[ picindex ].title;

		picshowimg[imgindex].src = picshowlinkdata[ picindex ].src; 
		picshowimg[imgindex].style.zIndex = 2;
		
		picindex = ( picindex+1 ) % picshowlinkdata.length
		picshowimg[  (imgindex+1) % 2 ].src = picshowlinkdata[ picindex ].src; 
		picshowimg[  (imgindex+1) % 2 ].style.zIndex = 0;
		
		//picshowimg[1].src = picshowlinkdata[ 1 ].firstChild.src; 
	}
	else  if( picshowlinkdata.length == 1 )
	{
		picshowimg[imgindex].src = picshowlinkdata[ picindex ].src; 
		picshowimg[imgindex].style.zIndex = 2;
		showingpichref = picshowlinkdata[ picindex ].title;
	}
	else
	{
		this.parent.innerHTML = "No pictures to show.";
	}

	
//private method

	this.transmitter = function()
	{
		if( runing || (op < tranSteps)  )
		{
			if( op < 0 )
			{
				op = tranSteps;
				window.clearInterval(picchintervalID);
				_this.changepic();
			}
			else
			{
				setOpacity( picshowimg[imgindex], 100*(op/tranSteps) );
				op--;
				picchintervalID = window.setTimeout(  _this.transmitter , intervalTime  );
			}
		}
	};
		
	
	this.changepic = function()
	{

			showingpichref = picshowlinkdata[ picindex ].title;
			picindex = picindex+1;
			if( picindex >= picshowlinkdata.length   )picindex =0;
			
	//		picindex = randomNumber( picshowdata.length );
			picshowimg[ (imgindex+1) % 2].style.zIndex = 2;
	
			picshowimg[imgindex].style.zIndex = 0;
			//picshowimg[ (imgindex+1) % 2 ].style.zIndex = 2;
			picshowimg[imgindex].src = picshowlinkdata[ picindex ].src; //picshowdata[ picindex ].src;
			picshowimg[imgindex].title = picshowlinkdata[ picindex ].title; //picshowdata[ picindex ].src;
			
			picshowimg[imgindex].style.zIndex = 1;
			setOpacity( picshowimg[imgindex] , 100 );
			
			imgindex = (imgindex+1) % 2;
			
			if( runing )
			{
				_this.run();
			}
	};


	
//events
	this.parent.onclick = function()
	{
		
		if( showingpichref != "undefined" && showingpichref != "" )
		{
			window.location = showingpichref; //picshowdata[ picindex ].src;
		}
		
	};


	this.parent.onmouseover = function()
	{
		_this.puase();
	};
	
	this.parent.onmouseout = function()
	{
		if( runing == false )
		{
			_this.run();
		}
	};
	


//public methods

	this.run = function()
	{
		runing = true;
		window.clearInterval(mainintervalID);
		mainintervalID = window.setTimeout( _this.transmitter , picTimePeriod );
	};
	
	
	this.puase = function()
	{
		runing = false;
		window.clearInterval(mainintervalID);
	};


	this.setPicStayTime = function(num)
	{
		picTimePeriod = num;
	};
	
	this.setMotionSteps = function(num)
	{
		tranSteps = num;	
	};
	
	this.setIntervalTime = function( num )
	{
		intervalTime = num;
	};

	this.isRuning = function()
	{
		return runing;
	};


}



//0-100
function setOpacity( obj,value ){
	if( document.all ){
				obj.style.filter = "alpha(opacity="+value+")";
	}
	else{
		value = value * 0.01;
		obj.style.opacity = value;
	}	
}



function randomNumber(limit)
{
		return Math.floor(Math.random()*limit);
}