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


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

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

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

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

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

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



