//  IXF1.11 :: Image cross-fade 
//  *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// ******************************************************
//global object
var ixf = { 'clock' : null, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/
/*
ixf.imgs = [
	'buttons/udm4-whitebutton88x31.gif',
	'buttons/udm4-greenbutton88x31.gif',
	'buttons/udm4-purplebutton88x31.gif'
	];
*/
/*****************************************************************************
*****************************************************************************/


/*
//cache the images
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()
{
	//if the timer is not already going
	if(ixf.clock == null)
	{
		var argv = arguments[0];
		var count = 0;
		
		ixf.arr = [];
		ixf.type = null;

		for(var i = 0; i < argv.length; i++) 
		{
			ixf.arr[count] = { };
			
			//copy the image object 
			ixf.arr[count].obj = argv[i][0];

			//copy the image src argument 
			ixf.arr[count].src = argv[i][1];

			//opacity max
			ixf.arr[count].opacitymax = argv[i][2];

			//Position
			ixf.arr[count].pos = argv[i][3];
			
			//store the supported form of opacity
			if(ixf.type == null) 
			{
				if(typeof ixf.arr[count].obj.style.opacity != 'undefined')
				{
					ixf.type = 'w3c';
				}
				else if(typeof ixf.arr[count].obj.style.MozOpacity != 'undefined')
				{
					ixf.type = 'moz';
				}
				else if(typeof ixf.arr[count].obj.style.KhtmlOpacity != 'undefined')
				{
					ixf.type = 'khtml';
				}
				else if(typeof ixf.arr[count].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
					ixf.type = (ixf.arr[count].obj.filters.length > 0 && typeof ixf.arr[count].obj.filters.alpha == 'object' && typeof ixf.arr[count].obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
				}
				else
				{
					ixf.type = 'none';
				}
			}
			
			//if any kind of opacity is supported
			if(ixf.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
				ixf.arr[count].newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

				//invisible
				ixf.arr[count].newimg.style.visibility = 'hidden';

				//set positioning classname
				ixf.arr[count].newimg.className = 'idupe';

				//set src to new image src
				ixf.arr[count].newimg.src = ixf.arr[count].src

				//move it to superimpose original image
				ixf.arr[count].newimg.style.left = ixf.arr[count].pos[0] + 'px';
				ixf.arr[count].newimg.style.top = ixf.arr[count].pos[1] + 'px';

				//transparent
				switch(ixf.type)
				{
					case 'ie' :
						ixf.arr[count].newimg.filters.alpha.opacity = 0;
						break;
						
					case 'khtml' :
						ixf.arr[count].newimg.style.KhtmlOpacity = 0;
						break;
						
					case 'moz' : 
						ixf.arr[count].newimg.style.MozOpacity = 0;
						break;
						
					default : 
						ixf.arr[count].newimg.style.opacity = 0;					
				}				
			}
			//otherwise if opacity is not supported
			else
			{
				//just do the image swap
				ixf.arr[count].obj.src = ixf.arr[count].src;
			}
			
			count++;
		}
		
		//opacity is supported
		if(ixf.type != 'none') 
		{		
			//copy and convert fade duration argument 
			ixf.length = parseInt(arguments[arguments.length - 1], 10) * 1000;   //1000
			
			//create fade resolution argument as 20 steps per transition
			ixf.resolution = parseInt(arguments[arguments.length - 1], 10) * 5;  //20
			
			//start the timer 
			ixf.clock = setInterval('ixf.crossfade()',ixf.length/ixf.resolution);
			
		}
	}
}

//crossfade timer function
ixf.crossfade = function()
{
	var step = 1 / ixf.resolution;

	//decrease the counter on a linear scale
	ixf.count -= step;
	
	//if the counter has reached the bottom
	if(ixf.count < step)
	{
		//clear the timer
		clearInterval(ixf.clock);
		ixf.clock = null;
		
		//reset the counter
		ixf.count = 1;
		
		//set the original image to the src of the new image
		for(var k = 0; k < ixf.arr.length; k++) 
		{
			ixf.arr[k].obj.src = ixf.arr[k].src;
		}
	}
	else
	{
		//set new opacity value on both elements
		//using whatever method is supported
		for(var i = 0; i < ixf.arr.length; i++) 
		{
			switch(ixf.type)
			{
				case 'ie' :
					if(String(ixf.arr[i].opacitymax).length == 0 || (1 - ixf.count) * 100 <= ixf.arr[i].opacitymax) {
						ixf.arr[i].newimg.filters.alpha.opacity = (1 - ixf.count) * 100;
					}
					break;
					
				case 'khtml' :
					if(String(ixf.arr[i].opacitymax).length == 0 || (1 - ixf.count) <= ixf.arr[i].opacitymax) {
						ixf.arr[i].newimg.style.KhtmlOpacity = (1 - ixf.count);
					}
					break;
					
				case 'moz' : 
					if(String(ixf.arr[i].opacitymax).length == 0 || (1 - ixf.count) <= ixf.arr[i].opacitymax) {
						ixf.arr[i].newimg.style.MozOpacity = (1 - ixf.count);
					}
					break;
					
				default : 
					if(String(ixf.arr[i].opacitymax).length == 0 || (1 - ixf.count) <= ixf.arr[i].opacitymax) {
						ixf.arr[i].newimg.style.opacity = (1 - ixf.count);					
					}
			}
			
			//now that we've gone through one fade iteration 
			//we can show the image that's fading in	
			ixf.arr[i].obj.style.zIndex = ((ixf.arr.length - (ixf.arr.length - i)) + 1) + ixf.arr.length;
			ixf.arr[i].newimg.style.zIndex = ((ixf.arr.length - (ixf.arr.length - i)) + 1);			
			ixf.arr[i].newimg.style.visibility = 'visible';
			
			//keep new image in position with original image
			//in case text size changes mid transition or something
			ixf.arr[i].newimg.style.left = ixf.arr[i].pos[0] + 'px';
			ixf.arr[i].newimg.style.top = ixf.arr[i].pos[1] + 'px';
		}
	}

	//if the counter is at the top, which is just after the timer has finished
	if(ixf.count == 1)
	{
		for(var j = 0; j < ixf.arr.length; j++)
		{
			//remove the duplicate image
			ixf.arr[j].newimg.parentNode.removeChild(ixf.arr[j].newimg);
		}
	}
}

//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;
}

//*****************************************************************************

//julien 27/01/2006 
//Externalisation pour appels externes !
function getOpacityType(Elt) 
{
	var pType = 'none';
  
	//store the supported form of opacity
	if(typeof Elt.style.opacity != 'undefined')
	{
		pType = 'w3c';
	}
	else if(typeof Elt.style.MozOpacity != 'undefined')
	{
		pType = 'moz';
	}
	else if(typeof Elt.style.KhtmlOpacity != 'undefined')
	{
		pType = 'khtml';
	}
	else if(typeof Elt.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
		pType = (Elt.filters.length > 0 && typeof Elt.filters.alpha == 'object' && typeof Elt.filters.alpha.opacity == 'number') ? 'ie' : 'none';
	}
	else
	{
		pType = 'none';
	}
		
	return pType;
}

//julien 27/01/2006 
//Externalisation pour appels externes !
function setOpacity(Elt, value, type) 
{
	if(type == null) 
	{
		type = getOpacityType(Elt);
	}
  
	//set new opacity value on both elements
	//using whatever method is supported
	switch(type)
	{
		case 'ie' :
			Elt.filters.alpha.opacity = value * 100;
			break;
			
		case 'khtml' :
			Elt.style.KhtmlOpacity = value;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			Elt.style.MozOpacity = (value == 1 ? 0.9999999 : value);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			Elt.style.opacity = (value == 1 ? 0.9999999 : value);
	}
}

// Retourne l'opacité courante
function getOpacityValue(Elt) {	
	if(typeof Elt == "string") 
	{
		Elt = document.getElementById(Elt);
	}
	
	switch(getOpacityType(Elt)) {
	case 'ie' :
		return Elt.filters.alpha.opacity;
		
	case 'khtml' :
		return Elt.style.KhtmlOpacity;
		
	case 'moz' : 
		return  Elt.style.MozOpacity;
		
	default : 
		return Elt.style.opacity;
	}
}

// BPU 13/03/06
// Retourne l'opacité courante pour l'impression
function getOpacityValueForPrint(Elt) {	
	if(typeof Elt == "string") 
	{
		Elt = document.getElementById(Elt);
	}
	
	switch(getOpacityType(Elt)) {
	case 'ie' :
		return Elt.filters.alpha.opacity;
		
	case 'khtml' :
		return Elt.style.KhtmlOpacity * 100;
		
	case 'moz' : 
		return  Elt.style.MozOpacity * 100;
		
	default : 
		return Elt.style.opacity * 100;
	}
}

function getRealPosition()
{
	var pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	var tmp = arguments[0].offsetParent;
	while(tmp != null)
	{
		pos += (arguments[1] == 'x') ? tmp.offsetLeft : tmp.offsetTop;
		tmp = tmp.offsetParent;
	}
	
	return pos;
}
