// IXF1.11 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object

var i=0;

function du(t)
 {
 document.getElementById("t"+t).innerHTML="here!"+i;
 i++;
 }

var ixfs = new Array;
var ixf = new Object;

/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

/*
ixf.imgs = [
	'buttons/udm4-whitebutton88x31.gif',
	'buttons/udm4-greenbutton88x31.gif',
	'buttons/udm4-purplebutton88x31.gif'
	];

ixf.imgsLen = ixf.imgs.length;
ixf.cache = [];
for(var i=0; i<ixf.imgsLen; i++)
{
	ixf.cache[i] = new Image;
	ixf.cache[i].src = ixf.imgs[i];
}
*/

//crossfade setup function
function crossfade()
{

//document.getElementById(arguments[0]).setAttribute("fadeStatus",2);

 index=document.getElementById(arguments[0]).getAttribute("fadeId");
 ixfs[index] = { 'clock' : null, 'count' : 1 }

	//if the timer is not already going
	if(ixfs[index].clock == null)
	{
		//copy the image object 
		ixfs[index].obj = document.getElementById(arguments[0]);
		
		//copy the image src argument 
		ixfs[index].src = arguments[1];
		
		//store the supported form of opacity
		if(typeof ixfs[index].obj.style.opacity != 'undefined')
		{
			ixfs[index].type = 'w3c';
		}
		else if(typeof ixfs[index].obj.style.MozOpacity != 'undefined')
		{
			ixfs[index].type = 'moz';
		}
		else if(typeof ixfs[index].obj.style.KhtmlOpacity != 'undefined')
		{
			ixfs[index].type = 'khtml';
		}
		else if(typeof ixfs[index].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
			ixfs[index].type = (ixfs[index].obj.filters.length > 0 && typeof ixfs[index].obj.filters.alpha == 'object' && typeof ixfs[index].obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			ixfs[index].type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			ixfs[index].obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(ixfs[index].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
			ixfs[index].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
			ixfs[index].newimg.className = 'idupe';
			
			//set src to new image src
			ixfs[index].newimg.src = ixfs[index].src

			//move it to superimpose original image
			ixfs[index].newimg.style.left = ixf.getRealPosition(ixfs[index].obj, 'x') + 'px';
			ixfs[index].newimg.style.top = ixf.getRealPosition(ixfs[index].obj, 'y') + 'px';
			
			//copy and convert fade duration argument 
			ixfs[index].length = parseInt(arguments[2], 10) * 1000;
			
			//create fade resolution argument as 20 steps per transition
			ixfs[index].resolution = parseInt(arguments[2], 10) * 20;
			
			//start the timer
			ixfs[index].clock = setInterval("ixf.crossfade('"+index+"')", ixfs[index].length/ixfs[index].resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
		ixfs[index].obj.src = ixfs[index].src;
		}
		
	}


//document.getElementById(arguments[0]).setAttribute("fadeStatus",1);

};

function wait()
{
}

//crossfade timer function
ixf.crossfade = function(index)
{

	window.status=index;
	//decrease the counter on a linear scale
	ixfs[index].count -= (1 / ixfs[index].resolution);
	
	//if the counter has reached the bottom
	if(ixfs[index].count < (1 / ixfs[index].resolution))
	{
		//clear the timer
		clearInterval(ixfs[index].clock);
		ixfs[index].clock = null;
		
		//reset the counter
		ixfs[index].count = 1;
		
		//set the original image to the src of the new image
		ixfs[index].obj.src = ixfs[index].src;

		clearTimeout(t);

//		ixfs[index].obj.setAttribute("fadeStatus",1);
	
	}
	
	//set new opacity value on both elements
	//using whatever method is supported
	switch(ixfs[index].type)
	{
		case 'ie' :
			ixfs[index].obj.filters.alpha.opacity = ixfs[index].count * 100;
			ixfs[index].newimg.filters.alpha.opacity = (1 - ixfs[index].count) * 100;
			break;
			
		case 'khtml' :
			ixfs[index].obj.style.KhtmlOpacity = ixfs[index].count;
			ixfs[index].newimg.style.KhtmlOpacity = (1 - ixfs[index].count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixfs[index].obj.style.MozOpacity = (ixfs[index].count == 1 ? 0.9999999 : ixfs[index].count);
			ixfs[index].newimg.style.MozOpacity = (1 - ixfs[index].count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixfs[index].obj.style.opacity = (ixfs[index].count == 1 ? 0.9999999 : ixfs[index].count);
			ixfs[index].newimg.style.opacity = (1 - ixfs[index].count);
	}
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	ixfs[index].newimg.style.visibility = 'visible';

	//keep new image in position with original image
	//in case text size changes mid transition or something
	ixfs[index].newimg.style.left = ixf.getRealPosition(ixfs[index].obj, 'x') + 'px';
	ixfs[index].newimg.style.top = ixf.getRealPosition(ixfs[index].obj, 'y') + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	if(ixfs[index].count == 1)
	{
		//remove the duplicate image
		ixfs[index].newimg.parentNode.removeChild(ixfs[index].newimg);
//                st=setTimeout("fadeCycle('"+ixfs[index].obj.id+"')",1000);	

	}
};



//get real position method
ixf.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;
};
