/**
 *
 * @author Jayme Jeffman Filho - www.jeffman.eng.br
 * @title Image Swapper Class
 * @date 03/10/2007
 */
// Code begin
//-----------------------------------------------
// Auxiliary classes
/** Image Record class constructor */
function ImageRecord (imgobj, delay)
{
	if( imgobj == null ) return null;
	if( delay == null ) delay = 500;
	this.imgobj = imgobj;
	this.delay = delay ;
}
//-----------------------------------------------
// main class code
// getter / setter methods 
function setFirstImage( index )
{
	if( index > 0 && index < this.images.length )
		this.firstImageIndex = index ;
}
//-----------------------------------------------
/** set an Image delay */
function setImageDelay(img_index, delay )
{
	this.images[img_index].delay = delay ;
}
//-----------------------------------------------
/** set All Images delay */
function setAllImagesDelay( delay )
{
	for( var i = 0; i < this.images.length; i++) 
		setImageDelay( i, delay );
}
//-----------------------------------------------
/** set Repetition Mode method */
function setRepeatForever( isForever )
{
	if(isForever || !isForever )
		this.repeatForever = isForever ;
}
//-----------------------------------------------
/** setRepetitions method */
function setRepetitions( numRep )
{
	if( numRep > 0 ) 
		this.maxLoop = numRep;
} 
//-----------------------------------------------
/** add image for swapping method */
function addImage( imgrecord )
{
	if( imgrecord == null ) return;
	this.images[this.images.length] = imgrecord;
	return ;
}
//-----------------------------------------------
/** swap method */
function swapImages()
{
	var index = -1;
	//	alert( "IntervalID = "+ this.intervalID ) ;
	if( this.intervalID != null )	
		try{ clearInterval(this.intervalID);}
		catch(e){alert( e.message ); }
	if( this.repeatForever || this.loopCount <= this.maxLoop )
	{ 
//		alert(  this.loopCount + "/" + this.maxLoop) ;
		if( this.currentImageIndex < 0 )
		{
			index = this.firstImageIndex ;
		}
		else
		{
			if( (this.currentImageIndex + 1) >=  this.images.length )
			{
				index = 0 ;
				this.loopCount++;
			}
			else 
				index = this.currentImageIndex + 1 ;
				
		}
		//alert("Indice da imagem = " + index );
		//alert("Delay da imagem = " + this.images[index].delay );
		//alert("IE"+ this.imgobj.src );
		
		// At this position the method has the next image to show
		if(this.useAlfaChannel || this.useImageClipping )
		{
			this.transitionImage.src = this.images[index].imgobj.src ;
			//this.transitionImage.style.display = "block";
			if(this.useImageClipping)
			{
				this.doImageClipping();
			}
			else if(this.useAlfaChannel)
			{
				this.doAlfaChannel();
			}	
			//alert("vou passar");
			this.imgobj.src = this.transitionImage.src ;
//			this.imgobj.style.display = "block";
//			if( this.transitionImage ) this.transitionImage.style.display = "none";
		}
		else
		{
			this.imgobj.src = this.images[index].imgobj.src ;
		}
		this.currentImageIndex = index;
		if( this.intervalID != null )	try{ clearInterval(this.intervalID);} catch(e){alert( e.message ); }
		try{ this.intervalID = setInterval('Swapper_Instances['+this.id+'].swapImages()',this.images[index].delay); }
		catch(e){alert(e.message);}
		//alert( "Novo IntervalID = " + this.intervalID ) ;
	}
}
//-----------------------------------------------
function clearTransitionsIntervals()
{
	this.transitionsIntervals.length = 0 ;
}
//-----------------------------------------------
function setTransitionsIntervals()
{
	if(!this.transitionImage ){alert('You must set the "Transition Image" first !'); return false; }

	var argv = setTransitionsIntervals.arguments;
  var argc = argv.length ;
	// retornar se for passado menos de dois valores
  if (argc<2){ return false ; } 
  
	// limpar intervalos anteriores
	this.clearTransitionsIntervals();
	// estabelecer novos intervalos
	for( var i = 0; i < argc; i++ )
	{
		try{ this.transitionsIntervals[this.transitionsIntervals.length] = parseInt(argv[i]);	}
		catch(e) { alert ("function setTransitionsIntervals \n" + e.message);}
	}
	//for(var i = 0; i < this.transitionsIntervals.length; i++) alert(i+ " = " + this.transitionsIntervals[i]);
}
//-----------------------------------------------
function setAlfaChannel( value )
{
	if(this.transitionsIntervals.length == 0 ) {alert('You must set the "Transition Intervals" first !'); return false; }
	this.useAlfaChannel = value ;
}
//-----------------------------------------------
function setImageClipping( value )
{
	if(this.transitionsIntervals.length == 0 ) {alert('You must set the "Transition Intervals" first !'); return false; }

	this.useImageClipping = value ;
}
//-----------------------------------------------
function setTransitionImage(imgobj)
{
	if(imgobj) this.transitionImage = imgobj ;
}
//-----------------------------------------------
function timeWait(millis)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }while(curDate-date < millis);
} 
//-----------------------------------------------
function doImageClipping()
{
/*
		if( this.intervalID != null )	try{ clearInterval(this.intervalID);} catch(e){alert( e.message ); }
		try{ this.intervalID = setInterval('Swapper_Instances['+this.id+'].swapImages()',this.images[index].delay); }
*/	
	
	var nStep = this.transitionsIntervals.length ;
	var sRect = 'rect(auto, auto, auto, ';
	
	for( var i = 0; i < nStep; i++)
	{
		//alert("passada "+ i);
		// rect ( top right bottom left); }
		if( this.intervalID != null )	try{ clearInterval(this.intervalID);} catch(e){alert( e.message ); }
		var wClip = Math.floor((i/nStep)* this.imgobj.width ) ;
		sRect = "rect(auto, auto, auto, " + (wClip < this.imgobj.width ? wClip : this.imgobj.width) + "px)" ;
		//alert(sRect);
		this.intervalID = setTimeout('Swapper_Instances['+this.id+'].clipIt("'+sRect+'")', this.transitionsIntervals[i]);
	}
	//this.imgobj.style.display = "none";
	try{ this.imgobj.style.clip = 'rect(auto, auto, auto, auto)';} catch(e){}
}
//-----------------------------------------------
function clipIt(aRect)
{
	this.imgobj.style.clip = aRect ; 
}
//-----------------------------------------------
function doAlfaChannel()
{
	var nStep = this.transitionsIntervals.length ;
	sOpa = "";
	
	for( var i = 0; i < nStep; i++)
	{
		sOpa = "" + i /	nStep;
		try{ this.imgobj.style.opacity = sOpa;} catch(e){}
		sOpa = "" + Math.floor((i * 100)/nStep) < 50 ? Math.floor((i * 100)/nStep) : 100 ;
		try{ this.imgobj.style.filter = "alpha(opacity="+sOpa+")";} catch(e){}
		timeWait(this.transitionsIntervals[i]);
	}
	this.imgobj.style.display = "none";
	try{ this.imgobj.style.opacity = "1"; } catch(e){}
	try{ this.imgobj.style.filter = "alpha(opacity=100)";} catch(e){}
}
//-----------------------------------------------
/** Image Swapper class constructor */
Swapper_Instances = new Array();
function JJImageSwapper (imgobj)
{
	if(imgobj == null ) return null; // || imgobj.classname is not "Image"
	
	this.id = Swapper_Instances.length;
  Swapper_Instances[this.id] = this;
  
	this.imgobj = imgobj ; // getElementById()
	this.transitionsIntervals = new Array();
	this.images = new Array();
	this.repeatForever = false ;
	this.maxLoop = 5;
	this.intervalID = null;
	this.firstImageIndex = 1 ;
	this.currentImageIndex = -1 ; // image swapping not started
	this.loopCount = 0;
	this.useAlfaChannel = false ;
	this.useImageClipping = false ;
	this.transitionImage = null;
	// Methods assignement
	this.addImage = addImage;
	this.setFirstImage = setFirstImage;
	this.setImageDelay = setImageDelay;
	this.setAllImagesDelay = setAllImagesDelay;
	this.setRepeatForever = setRepeatForever ;
	this.setRepetitions = setRepetitions;
	this.swapImages = swapImages;
	this.clearTransitionsIntervals = clearTransitionsIntervals ;
	this.setTransitionsIntervals = setTransitionsIntervals ;
	this.setTransitionImage = setTransitionImage ;
	this.setImageClipping = setImageClipping;
	this.setAlfaChannel = setAlfaChannel;
	this.doImageClipping = doImageClipping;
	this.doAlfaChannel = doAlfaChannel;
	this.clipIt = clipIt;
	//debug alerts
	//alert(images);
	//alert("array de imagens = " +this.images) ;
	return this;
}

