var globalScope = new Array();
function ieIntervalHandler( id, strFunc,speed )
{


	/* D.1 - correct the scope then make the call */
	var scope = globalScope[id];
	eval( "scope." + strFunc + "()" );
}

function setTimeOutObject(obj,strFunc,uniqueId,speed){
if( document.all )
   {
	globalScope[uniqueId]=obj;
	   

	var a=1;
	
	var to=window.setTimeout( 'ieIntervalHandler("' + uniqueId + '","'+strFunc+'")', speed );
   }
   else{
	var to=window.setTimeout(function(thisObj) { eval('thisObj.'+strFunc+'()'); }, speed, obj);
	}
	
	return to;
}

function SlideShow(gallery,speed,smooth){

	this._corrente=0;
	this._passato=0;
	this._passi=20;
	this._smooth=smooth;
	this._speed=speed;
	this._gallery=gallery;
	//this._stop.onclick=this.stop;
	this._exit=false;
	this._imgs=gallery.getElementsByTagName('img');
	var date=new Date();
	this.uniqueId =  date.getTime();

	globalScope[this.uniqueId] = this;
	this._fade=new Fade(this._imgs[this._passato],0);
	
}
SlideShow.prototype._corrente;
SlideShow.prototype._fade;
SlideShow.prototype._passato;
SlideShow.prototype._passi;
SlideShow.prototype._smooth;
SlideShow.prototype._speed;
SlideShow.prototype._gallery;
SlideShow.prototype._imgs;
SlideShow.prototype.uniqueId;
SlideShow.prototype.t0;
SlideShow.prototype.play =function(){


	//reset past object
	this._imgs[this._passato].className="hidden";
	this._imgs[this._passato].style.zIndex=0;
	this._fade.solid();

	this._fade=null;

	//reset current object
	var corrente=this._imgs[this._corrente];

	corrente.className="show";
				

	corrente.style.zIndex=10000;
	
	//swap objects
	this._passato=this._corrente;
	this._corrente++;
	
	if(this._corrente>this._imgs.length-1){
		//check completed loop
		this._corrente=0;
	}
	//send current object on background
	corrente=this._imgs[this._corrente];
	corrente.className="show";
	corrente.style.zIndex=0;
	
	var passato=this._imgs[this._passato];
	//create fade oject for past object
   this._fade=new Fade(passato,this._smooth);
   //fadeout past object
	this._fade.out();
	
	//recall play
	this.t0=setTimeOutObject(this,'play',this.uniqueId,this._speed);
}

SlideShow.prototype.reversePlay =function(){
	//reset past object
	this._imgs[this._passato].className="hidden";
	this._imgs[this._passato].style.zIndex=0;
	this._fade.solid();
	
	//reset current object
	var corrente=this._imgs[this._corrente];
	corrente.className="show";
	corrente.style.zIndex=10000;
	
	//swap objects
	this._passato=this._corrente;
	this._corrente--;
	
	if(this._corrente<0){
		//check completed loop
		this._corrente=this._imgs.length-1;
	}
	//send current object on background
	corrente=this._imgs[this._corrente];
	corrente.className="show";
	corrente.style.zIndex=0;
	
	var passato=this._imgs[this._passato];
	//create fade oject for past object
   this._fade=new Fade(passato,this._smooth);
   //fadeout past object
	this._fade.out();
	//recall play
	if(!this._exit){
		this.t0=setTimeOutObject(this,'play',this.uniqueId,this._speed);
	}
}
SlideShow.prototype.stop =function(){
	alert(this.t0);
	window.clearTimeout(this.t0);
	this._exit=true;
}

function Fade(obj,smooth){
 
 this._smooth=smooth;
 this._obj=obj;
 this._fadeout=0;
 this._fadein=smooth;
 var date=new Date();
 this.uniqueId =  date.getTime()+parseInt(Math.random()*30);
 globalScope[this.uniqueId] = this;
 this.out=function(){

 	var value=((this._smooth-this._fadeout)/this._smooth)*10;
 	
	this.setOpacity(value);
	
	this._fadeout++;
	
	if (this._fadeout>this._smooth) {
	this._fadeout=0;
	return true;}
	else{
	
	 t1=setTimeOutObject(this,'out', this.uniqueId,1);
		 	}
 }
 
 this.fadein=function(){
 	var value=((this._smooth-this._fadein)/this._smooth)*10;
	this.setOpacity(value);
	this._fadein--;
	if (this._fadein<0) {
	this._fadein=this._smooth;
	return true;}
	else{
	 t1=setTimeOutObject(this,'in',this.uniqueId,100);
		 	}
 }

 
 this.transparent=function(){
 	this.setOpacity(0);
 }
  this.solid=function(){
 	this.setOpacity(10);
 }
 this.setOpacity=function(value){
 	
 	 if(document.all){
 	 this._obj.style.filter ="alpha(opacity="+(value*10)+")";
 	 }
 	 else {this._obj.style.opacity = value/10;}
	 }
}
