// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object

function ISF()
{
	this.isf = { 'clock' : null, 'fade' : true, 'count' : 1 };
}

//swapfade setup function
ISF.prototype.swapfade = function ()
{
	//if the timer is not already going
	if(this.isf.clock == null)
	{
		//copy the image object 
		this.isf.obj = arguments[0];
		
		//copy the image src argument 
		this.isf.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof this.isf.obj.style.opacity != 'undefined')
		{
			this.isf.type = 'w3c';
		}
		else if(typeof this.isf.obj.style.MozOpacity != 'undefined')
		{
			this.isf.type = 'moz';
		}
		else if(typeof this.isf.obj.style.KhtmlOpacity != 'undefined')
		{
			this.isf.type = 'khtml';
		}
		else if(typeof this.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
			this.isf.type = (this.isf.obj.filters.length > 0 && typeof this.isf.obj.filters.alpha == 'object' && typeof this.isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			this.isf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			this.isf.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(this.isf.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			this.isf.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			this.isf.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			//debugger;
			//alert(arguments[4]);
			var obj = arguments[4];
			this.isf.clock = setInterval(function(){obj.swapfadetimer(obj);}, this.isf.length/this.isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			this.isf.obj.src = this.isf.src;
		}
		
	}
}

ISF.prototype.swapfadetimer = function(obj)
{
	{
		//increase or reduce the counter on an exponential scale
		this.isf.count = (this.isf.fade) ? this.isf.count * 0.9 : (this.isf.count * (1/0.9)); 
		
		//if the counter has reached the bottom
		if(this.isf.count < (1 / this.isf.resolution))
		{
			//clear the timer
			clearInterval(this.isf.clock);
			this.isf.clock = null;
	
			//do the image swap
			this.isf.obj.src = this.isf.src;
	
			//reverse the fade direction flag
			this.isf.fade = false;
			
			//restart the timer
			this.isf.clock = setInterval(function(){obj.swapfadetimer(obj);}, this.isf.length/this.isf.resolution);
	
		}
		
		//if the counter has reached the top
		if(this.isf.count > (1 - (1 / this.isf.resolution)))
		{
			//clear the timer
			clearInterval(this.isf.clock);
			this.isf.clock = null;
	
			//reset the fade direction flag
			this.isf.fade = true;
			
			//reset the counter
			this.isf.count = 1;
		}
	
		//set new opacity value on element
		//using whatever method is supported
		switch(this.isf.type)
		{
			case 'ie' :
				this.isf.obj.filters.alpha.opacity = this.isf.count * 100;
				break;
				
			case 'khtml' :
				this.isf.obj.style.KhtmlOpacity = this.isf.count;
				break;
				
			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				this.isf.obj.style.MozOpacity = (this.isf.count == 1 ? 0.9999999 : this.isf.count);
				break;
				
			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				this.isf.obj.style.opacity = (this.isf.count == 1 ? 0.9999999 : this.isf.count);
		}
	}
}
