// IXF1.0 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var itw = { 'clock' : null, 'count' : 1 }
/*******************************************************/


//crosswipe setup function
function crosswipe2()
{
	//if the timer is not already going
	if(itw.clock == null)
	{
		//copy the image object 
		itw.obj = arguments[0];
		
		//get its dimensions
		itw.size = { 'w' : itw.obj.width, 'h' : itw.obj.height };
		
		//copy the image src argument 
		itw.src = arguments[1];
		
		//change the image alt text if defined
		if(typeof arguments[4] != 'undefined' && arguments[4] != '')
		{
			itw.obj.alt = arguments[4];
		}

		//if dynamic element creation is supported
		if(typeof document.createElementNS != 'undefined' || typeof document.createElement != 'undefined')
		{
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			itw.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
			itw.newimg.className = 'idupe';

			//set src to new image src
			itw.newimg.src = itw.src

			//move it to superimpose original image
			itw.newimg.style.left = itw.getRealPosition(itw.obj, 'x') + 'px';
			itw.newimg.style.top = itw.getRealPosition(itw.obj, 'y') + 'px';

			//set it to be completely hidden with clip
			itw.newimg.style.clip = 'rect(0, 0, 0, 0)';

			//show the image 
			itw.newimg.style.visibility = 'visible';

			//copy and convert fade duration argument 
			itw.length = parseInt(arguments[2], 10) * 1000;

			//create fade resolution argument as 20 steps per transition
			itw.resolution = parseInt(arguments[2], 10) * 20;

			//copy slide direction argument
			itw.dir = arguments[3];

			//start the timer
			itw.clock = setInterval('itw.crosswipe()', itw.length/itw.resolution);
		}
		
		//otherwise if dynamic element creation is not supported
		else
		{
			//just do the image swap
			itw.obj.src = itw.src;
		}
		
	}
};

//crosswipe timer function
itw.crosswipe = function()
{
	//decrease the counter on a linear scale
	itw.count -= (1 / itw.resolution);
	
	//if the counter has reached the bottom
	if(itw.count < (1 / itw.resolution))
	{
		//clear the timer
		clearInterval(itw.clock);
		itw.clock = null;
		
		//reset the counter
		itw.count = 1;
		
		//set the original image to the src of the new image
		itw.obj.src = itw.src;
	}
	
	//animate the clip of the new image
	//using the width and height properties we saved earlier
	itw.newimg.style.clip = 'rect('
		+ ( (/bt|bltr|brtl/.test(itw.dir)) ? (itw.size.h * itw.count) : (/che|cc/.test(itw.dir)) ? ((itw.size.h * itw.count) / 2) : (0) )
		+ 'px, '
		+ ( (/lr|tlbr|bltr/.test(itw.dir)) ? (itw.size.w - (itw.size.w * itw.count)) : (/cve|cc/.test(itw.dir)) ? (itw.size.w - ((itw.size.w * itw.count) / 2)) : (itw.size.w) )
		+ 'px, '
		+ ( (/tb|tlbr|trbl/.test(itw.dir)) ? (itw.size.h - (itw.size.h * itw.count)) : (/che|cc/.test(itw.dir)) ? (itw.size.h - ((itw.size.h * itw.count) / 2)) : (itw.size.h) )
		+ 'px, '
		+ ( (/lr|tlbr|bltr/.test(itw.dir)) ? (0) : (/tb|bt|che/.test(itw.dir)) ? (0) : (/cve|cc/.test(itw.dir)) ? ((itw.size.w * itw.count) / 2) : (itw.size.w * itw.count) ) 
		+ 'px)';
			
	//keep new image in position with original image
	//in case text size changes mid transition or something
	itw.newimg.style.left = itw.getRealPosition(itw.obj, 'x') + 'px';
	itw.newimg.style.top = itw.getRealPosition(itw.obj, 'y') + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	if(itw.count == 1)
	{
		//remove the duplicate image
		itw.newimg.parentNode.removeChild(itw.newimg);
	}
};



//get real position method
itw.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;
};
