// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// Minor tweaks for annesstudio.com by Brad Marshall at
// Analog Daydream -- http://www.analog-daydream.com/
//******************************************************
//global object
var isf = { 'clock' : null, 'fade' : true, 'count' : 1 }
/*******************************************************



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

isf.imgs = [
   //commissioned
   '../images/gallery/large/large-madison.jpg',
   '../images/gallery/large/large-maranda.jpg',
   '../images/gallery/large/large-molly.jpg',
   '../images/gallery/large/large-lisas-house.jpg',
   '../images/gallery/large/large-messy.jpg',
   '../images/gallery/large/large-nancys-dad.jpg',
   '../images/gallery/large/large-pb050037.jpg',
   '../images/gallery/large/large-pc210066.jpg',
   '../images/gallery/large/large-img206.jpg', 
   '../images/gallery/large/large-img001.jpg',  
   '../images/gallery/large/large-allens.jpg',   
   '../images/gallery/large/large-mural-1.jpg',
   '../images/gallery/large/large-mural-2.jpg',
   '../images/gallery/large/large-mural-3.jpg',
   '../images/gallery/large/large-mural-4.jpg',
   '../images/gallery/large/large-mural-6.jpg',
   '../images/gallery/large/large-jen.jpg',
   '../images/gallery/large/large-tony-kate.jpg',   
   '../images/gallery/large/large-brenda.jpg',
   '../images/gallery/large/large-img_2567.jpg',
   '../images/gallery/large/large-img_3335.jpg',
   '../images/gallery/large/large-img_3394.jpg',
   '../images/gallery/large/large-lacee.jpg',
   '../images/gallery/large/large-sin-sean.jpg',      
                     
   //original artwork
   '../images/gallery/large/large-copter-plane.jpg',
   '../images/gallery/large/large-cupcake.jpg',
   '../images/gallery/large/large-explore.jpg',
   '../images/gallery/large/large-first-snow.jpg',
   '../images/gallery/large/large-footprints.jpg',
   '../images/gallery/large/large-kylie.jpg',
   '../images/gallery/large/large-lydia.jpg',
   '../images/gallery/large/large-pasture.jpg',
   '../images/gallery/large/large-sandbox.jpg',
   '../images/gallery/large/large-the-look.jpg',
   '../images/gallery/large/large-untitled.jpg',
   '../images/gallery/large/large-walk-away.jpg',
   '../images/gallery/large/large-polar-bear.jpg',
   '../images/gallery/large/large-table-for-two.jpg',
   '../images/gallery/large/large-windmill.jpg',
   '../images/gallery/large/large-skates.jpg',
   '../images/gallery/large/large-sea-urchin.jpg',
   '../images/gallery/large/large-age-and-wisdom.jpg',
   '../images/gallery/large/large-stonehenge.jpg',
   '../images/gallery/large/large-prefontaine.jpg',
   '../images/gallery/large/large-zenith.jpg',    
   '../images/gallery/large/large-thinking-process.jpg',
   '../images/gallery/large/large-assume.jpg',
   '../images/gallery/large/large-exageration-study.jpg',
   '../images/gallery/large/large-the-hole.jpg',
   '../images/gallery/large/large-instincts.jpg', 
   '../images/gallery/large/large-teddybear.jpg',
   '../images/gallery/large/large-img316.jpg',
   '../images/gallery/large/large-charity.jpg',
   '../images/gallery/large/large-boy-and-duck.jpg',
   '../images/gallery/large/large-m_comp_1.jpg', 
   '../images/gallery/large/large-m_comp_2.jpg',
   '../images/gallery/large/large-pounce_1.jpg',
   '../images/gallery/large/large-pounce_2.jpg',
   '../images/gallery/large/large-pounce_3.jpg',
   '../images/gallery/large/large-untitled_2.jpg',
   '../images/gallery/large/large-water-study_1.jpg',
   '../images/gallery/large/large-water-study_2.jpg',   
   '../images/gallery/large/large-joseph.jpg',
   '../images/gallery/large/large-boots.jpg',      
             
   //students
   '../images/gallery/large/large-student1.jpg',
   '../images/gallery/large/large-student2.jpg',
   '../images/gallery/large/large-student3.jpg',
   '../images/gallery/large/large-student4.jpg',
   '../images/gallery/large/large-student5.jpg',
   '../images/gallery/large/large-student6.jpg',
   '../images/gallery/large/large-student7.jpg',
   '../images/gallery/large/large-student8.jpg',
   '../images/gallery/large/large-student9.jpg',
   '../images/gallery/large/large-student10.jpg',
   '../images/gallery/large/large-student11.jpg',
   '../images/gallery/large/large-student12.jpg',
   '../images/gallery/large/large-student13.jpg',
   '../images/gallery/large/large-student14.jpg',
   '../images/gallery/large/large-student15.jpg',
   '../images/gallery/large/large-student16.jpg',
   '../images/gallery/large/large-student17.jpg',
   '../images/gallery/large/large-student18.jpg',
   '../images/gallery/large/large-student19.jpg',
   '../images/gallery/large/large-student20.jpg',
   '../images/gallery/large/large-student21.jpg',
   '../images/gallery/large/large-student22.jpg', 
   '../images/gallery/large/large-david.jpg',
   '../images/gallery/large/large-student24.jpg',
   '../images/gallery/large/large-student25.jpg',
   '../images/gallery/large/large-student26.jpg',
   '../images/gallery/large/large-student27.jpg',
   '../images/gallery/large/large-student28.jpg',
   '../images/gallery/large/large-student29.jpg', 
   '../images/gallery/large/large-student30.jpg',             
     
	];

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

//this function was added by Brad at Analog Daydream. Automatically scrolls
//the window back up to the large gallery image when a thumbnail is clicked.
function scrollWindow() {
   window.scroll(0, 350);
}

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


//swapfade setup function
function swapfade()
{
	//if the timer is not already going
	if(isf.clock == null)
	{
		//copy the image object 
		isf.obj = arguments[0];
		
		//copy the image src argument 
		isf.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof isf.obj.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
		}
		else if(typeof isf.obj.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
		}
		else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
		}
		else if(typeof isf.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
			isf.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isf.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(isf.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions

         //Brad at Analog Daydream changed this multiplier
         //from 500 to 250 to help speed up the animation.
			isf.length = parseInt(arguments[2], 10) * 250;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf.obj.src = isf.src;
		}
		
	}
};


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

		//do the image swap
		isf.obj.src = isf.src;

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

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

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

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


