// JavaScript Document

//array to store scope ID's
var globalScope = new Array();

// set Interval handler function with scope corrected that works with both IE and Firefox
function IntervalHandler( id, strFunc )
{
	// correct the scope then make the call 
	var scope = globalScope[id];
	if (scope != null)
	{
		eval( "scope." + strFunc );
	}
}

//this class rotates an set on images
function Rotator() 
{
	//variables
	var me = this;
	this.Images = new Array();	//string array of image urls to rotate in the order they should eb rotated
	this.ElementId = '';	//the html id of the image object to rotate
	this.RotateSpeed = 5000; //the speed in which images are rotated in milliseconds. Not used if FadeImages is true
	this.FadeImages = true;	//if true the images will fade in and out. If true OpacityIncSpeed, OpacityPause100 and OpacityPause0 is used instead of RotateSpeed
	this.Opacity = 0;	//the current image opactity
	this.OpacityInc = 2;	//The amount to increment opacity by.
	this.OpacityIncSpeed = 50; //the speed in which opacity is incremented in milliseconds
	this.OpacityPause100 = 250; //the length in milliseconds that the fade is paused while the image opacity is 100 
	this.OpacityPause0 = 250; //the length in milliseconds that the fade is paused while the image opacity is 0 (not visible) 
	this.ImageElement = new Image;
	this.ImagesLoaded = false;	//true when all the images are loaded
	this.RotateTimeOutID = 0;
	this.FadeTimeOutID = 0;
	this.MaxLoop = 0;	//the maximum number of loops to do. If 0 then loops will be continuous
	this.LoopCount = -1; 	//the number of loops completed
	this.LoopStartImageIndex = 0;	//the image index the looping started at
	this.ImageIndex = 0; //the current image index
	
	//methods
	
	//starts rotation at a nominated index. ImageIndex is optional
	this.Start = function(StartImageIndex)
	{
		//add this class instance ID to the scope array - use page element Id as this should be unique
		globalScope[ me.ElementId ] = this;
		
		//get page element where rotated images will be displayed
		me.ImageElement = document.getElementById(me.ElementId);
			
		//check if images loaded
		if (!me.ImagesLoaded)
		{
			//start loading images 
			me.LoadImage(0);
		}
		
		//check if optional value has been supplied
		if(StartImageIndex===undefined)
		{
			StartImageIndex=0;
		}
		
		//set loop start index
		this.LoopStartImageIndex = StartImageIndex;
		
		//start image rotation
		me.RotateImages(StartImageIndex);
		
	}
	
	//stops rotation
	this.Stop = function()
	{
		clearTimeout(me.RotateTimeOutID);
		clearTimeout(me.FadeTimeOutID);
	}
	
	//rotates the images
	this.RotateImages = function(ImageIndex)
	{
		me.ImageIndex = ImageIndex;
		
		//count image loops
		if (me.ImageIndex == me.LoopStartImageIndex)
		{
			me.LoopCount += 1;
		}
		
		//check if we have passed max loop count
		if (me.MaxLoop != 0)
		{
			if (me.MaxLoop <= me.LoopCount)
			{
				me.Stop();
				return;
			}
		}
		
		//get image to rotate
		me.ImageElement.src = me.Images[me.ImageIndex];
		
		//check if fading
		if (me.FadeImages)
		{
			//rotate with fading
			me.FadeImage();
		}
		else
		{
			//rotate image without fading
			me.RotateImage(me.RotateSpeed);
		}
	}

	//rotates the image
	this.RotateImage = function(Speed)
	{
		//rotate the image
		if (me.ImageIndex == me.Images.length - 1)
		{
			me.RotateTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","RotateImages(0)")', Speed );
		}
		else
		{
			me.RotateTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","RotateImages(' + me.ImageIndex + ' + 1)")', Speed );
		}
		return;
	}
	
	//fades the images
	this.FadeImage = function()
	{

		var Speed = me.OpacityIncSpeed;
		
		//check if we need to rotate the image
		if (me.Opacity == 0)	
		{
			//set opacity to increase
			if (me.OpacityInc < 0)
			{
				me.OpacityInc *= -1;
			}
			
			//check if this is the fiart image to fade
			if (me.FadeTimeOutID == 0)
			{
				Speed = me.OpacityPause0;
			}
			else
			{
				me.Opacity += me.OpacityInc;
				me.RotateImage(me.OpacityPause0);
				return;
			}
		}
		
		if (me.Opacity == 100)	
		{
			//set opacity to decrease
			if (me.OpacityInc > 0)
			{
				me.OpacityInc *= -1;
			}
			Speed = me.OpacityPause100;
		}
		  
		//increment opacity
		me.Opacity += me.OpacityInc;
		me.FadeTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","SetOpacity()")', Speed );
		return;
	}

		
	//sets opacity
	this.SetOpacity = function()
	{
		
		//check minimum value
		if (me.Opacity < 0)
		{
			me.Opacity = 0;
		}
		
		//check maximum value
		if (me.Opacity > 100)
		{
			me.Opacity = 100;
		}
		
		// IE/Win
		me.ImageElement.style.filter = "alpha(opacity:" + me.Opacity + ")";
		
		// Safari<1.2, Konqueror
		me.ImageElement.style.KHTMLOpacity = me.Opacity/100;
		
		// Older Mozilla and Firefox
		me.ImageElement.style.MozOpacity = me.Opacity/100;
		
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		me.ImageElement.style.opacity = me.Opacity/100;
		
		//continue fade
		me.FadeImage();
	
	}

	//recursive function used to load all the images
	this.LoadImage = function(ImageIndex)
	{
		if (ImageIndex == me.Images.length)
		{
			me.ImagesLoaded = true;
			return;
		}
	   
	   me.CheckIfImageLoaded(ImageIndex);    
	}
	
	//recursive function to check if image has been uploaded
	//loads the next image in the array when completed
	this.CheckIfImageLoaded = function(ImageIndex)
	{
		var lImage = new Image();
		lImage.src = me.Images[ImageIndex]; 
		if (lImage.complete == true)
		{
			me.LoadImage(ImageIndex+1);
			return;
		}
		
	   setTimeout( 'IntervalHandler("' + me.ElementId + '","CheckIfImageLoaded(' + ImageIndex + ')")', 50 );
	}
	
}







