// JavaScript Document
function RotateHomeImages()
{
	var lRotator = new Rotator();
	lRotator.Images = new Array("images/home/home-panel1.gif","images/home/home-panel2.gif","images/home/home-panel3.gif");
	lRotator.ElementId = "rotatinghomeimage";
	lRotator.RotateSpeed = 6700;
	lRotator.Start();
}

function RotateTestimonials()
{
	var lRotator = new Rotator();
	lRotator.Images = new Array("images/common/CE-testimonials1.gif","images/common/CE-testimonials2.gif","images/common/CE-testimonials3.gif", "images/common/CE-testimonials4.gif","images/common/CE-testimonials5.gif","images/common/CE-testimonials6.gif",
"images/common/CE-testimonials7.gif","images/common/CE-testimonials8.gif","images/common/CE-testimonials9.gif",
"images/common/CE-testimonials10.gif","images/common/CE-testimonials11.gif","images/common/CE-testimonials12.gif",
"images/common/CE-testimonials13.gif");
	lRotator.ElementId = "rotatetestimonial";
	lRotator.RotateSpeed = 6700;
	
	//get random number between 0 and 12
	var ImageIndex = Math.floor(Math.random()*13);
	lRotator.Start(ImageIndex);
}

//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];
	eval( "scope." + strFunc );
}

//this class rotates an set on images
function Rotator() 
{
	//variables
	var me = this;
	this.Opacity = 0;
	this.OpacityInc = 2;
	this.StartOpacityInc = 2;
	this.FadeImages = true;	//if true the images will fade in and out
	this.ImagesLoaded = false;	//true when all the images are loaded
	this.Images = new Array();
	this.ElementId = '';
	this.ImageElement = new Image;
	this.RotateSpeed = 5000; //the speed in which images are rotated in milliseconds
	this.FadeSpeed = 50; //the speed in which images are faded in milliseconds
	this.FadePause = 250; //the length in milliseconds that the fade is paused while the image is 100 opacity
	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
	
	//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 thsi 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);
		
		//set up image fade
		if (me.FadeImages)
		{
			me.FadeImage();
		}
	}
	
	//stops rotation
	this.Stop = function()
	{
		clearTimeout(me.RotateTimeOutID);
		clearTimeout(me.FadeTimeOutID);
	}
	
	//rotates the images
	this.RotateImages = function(ImageIndex)
	{
		//count image loops
		if (ImageIndex == this.LoopStartImageIndex)
		{
			this.LoopCount += 1;
		}
		
		//check if we have passed max loop count
		if (this.MaxLoop != 0)
		{
			if (this.MaxLoop <= this.LoopCount)
			{
				this.Stop();
				return;
			}
		}
		
		//get image to rotate
		me.ImageElement.src = me.Images[ImageIndex];
		me.Opacity = 0;
		me.OpacityInc = me.StartOpacityInc;
		if (ImageIndex == me.Images.length - 1)
		{
			me.RotateTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","RotateImages(0)")', me.RotateSpeed );
		}
		else
		{
			me.RotateTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","RotateImages(' + ImageIndex + ' + 1)")', me.RotateSpeed );
		}
	}
	
	//fades the images
	this.FadeImage = function()
	{
		  // 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;
		  
		  me.Opacity += me.OpacityInc;
		  
		  if (me.Opacity == 100)
		  {
			me.OpacityInc *= -1;	
			me.FadeTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","FadeImage()")', this.FadePause );
			return;
		  }
			  
		  me.FadeTimeOutID = setTimeout( 'IntervalHandler("' + me.ElementId + '","FadeImage()")', me.FadeSpeed );
	}
	
	//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 );
	}
	
}





