/*******************************************************************************
 ISF1.11 :: Image swap-fade 
 ------------------------------------------------------------------------------
 Copyright (c) 2004 James Edwards (brothercake)          <cake@brothercake.com>
 BSD License                          See license.txt for licensing information
 Info/Docs       http://www.brothercake.com/site/resources/scripts/transitions/
 ------------------------------------------------------------------------------
*******************************************************************************/
//global object
var isf1 = { 'clock' : null, 'fade' : true, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

isf1.imgs = [
	'images/love/love_marches_on_1.jpg',
	'images/love/love_marches_on_2.jpg',
	'images/love/love_marches_on_3.jpg',
	'images/love/love_marches_on_4.jpg',
	'images/archaeology/archaeology_1.jpg',
	'images/archaeology/archaeology_2.jpg',
	'images/archaeology/archaeology_3.jpg',
	'images/archaeology/archaeology_4.jpg',
	'images/immersion/immersion_1.jpg',
	'images/immersion/immersion_2.jpg',
	'images/immersion/immersion_3.jpg',
	'images/immersion/immersion_4.jpg',
	'images/riverbank/riverbank_1.jpg',
	'images/riverbank/riverbank_2.jpg',
	'images/riverbank/riverbank_3.jpg',
	'images/riverbank/riverbank_4.jpg',
	'images/rothko/rothko_1.jpg',
	'images/rothko/rothko_2.jpg',
	'images/rothko/rothko_3.jpg',
	'images/rothko/rothko_4.jpg',
	'images/rothko2/rothko_1.jpg',
	'images/rothko2/rothko_2.jpg',
	'images/rothko2/rothko_3.jpg',
	'images/rothko2/rothko_4.jpg',
	'images/injection/injection_1.jpg',
	'images/injection/injection_2.jpg',
	'images/injection/injection_3.jpg',
	'images/injection/injection_4.jpg',
	'images/dawn/dawn_1.jpg',
	'images/dawn/dawn_2.jpg',
	'images/dawn/dawn_3.jpg',
	'images/dawn/dawn_4.jpg',
	'images/syncretism/syncretism_3.jpg',
	'images/dusk/dusk_1.jpg',
	'images/dusk/dusk_3.jpg',
	'images/dusk/dusk_4.jpg',
	'images/euler/euler_1.jpg',
	'images/euler/euler_2.jpg',
	'images/euler/euler_3.jpg',
	'images/euler/euler_4.jpg',
	'images/okeefe/flower_4.jpg',
	'images/okeefe/flower_51.jpg',
	'images/okeefe/flower_4n.jpg',
	'images/okeefe/ripple.jpg',
	'images/sketches/flower_17.jpg',
	'images/sketches/flower_20.jpg',
	'images/sketches/flower_26.jpg',
	'images/sketches/flower_42.jpg',
	'images/music/coltrane.jpg',
	'images/music/moons.jpg',
	'images/music/sabrina.jpg',
	'images/music/unity.jpg',
	'images/pendulum/pendulum_01.jpg',
	'images/pendulum/pendulum_03.jpg',
	'images/pendulum/pendulum_04.jpg',
	'images/pendulum/pendulum_15.jpg',
	'images/nature/nature_1.jpg',
	'images/nature/nature_2.jpg',
	'images/nature/nature_3.jpg',
	'images/nature/nature_4.jpg',
	'images/syncretism/syncretism_11.jpg',
	'images/syncretism/syncretism_21.jpg',
	'images/syncretism/syncretism_31.jpg',
	'images/syncretism/syncretism_41.jpg',
	'images/chameleon/stacks.jpg',
	'images/chameleon/funky.jpg',
	'images/chameleon/bees.jpg',
	'images/chameleon/chameleon.jpg',
	'images/photos/four_square.jpg',
	'images/photos/four_leaf_clover.jpg',
	'images/photos/temple.jpg',
	'images/photos/peace_and_archaeology.jpg'
	];

/*****************************************************************************
*****************************************************************************/



//cache the images
isf1.imgsLen = isf1.imgs.length;
isf1.cache = [];
for(var i=0; i<isf1.imgsLen; i++)
{
	isf1.cache[i] = new Image;
	isf1.cache[i].src = isf1.imgs[i];
}


//swapfade1 setup function
function swapfade1()
{
	//if the timer is not already going
	if(isf1.clock == null)
	{
		//copy the image object 
		isf1.obj = arguments[0];
		
		//copy the image src argument 
		isf1.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof isf1.obj.style.opacity != 'undefined')
		{
			isf1.type = 'w3c';
		}
		else if(typeof isf1.obj.style.MozOpacity != 'undefined')
		{
			isf1.type = 'moz';
		}
		else if(typeof isf1.obj.style.KhtmlOpacity != 'undefined')
		{
			isf1.type = 'khtml';
		}
		else if(typeof isf1.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
			isf1.type = (isf1.obj.filters.length > 0 && typeof isf1.obj.filters.alpha == 'object' && typeof isf1.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf1.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isf1.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(isf1.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade1 is two distinct transitions
			isf1.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf1.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isf1.clock = setInterval('isf1.swapfade1()', isf1.length/isf1.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf1.obj.src = isf1.src;
		}
		
	}
};


//swapfade1 timer function
isf1.swapfade1 = function()
{
	//increase or reduce the counter on an exponential scale
	isf1.count = (isf1.fade) ? isf1.count * 0.9 : (isf1.count * (1/0.9)); 
	
	//if the counter has reached the bottom
	if(isf1.count < (1 / isf1.resolution))
	{
		//clear the timer
		clearInterval(isf1.clock);
		isf1.clock = null;

		//do the image swap
		isf1.obj.src = isf1.src;

		//reverse the fade direction flag
		isf1.fade = false;
		
		//restart the timer
		isf1.clock = setInterval('isf1.swapfade1()', isf1.length/isf1.resolution);

	}
	
	//if the counter has reached the top
	if(isf1.count > (1 - (1 / isf1.resolution)))
	{
		//clear the timer
		clearInterval(isf1.clock);
		isf1.clock = null;

		//reset the fade direction flag
		isf1.fade = true;
		
		//reset the counter
		isf1.count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isf3.type)
	{
		case 'ie' :
			isf1.obj.filters.alpha.opacity = isf1.count * 100;
			break;
			
		case 'khtml' :
			isf1.obj.style.KhtmlOpacity = isf1.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf1.obj.style.MozOpacity = (isf1.count == 1 ? 0.9999999 : isf1.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf1.obj.style.opacity = (isf1.count == 1 ? 0.9999999 : isf1.count);
	}
};



