// **********************************************************************
// IXF1.11 :: Image cross-fade                                          *
// **********************************************************************
// Base on DOM scripting by brothercake -- http://www.brothercake.com/  *
// **********************************************************************
// Source: http://javascript.about.com/library/blcyclet.htm             *
// **********************************************************************
// Modified by Nicola Boldrin                                           *
// **********************************************************************


//global object
var ixf = { 'clock' : null, 'count' : 1 }


//crossfade setup function
function crossfade() {
	
	//if the timer is not already going
	if(ixf.clock == null) {
		
		//image background
		ixf.inImg = arguments[0];
		
		//copy the image object 
		ixf.outImg = arguments[1];
		
		//copy the image src argument
		ixf.src = arguments[2];
		
		//store the supported form of opacity
		
		if(typeof ixf.outImg.style.opacity != 'undefined')
		{
			ixf.type = 'w3c';
		}
		else if(typeof ixf.outImg.style.MozOpacity != 'undefined')
		{
			ixf.type = 'moz';
		}
		else if(typeof ixf.outImg.style.KhtmlOpacity != 'undefined')
		{
			ixf.type = 'khtml';
		}
		else if(typeof ixf.outImg.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
			//ixf.type = (ixf.outImg.filters.length > 0 && typeof ixf.outImg.filters.alpha == 'object' && typeof ixf.outImg.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			ixf.type = (document.all) ? 'ie' : 'none';
		}
		

		//change the image alt text if defined
		//if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		//{
		//	ixf.outImg.alt = arguments[3];
		//}
		
		
		//if any kind of opacity is supported
		if(ixf.type != 'none')
		{
			
			//copy and convert fade duration argument
			ixf.length = parseInt(arguments[3], 10) * 500;

			//create fade resolution argument as 20 steps per transition
			ixf.resolution = parseInt(arguments[3], 10) * 40;
			
			//start the timer
			ixf.clock = setInterval('ixf.crossfade()', ixf.length/ixf.resolution);
			
		}
		
	}
};


//crossfade timer function
ixf.crossfade = function()
{
	//decrease the counter on a linear scale
	ixf.count -= (1 / ixf.resolution);
	
	//if the counter has reached the bottom
	if(ixf.count < (1 / ixf.resolution))
	{
		//clear the timer
		clearInterval(ixf.clock);
		ixf.clock = null;
		
		//reset the counter
		ixf.count = 1;
		
		//set the original image to the src of the new image
		var tmp = ixf.inImg.src;
		ixf.outImg.src = ixf.inImg.src;
		ixf.inImg.src = ixf.src;
	}
	
	//set new opacity value on both elements
	//using whatever method is supported
	switch(ixf.type)
	{
		case 'ie' :
			//ixf.outImg.filters.alpha.opacity = ixf.count * 100;
			//ixf.inImg.filters.alpha.opacity = (1 - ixf.count) * 100;
			
			ixf.outImg['filter'] = 'alpha(opacity='+(ixf.count * 100)+')';
			ixf.inImg['filter']  = 'alpha(opacity='+((1 - ixf.count) * 100)+')';
			break;
			
		case 'khtml' :
			ixf.outImg.style.KhtmlOpacity = ixf.count;
			ixf.inImg.style.KhtmlOpacity = (1 - ixf.count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.outImg.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.inImg.style.MozOpacity = (1 - ixf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.outImg.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.inImg.style.opacity = (1 - ixf.count);
	}
};

