// isf21.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isf2 = { 'clock' : null, 'fade' : true, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

isf2.imgs = [
	'common/featured_excursion_fishing_bottom.png',
	'common/featured_excursion_biking_bottom.png',
	'common/featured_excursion_camping_bottom.png',
	'common/featured_excursion_hunting_bottom.png'
];

/*****************************************************************************
*****************************************************************************/



//cache the images
isf2.imgsLen = isf2.imgs.length;
isf2.cache = [];
for(var i=0; i<isf2.imgsLen; i++)
{
	isf2.cache[i] = new Image;
	isf2.cache[i].src = isf2.imgs[i];
}


//swapfade2 setup function
function swapfade2()
{
	//if the timer is not already going
	if(isf2.clock == null)
	{
		//copy the image object 
		isf2.obj = arguments[0];
		
		//copy the image src argument 
		isf2.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof isf2.obj.style.opacity != 'undefined')
		{
			isf2.type = 'w3c';
		}
		else if(typeof isf2.obj.style.MozOpacity != 'undefined')
		{
			isf2.type = 'moz';
		}
		else if(typeof isf2.obj.style.KhtmlOpacity != 'undefined')
		{
			isf2.type = 'khtml';
		}
		else if(typeof isf2.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
			isf2.type = (isf2.obj.filters.length > 0 && typeof isf2.obj.filters.alpha == 'object' && typeof isf2.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf2.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isf2.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		// 
		if(isf2.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade2 is two distinct transitions
			isf2.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf2.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isf2.clock = setInterval('isf2.swapfade2()', isf2.length/isf2.resolution/1.5);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf2.obj.src = isf2.src;
			//alert(isf2.obj.style.backgroundImage);
		}
		
	}
};


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

		//do the image swap
		isf2.obj.src = isf2.src;
		
		//alert(isf2.obj.style.backgroundImage);
		
		//reverse the fade direction flag
		isf2.fade = false;
		
		//restart the timer
		isf2.clock = setInterval('isf2.swapfade2()', isf2.length/isf2.resolution/1.5);

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

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

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



