/*******************************************************************************
 IXF1.11 :: Image cross-fade 
 ------------------------------------------------------------------------------
 Copyright (c) 2004 James Edwards (brothercake)          <cake@brothercake.com>
 BSD License                          See license.txt for licensing information
 Info/Docs       http://www.brothercake.com/site/resources/scripts/transitions/
 ------------------------------------------------------------------------------
*******************************************************************************/
//global object
var ixf = { 'clock' : null, 'count' : 1 }
var mainTimer = setInterval('swap("picture")', 3000);
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

ixf.imgs = [
	];

/*****************************************************************************
*****************************************************************************/



//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];
}


//swap setup function
function swap()
{

    clearInterval(mainTimer);
    mainTimer = null;
    mainTimer = setInterval('swap("picture")', Math.floor(Math.random() * 5000) + 5000);

	//if the timer is not already going
	if(ixf.clock == null)
	{
		//copy the image object 
        if (arguments[0] == 'picture') {
            arguments[0] = 'picture' + Math.floor(Math.random() * 2) + Math.floor(Math.random() * 2);
        }
		ixf.subObj = document.getElementById(arguments[0]);
        ixf.mainObj = document.getElementById('picture');
        		
		//store the supported form of opacity
		if(typeof ixf.subObj.style.opacity != 'undefined')
		{
			ixf.type = 'w3c';
		}
		else if(typeof ixf.subObj.style.MozOpacity != 'undefined')
		{
			ixf.type = 'moz';
		}
		else if(typeof ixf.subObj.style.KhtmlOpacity != 'undefined')
		{
			ixf.type = 'khtml';
		}
		else if(typeof ixf.subObj.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.subObj.filters.length > 0 && typeof ixf.subObj.filters.alpha == 'object' && typeof ixf.subObj.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.newMainImg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
            ixf.newSubImg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			//set positioning classname
			ixf.newMainImg.id = 'mainIdupe';
            ixf.newSubImg.id = 'subIdupe';
            ixf.newMainImg.className = ixf.mainObj.className;
            ixf.newSubImg.className = ixf.subObj.className;
			
			//set src to new image src
			ixf.newMainImg.src = ixf.subObj.src
            ixf.newSubImg.src = ixf.mainObj.src;
            

			//move it to superimpose original image
			ixf.newMainImg.style.left = ixf.getRealPosition(ixf.mainObj, 'x') + 'px';
			ixf.newMainImg.style.top = ixf.getRealPosition(ixf.mainObj, 'y') + 'px';
			ixf.newSubImg.style.left = ixf.getRealPosition(ixf.subObj, 'x') + 'px';
			ixf.newSubImg.style.top = ixf.getRealPosition(ixf.subObj, 'y') + 'px';
			
			//copy and convert fade duration argument 
			ixf.length = 2 * 1000;
			
			//create fade resolution argument as 20 steps per transition
			ixf.resolution = 2 * 20;
			
			//start the timer
			ixf.clock = setInterval('ixf.swap()', ixf.length/ixf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
            ixf.tempSrc = ixf.mainObj.src;
			ixf.mainObj.src = ixf.subObj.src;
            ixf.subObj.src = ixf.tempSrc;
		}
		
	}
};


//swap timer function
ixf.swap = function()
{
	//decrease the counter on a linear scale
	ixf.count -= (1 / ixf.resolution);
	
	//if the counter has reached the bottom
	if(ixf.count < (1 / ixf.resolution))
	{
		//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
            ixf.tempSrc = ixf.mainObj.src;
			ixf.mainObj.src = ixf.subObj.src;
            ixf.subObj.src = ixf.tempSrc;
	}
	
	//set new opacity value on both elements
	//using whatever method is supported
	switch(ixf.type)
	{
		case 'ie' :
			ixf.mainObj.filters.alpha.opacity = ixf.count * 100;
			ixf.subObj.filters.alpha.opacity = ixf.count * 100;
			ixf.newMainImg.filters.alpha.opacity = (1 - ixf.count) * 100;
			ixf.newSubImg.filters.alpha.opacity = (1 - ixf.count) * 100;
			break;
			
		case 'khtml' :
			ixf.mainObj.style.KhtmlOpacity = ixf.count;
			ixf.subObj.style.KhtmlOpacity = ixf.count;
			ixf.newMainImg.style.KhtmlOpacity = (1 - ixf.count);
			ixf.newSubImg.style.KhtmlOpacity = (1 - ixf.count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.mainObj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.subObj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newMainImg.style.MozOpacity = (1 - ixf.count);
			ixf.newSubImg.style.MozOpacity = (1 - ixf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.mainObj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.subObj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newMainImg.style.opacity = (1 - ixf.count);
			ixf.newSubImg.style.opacity = (1 - ixf.count);
	}
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	ixf.newMainImg.style.visibility = 'visible';
	ixf.newSubImg.style.visibility = 'visible';
	
	//keep new image in position with original image
	//in case text size changes mid transition or something
	ixf.newMainImg.style.left = ixf.getRealPosition(ixf.mainObj, 'x') + 'px';
	ixf.newMainImg.style.top = ixf.getRealPosition(ixf.mainObj, 'y') + 'px';
	ixf.newSubImg.style.left = ixf.getRealPosition(ixf.subObj, 'x') + 'px';
	ixf.newSubImg.style.top = ixf.getRealPosition(ixf.subObj, 'y') + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	if(ixf.count == 1)
	{
		//remove the duplicate image
		ixf.newMainImg.parentNode.removeChild(ixf.newMainImg);
		ixf.newSubImg.parentNode.removeChild(ixf.newSubImg);
	}
};



//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;
};


