//*************************************************************************************
// RotateFade is used to fade an array of images in and out while they are rotated	  *
// RotateFade was developed by Richard Baldwin for http://www.knowitall.org			  *
// Image fading based on script provided by brothercake -- http://www.brothercake.com/*
//*************************************************************************************

//global object and variables... isf is the image object and Links is the hyperlink object
//count is used to increment and decrement the opacity of the image
//imageNum is the index for the array of images in the loop
//fadeRatio sets how fast each image fades in and out... 1 is very quick and 100 is very slow
//inout is a status flag for what the image is doing... can be in, out, pause, back, or next
//timeout is a timer variable used for the delay that will fire after the delay value unless it is cleared
//delay is the time in milliseconds that the image pauses before it fades out
//start is the start flag that is set whenever startRotation is called
var isf = {'count' : 1, 'imageNum' : 0, 'fadeRatio' : 7, 'inout' : 'delay', 'timeOut' : null, 'delay' : 5500, 'start' : false}
var links = {}

//List the images that need to be cached and alt text for images and the hyperlinks
isf.imgs = ['rotatingimages/picture1.jpg','rotatingimages/picture2.jpg','rotatingimages/picture3.jpg','rotatingimages/picture4.jpg','rotatingimages/picture5.jpg','rotatingimages/picture6.jpg'];isf.altText = ['NEW Video.','NEW Video.','NEW Video.','NEW Video.','NEW Video.','NEW Video.'];links.location = ['#','#','#','#','#','#'];//cache the images and load them into an array
isf.imgsLen = isf.imgs.length;
isf.cache = [];
for(var i = 0; i < isf.imgsLen; i++)
{
	isf.cache[i] = new Image;
	isf.cache[i].src = isf.imgs[i];
};

function Pause()
{
	//clear the timer so isf.timeOut doesn't fire, also set inout to pause
	clearTimeout(isf.timeOut);
	isf.inout = 'pause';
};

function Restart()
{
	//clear the timer so isf.timeOut doesn't fire, also set inout to in
	clearTimeout(isf.timeOut);
	isf.inout = 'in';
};

function Back()
{
	//clear the timer so isf.timeOut doesn't fire, also set inout to back
	clearTimeout(isf.timeOut);
	isf.inout = 'back';
	
	//if during initial delay clear it and start rotation
	if (isf.start == false)
	{
		clearTimeout(initialDelay);
		isf.start = true;
		startRotation();		
	}		
};

function Next()
{
	//clear the timer so isf.timeOut doesn't fire, also set inout to next
	clearTimeout(isf.timeOut);
	isf.inout = 'next';
	
	//if during initial delay clear it and start rotation
	if (isf.start == false)
	{
		clearTimeout(initialDelay);		
		isf.start = true;
		startRotation();
	}		
};

function resetFade()
{
	//fires unless isf.timeOut is cleared, this ends the delay and starts the fade out
	isf.inout = 'out';		
};

function setupFade()
{
	//store the supported form of opacity
	if(typeof isf.obj.style.opacity != 'undefined')
	{
		isf.type = 'w3c';
	}
	else if(typeof isf.obj.style.MozOpacity != 'undefined')
	{
		isf.type = 'moz';
	}
	else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
	{
		isf.type = 'khtml';
	}
	else if(typeof isf.obj.filters == 'object')
	{
		isf.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
	}
	else
	{
		isf.type = 'none';
	}
};

function rotateFeatures()
{
	//function called from onload that sets the objects and starts rotation after delay
	isf.obj = arguments[0];
	links.obj = arguments[1];
	initialDelay = setTimeout("startRotation()", isf.delay);
};

function startRotation()
{
	//if initial call set start to true and fade to out
	if (isf.start == false)
	{
		isf.start = true;
		isf.inout = 'out';			
	}		
	
	//sets up the opacity type and starts fade, which executes every 50 milliseconds
	setupFade();
	setInterval("fadeImage()", 50);	
};

function fadeImage()
{	
	//if opacity change will work... check inout status then run the fade 
	if(isf.type != 'none')
	{			
		if (isf.inout == 'out') //if set to out then fade image out
		{
			//reduce the counter on an exponential scale
			isf.count = isf.count * 0.9;
			
			//if the image has completely faded
			if(isf.count < (1 / isf.fadeRatio))
			{
				//increment the image... if end of array then loop to beginning
				isf.imageNum = isf.imageNum + 1;
	
				if (isf.imageNum > (isf.imgsLen - 1)) 
				{
					isf.imageNum = 0;
				}
				
				//swap the image with the next in line, change alt text, change the hyperlink, and set fade to in
				isf.obj.src = isf.cache[isf.imageNum].src;	
				isf.obj.alt = isf.altText[isf.imageNum];
				links.obj.href = links.location[isf.imageNum];
				isf.inout = 'in';
			}
		}
		else if (isf.inout == 'in') //if set to in then fade image in
		{
			//increase the counter on an exponential scale
			isf.count = isf.count * (1/0.9);
			
			//if the image has faded all the way up
			if(isf.count > (1 - (1 / isf.fadeRatio)))
			{
				//reset count to 1, set fade to pause, set timer to end delay
				isf.count = 1;
				isf.inout = 'pause';
				isf.timeOut = setTimeout("resetFade()", isf.delay);
			}
		}
		else if (isf.inout == 'back') //if set to back then jump back
		{
			//decrement the image... if at beginning loop to the end
			isf.imageNum = isf.imageNum - 1;
						
			if (isf.imageNum < 0) 
			{
				isf.imageNum = isf.imgsLen - 1;
			}			
			
			//reset count to 1, swap the image with previous, change alt text, change the hyperlink, set fade to in
			isf.count = 1;
			isf.obj.src = isf.cache[isf.imageNum].src;	
			isf.obj.alt = isf.altText[isf.imageNum];
			links.obj.href = links.location[isf.imageNum];
			isf.inout = 'in';
		}
		else if (isf.inout == 'next') //if set to next then jump forward
		{
			//increment the image... if end of array then loop to beginning
			isf.imageNum = isf.imageNum + 1;
						
			if (isf.imageNum > (isf.imgsLen - 1)) 
			{
				isf.imageNum = 0;
			}
			
			//reset count to 1, swap the image with the next in line, change alt text, change the hyperlink, set fade to in
			isf.count = 1;
			isf.obj.src = isf.cache[isf.imageNum].src;	
			isf.obj.alt = isf.altText[isf.imageNum];
			links.obj.href = links.location[isf.imageNum];
			isf.inout = 'in';
		}	
		else if (isf.inout == 'pause') //if set to pause make sure opacity is full and wait
		{
			//reset count to 1, which is full opacity
			isf.count = 1;
		}
		//fade the image based on browser type detected in setupFade()
		switch(isf.type)
		{
			case 'ie' :
				isf.obj.filters.alpha.opacity = isf.count * 100;
				break;
				
			case 'khtml' :
				isf.obj.style.KhtmlOpacity = isf.count;
				break;
				
			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
				break;
				
			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
		}
	}
};

