// IXF1.11 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var ixf_array = new Array();
//*******************************************************/

//crossfade setup function
function crossfade()
{
	if(typeof arguments[4] != 'undefined' && arguments[4] != '')
	{
		var localIxf = ixf_array[arguments[4]];
		if(!localIxf)
		{
			localIxf = new ixf(null,1);
			ixf_array[arguments[4]] = localIxf;
		}
	
		//if the timer is not already going
		if(localIxf.clock == null)
		{
			//copy the image object 
			localIxf.obj = arguments[0];
			
			//copy the image src argument 
			localIxf.src = arguments[1];
			
			//store the supported form of opacity
			if(typeof localIxf.obj.style.opacity != 'undefined')
			{
				localIxf.type = 'w3c';
			}
			else if(typeof localIxf.obj.style.MozOpacity != 'undefined')
			{
				localIxf.type = 'moz';
			}
			else if(typeof localIxf.obj.style.KhtmlOpacity != 'undefined')
			{
				localIxf.type = 'khtml';
			}
			else if(typeof localIxf.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
				localIxf.type = (localIxf.obj.filters.length > 0 && typeof localIxf.obj.filters.alpha == 'object' && typeof localIxf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			}
			else
			{
				localIxf.type = 'none';
			}
			
			//change the image alt text if defined
			if(typeof arguments[3] != 'undefined' && arguments[3] != '')
			{
				localIxf.obj.alt = arguments[3];
			}
			
			//if any kind of opacity is supported
			if(localIxf.type != 'none')
			{
				//create a new image object and append it to body
				//detecting support for namespaced element creation, in case we're in the XML DOM
				localIxf.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
	
				//set positioning classname
				localIxf.newimg.className = 'idupe';
				
				//set src to new image src
				localIxf.newimg.src = localIxf.src
	
				//move it to superimpose original image
				localIxf.newimg.style.left = localIxf.getRealPosition(localIxf.obj, 'x') + 'px';
				localIxf.newimg.style.top = localIxf.getRealPosition(localIxf.obj, 'y') + 'px';
				
				//copy and convert fade duration argument 
				localIxf.length = parseInt(arguments[2], 10) * 1000;
				
				//create fade resolution argument as 20 steps per transition
				localIxf.resolution = parseInt(arguments[2], 10) * 20;
				
				//start the timer
				//localIxf.clock = setInterval('localIxf.crossfade()', localIxf.length/localIxf.resolution);
				localIxf.clock = setInterval('ixf_array[' + arguments[4] + '].crossfade()', localIxf.length/localIxf.resolution);				
			}			
			//otherwise if opacity is not supported
			else
			{
				//just do the image swap
				localIxf.obj.src = localIxf.src;
			}
		}
	}
}

//var ixf = { 'clock' : null, 'count' : 1 }
ixf = function(clock, count)
{
	this.clock = clock;
	this.count = count;	
}

ixf.prototype = {
	//crossfade timer function
	crossfade: function()
	{
		//decrease the counter on a linear scale
		this.count -= (1 / this.resolution);
						
		//if the counter has reached the bottom
		if(this.count < (1 / this.resolution))
		{
			//clear the timer
			clearInterval(this.clock);
			this.clock = null;
			
			//reset the counter
			this.count = 1;
			
			//set the original image to the src of the new image
			this.obj.src = this.src;
		}		
		
		//set new opacity value on both elements
		//using whatever method is supported
		switch(this.type)
		{
			case 'ie' :
				this.obj.filters.alpha.opacity = this.count * 100;
				this.newimg.filters.alpha.opacity = (1 - this.count) * 100;
				break;
				
			case 'khtml' :
				this.obj.style.KhtmlOpacity = this.count;
				this.newimg.style.KhtmlOpacity = (1 - this.count);
				break;
				
			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				this.obj.style.MozOpacity = (this.count == 1 ? 0.9999999 : this.count);
				this.newimg.style.MozOpacity = (1 - this.count);
				break;
				
			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				this.obj.style.opacity = (this.count == 1 ? 0.9999999 : this.count);
				this.newimg.style.opacity = (1 - this.count);
		}
				
		//now that we've gone through one fade iteration 
		//we can show the image that's fading in
		this.newimg.style.visibility = 'visible';
		
		//keep new image in position with original image
		//in case text size changes mid transition or something
		this.newimg.style.left = this.getRealPosition(this.obj, 'x') + 'px';
		this.newimg.style.top = this.getRealPosition(this.obj, 'y') + 'px';
		
		//if the counter is at the top, which is just after the timer has finished
		if(this.count == 1)
		{
			//remove the duplicate image
			this.newimg.parentNode.removeChild(this.newimg);
		}		
	},
	
	//get real position method
	getRealPosition: function()
	{
		this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
		this.tmp = arguments[0].offsetParent;
		while(this.tmp != null)
		{
			this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
			this.tmp = this.tmp.offsetParent;
		}
		
		return this.pos;
	}
}