// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isfa = { 'clock' : null, 'fade' : true, 'count' : 1 }
//swapfade setup function
function txtswapfade()
{
	//if the timer is not already going
	if(isfa.clock == null)
	{
		//copy the image object 
		isfa.obj = arguments[0];
		
		//copy the image src argument 
		isfa.txt = arguments[1];
		
		//store the supported form of opacity
		if(typeof isfa.obj.style.opacity != 'undefined')
		{
			isfa.type = 'w3c';
		}
		else if(typeof isfa.obj.style.MozOpacity != 'undefined')
		{
			isfa.type = 'moz';
		}
		else if(typeof isfa.obj.style.KhtmlOpacity != 'undefined')
		{
			isfa.type = 'khtml';
		}
		else if(typeof isfa.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
			isfa.type = (isfa.obj.filters.length > 0 && typeof isfa.obj.filters.alpha == 'object' && typeof isfa.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isfa.type = 'none';
		}
		
		
		
		//if any kind of opacity is supported
		if(isfa.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isfa.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isfa.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isfa.clock = setInterval('isfa.txtswapfade()', isfa.length/isfa.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isfa.obj.innerHTML = isfa.txt;
		}
		
	}
};


//swapfade timer function
isfa.txtswapfade = function()
{
	//increase or reduce the counter on an exponential scale
	isfa.count = (isfa.fade) ? isfa.count * 0.9 : (isfa.count * (1/0.9)); 
	
	//if the counter has reached the bottom
	if(isfa.count < (1 / isfa.resolution))
	{
		//clear the timer
		clearInterval(isfa.clock);
		isfa.clock = null;

		//do the image swap
		isfa.obj.innerHTML = isfa.txt;

		//reverse the fade direction flag
		isfa.fade = false;
		
		//restart the timer
		isfa.clock = setInterval('isfa.txtswapfade()', isfa.length/isfa.resolution);

	}
	
	//if the counter has reached the top
	if(isfa.count > (1 - (1 / isfa.resolution)))
	{
		//clear the timer
		clearInterval(isfa.clock);
		isfa.clock = null;

		//reset the fade direction flag
		isfa.fade = true;
		
		//reset the counter
		isfa.count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isfa.type)
	{
		case 'ie' :
			isfa.obj.filters.alpha.opacity = isfa.count * 100;
			break;
			
		case 'khtml' :
			isfa.obj.style.KhtmlOpacity = isfa.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isfa.obj.style.MozOpacity = (isfa.count == 1 ? 0.9999999 : isfa.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isfa.obj.style.opacity = (isfa.count == 1 ? 0.9999999 : isfa.count);
	}
};



