//global object
var tIxf = { 'clock' : null, 'count' : 1, 'finClock' : null }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

tIxf.imgs = [];

/*****************************************************************************
*****************************************************************************/



//cache the images
tIxf.imgsLen = tIxf.imgs.length;
tIxf.cache = [];
for(var i=0; i<tIxf.imgsLen; i++)
{
	tIxf.cache[i] = new Image;
	tIxf.cache[i].src = tIxf.imgs[i];
}


//crossfade setup function
function tinyCrossfade()
{
	//if the timer is not already going
	if(tIxf.clock == null)
	{
		//copy the image object 
		tIxf.obj = arguments[0];
		
		//copy the image src argument 
		tIxf.src = arguments[2];
		
		if( tIxf.obj.src == tIxf.src )
			return;
		
		//store the supported form of opacity
		if(typeof tIxf.obj.style.opacity != 'undefined')
		{
			tIxf.type = 'w3c';
		}
		else if(typeof tIxf.obj.style.MozOpacity != 'undefined')
		{
			tIxf.type = 'moz';
		}
		else if(typeof tIxf.obj.style.KhtmlOpacity != 'undefined')
		{
			tIxf.type = 'khtml';
		}
		else if(typeof tIxf.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
			tIxf.type = (tIxf.obj.filters.length > 0 && typeof tIxf.obj.filters.alpha == 'object' && typeof tIxf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			tIxf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[4] != 'undefined' && arguments[4] != '')
		{
			tIxf.obj.alt = arguments[4];
		}
		
		//if any kind of opacity is supported
		if(tIxf.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
			//tIxf.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			//var objParent = (tIxf.obj.parentElement ? tIxf.obj.parentElement : tIxf.obj.parentNode );
			//tIxf.newimg = objParent.appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			tIxf.newimg = arguments[1];

			//set positioning classname
			tIxf.newimg.className = 'idupe';
			
			//set src to new image src
			tIxf.newimg.src = tIxf.src

			//move it to superimpose original image
			//tIxf.newimg.style.left = tIxf.getRealPosition(tIxf.obj, 'x') + 'px';
			//tIxf.newimg.style.top = tIxf.getRealPosition(tIxf.obj, 'y') + 'px';
			
			//copy and convert fade duration argument 
			tIxf.length = parseInt(arguments[3], 10) * 1000;
			
			//create fade resolution argument as 20 steps per transition
			//alert(tIxf.type);
			if( tIxf.type == 'w3c' )
				tIxf.resolution = parseInt(arguments[3], 10) * 10;
			else
				tIxf.resolution = parseInt(arguments[3], 10) * 20;
			
			//start the timer
			tIxf.clock = setInterval('tIxf.crossfade()', tIxf.length/tIxf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			tIxf.obj.src = tIxf.src;
		}
		
	}
};


//crossfade timer function
tIxf.crossfade = function()
{
	//decrease the counter on a linear scale
	tIxf.count -= (1 / tIxf.resolution);
	
	//if the counter has reached the bottom
	if(tIxf.count < (1 / tIxf.resolution))
	{
		//clear the timer
		clearInterval(tIxf.clock);
		tIxf.clock = null;
		
		//reset the counter
		tIxf.count = 0;
		
		//set the original image to the src of the new image
		tIxf.obj.style.visibility = 'hidden';
		tIxf.obj.src = tIxf.src;	
		
		tIxf.finClock = setInterval('tIxf.finalize()', 50);								
	}
	
	tIxf.setOpacity(tIxf.count);
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	tIxf.newimg.style.visibility = 'visible';
	
	//keep new image in position with original image
	//in case text size changes mid transition or something
	//tIxf.newimg.style.left = tIxf.getRealPosition(tIxf.obj, 'x') + 'px';
	//tIxf.newimg.style.top = tIxf.getRealPosition(tIxf.obj, 'y') + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	/*if(tIxf.count == 1)
	{
		//remove the duplicate image
		//tIxf.newimg.parentNode.removeChild(tIxf.newimg);
		tIxf.newimg.style.visibility = 'hidden';
	}*/
};

tIxf.setOpacity = function(countVal) {
	//set new opacity value on both elements
	//using whatever method is supported
	switch(tIxf.type)
	{
		case 'ie' :
			tIxf.obj.filters.alpha.opacity = countVal * 100;
			tIxf.newimg.filters.alpha.opacity = (1 - countVal) * 100;
			break;
			
		case 'khtml' :
			tIxf.obj.style.KhtmlOpacity = tIxf.count;
			tIxf.newimg.style.KhtmlOpacity = (1 - countVal);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			tIxf.obj.style.MozOpacity = (countVal == 1 ? 0.9999999 : countVal);
			tIxf.newimg.style.MozOpacity = (1 - countVal);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			tIxf.obj.style.opacity = (countVal == 1 ? 0.9999999 : countVal);
			tIxf.newimg.style.opacity = (1 - countVal);
	}	
}

tIxf.finalize = function() {
	//alert('finalize: ' + tIxf.obj.readyState);
	if( tIxf.obj.readyState==null || tIxf.obj.readyState=='complete' ) {					
		clearInterval(tIxf.finClock);
		tIxf.finClock = null;
		tIxf.count = 1;
		tIxf.setOpacity(1);
		tIxf.newimg.style.visibility = 'hidden';	
		tIxf.obj.style.visibility = 'visible';
	}		
}

//get real position method
tIxf.getRealPosition = function()
{
	return 0;
	
	//debugger;
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop + arguments[0].offsetHeight;
	/*this.tmp = arguments[0].offsetParent;
	while(this.tmp != null && this.tmp.style.position!='relative' && this.tmp.style.position!='absolute')
	{
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}*/
	
	return (arguments[1] == 'x') ? this.pos : -(this.pos);
};
