// ISF1.11 :: Image swap-fade 
// *****************************************************
// Adapted from DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object - assumes three image elements for fading in and out
var isfArr = new Array();
for (var i = 0; i < 3; i++) {
	isfArr[i] = new Object;
	isfArr[i] = { 'clock' : null, 'fade' : true, 'count' : 1 };
}
/*******************************************************/

//swapfade setup function
function swapfade()
{
var count = arguments[4];

	//if the timer is not already going
	if(isfArr[count].clock == null)
	{
		//copy the image object 
		isfArr[count].obj = arguments[0];	
		
		//copy the image src argument 
		isfArr[count].src = arguments[1];	
		
		//store the supported form of opacity
		if(typeof isfArr[count].obj.style.opacity != 'undefined')
		{
			isfArr[count].type = 'w3c';
		}
		else if(typeof isfArr[count].obj.style.MozOpacity != 'undefined')
		{
			isfArr[count].type = 'moz';
		}
		else if(typeof isfArr[count].obj.style.KhtmlOpacity != 'undefined')
		{
			isfArr[count].type = 'khtml';
		}
		else if(typeof isfArr[count].obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			isfArr[count].type = (isfArr[count].obj.filters.length > 0 && typeof isfArr[count].obj.filters.alpha == 'object' && typeof isfArr[count].obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isfArr[count].type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isfArr[count].obj.alt = arguments[3];		
		}
		
		//if any kind of opacity is supported
		if(isfArr[count].type != 'none')
		{

			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isfArr[count].length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isfArr[count].resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isfArr[count].clock = setInterval("swapfade_timer('"+count+"')", isfArr[count].length/isfArr[count].resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{	
			//just do the image swap
			isfArr[count].obj.src = isfArr[count].src;
		}
		
	}
};


//swapfade timer function
swapfade_timer = function()
{
var count = arguments[0];

	//increase or reduce the counter on an exponential scale
	isfArr[count].count = (isfArr[count].fade) ? isfArr[count].count * 0.9 : (isfArr[count].count * (1/0.9)); 
	
	//if the counter has reached the bottom
	if(isfArr[count].count < (1 / isfArr[count].resolution))
	{
		//clear the timer
		clearInterval(isfArr[count].clock);
		isfArr[count].clock = null;

		//do the image swap
		isfArr[count].obj.src = isfArr[count].src;

		//reverse the fade direction flag
		isfArr[count].fade = false;
		
		//restart the timer
		isfArr[count].clock = setInterval("swapfade_timer('"+count+"')", isfArr[count].length/isfArr[count].resolution);

	}
	
	//if the counter has reached the top
	if(isfArr[count].count > (1 - (1 / isfArr[count].resolution)))
	{
		//clear the timer
		clearInterval(isfArr[count].clock);
		isfArr[count].clock = null;

		//reset the fade direction flag
		isfArr[count].fade = true;
		
		//reset the counter
		isfArr[count].count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isfArr[count].type)
	{
		case 'ie' :
			isfArr[count].obj.filters.alpha.opacity = isfArr[count].count * 100;
			break;
			
		case 'khtml' :
			isfArr[count].obj.style.KhtmlOpacity = isfArr[count].count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isfArr[count].obj.style.MozOpacity = (isfArr[count].count == 1 ? 0.9999999 : isfArr[count].count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isfArr[count].obj.style.opacity = (isfArr[count].count == 1 ? 0.9999999 : isfArr[count].count);
	}
};


