// 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 = Array();

function cacheImages () {
	ixf.imgs = pictures;

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



	//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()
{
	counter++;
	quot = (counter%(pictures.length));

	//if the timer is not already going
	if(ixf.clock == null)
	{
		//copy the image object 
		ixf.obj = arguments[0];
		ixf.olddiv = pixtext;

		//copy the image src argument 
		ixf.src = arguments[1];

		//store the supported form of opacity
		if(typeof ixf.obj.style.opacity != 'undefined')
		{
			ixf.type = 'w3c';
		}
		else if(typeof ixf.obj.style.MozOpacity != 'undefined')
		{
			ixf.type = 'moz';
		}
		else if(typeof ixf.obj.style.KhtmlOpacity != 'undefined')
		{
			ixf.type = 'khtml';
		}
		else if(typeof ixf.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.obj.filters.length > 0 && typeof ixf.obj.filters.alpha == 'object' && typeof ixf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			if(ixf.type == 'ie') {
				if(!ixf.fotograf) {
					ixf.fotograf = document.getElementById('fotograf');
					ixf.fotograf = document.body.appendChild(ixf.fotograf.parentNode.removeChild(ixf.fotograf));
					// ie6 special
					window.onerror = function(){return true;};
					ixf.fotograf.style.zIndex = 100;
					window.onerror = null;
				}
				if(!ixf.logo3) {
					ixf.logo3 = document.getElementById('logo3');
					ixf.logo3 = document.body.appendChild(ixf.logo3.parentNode.removeChild(ixf.logo3));
					// ie6 special
					window.onerror = function(){return true;};
					ixf.logo3.style.zIndex = 100;
					window.onerror = null;
				}
				if(!ixf.logo) {
					ixf.logo = document.getElementById('logo');
					ixf.logo = document.body.appendChild(ixf.logo.parentNode.removeChild(ixf.logo));
					// ie6 special
					window.onerror = function(){return true;};
					ixf.logo.style.zIndex = 100;
					window.onerror = null;
				}
			}
		}
		else
		{
			ixf.type = 'none';
		}

		//change the image text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			ixf.obj.text = arguments[3];
		}

		//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.newimg = document.body.appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			ixf.newimg = document.body.appendChild(document.createElement('img'));

			//set src to new image src
			ixf.newimg.src = ixf.src;

			//set positioning classname
			ixf.newimg.className = 'idupe';

			// old text
			if(ixf.olddiv.parentNode.nodeName.toLowerCase() != "body")
				ixf.olddiv = document.body.appendChild(ixf.olddiv.parentNode.removeChild(ixf.olddiv));
			ixf.olddiv.innerHTML = ixf.obj.text;

			// new text
			ixf.newdiv = document.body.appendChild(ixf.olddiv.cloneNode(true));
			ixf.newdiv.className = 'dobe';
			ixf.newdiv.innerHTML = altparams[quot];

			//copy and convert fade duration argument 
			ixf.length = parseInt(arguments[2], 10) * 1000;

			//create fade resolution argument as 20 steps per transition
			ixf.resolution = parseInt(arguments[2], 10) * 20;

			//start the timer
			ixf.clock = setInterval('ixf.crossfade()', ixf.length/ixf.resolution);
			ixf.onresize();
			ixf.newdiv.style.filter = 'alpha(opacity=100)';
			ixf.newdiv.style.display = 'none';
		}

		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			ixf.obj.src = ixf.src;
			ixf.olddiv.innerHTML = altparams[quot];
		}

	}
};


//crossfade timer function
ixf.crossfade = 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.obj.loaded = false;
		ixf.obj.onload = function(){ixf.endfader();};
		ixf.obj.src = ixf.src;
		ixf.olddiv.innerHTML = ixf.newdiv.innerHTML;
		ixf.olddiv.style.left = ixf.newdiv.style.left;
		ixf.olddiv.style.top = ixf.newdiv.style.top;
		ixf.olddiv.style.width = ixf.newdiv.style.width;
		ixf.olddiv.style.height = ixf.newdiv.style.height;
	}

	if(ixf.count != 1) ixf.transchange();

	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	ixf.newimg.style.visibility = 'visible';
	ixf.newdiv.style.display = '';

	//keep new image in position with original image
	//in case text size changes mid transition or something
	ixf.newimg.style.left = ixf.pos.x + 'px';
	ixf.newimg.style.top = ixf.pos.y + 'px';

	ixf.onresize();

	//if the counter is at the top, which is just after the timer has finished
	if(ixf.count == 1)
	{
	}
};


//set new opacity value on both elements
//using whatever method is supported
ixf.endfader = function()
{
	ixf.transchange();
	//remove the duplicate image
	ixf.newimg.parentNode.removeChild(ixf.newimg);
	//remove the duplicate div
	ixf.newdiv.parentNode.removeChild(ixf.newdiv);
}


//set new opacity value on both elements
//using whatever method is supported
ixf.transchange = function()
{
	switch(ixf.type)
	{
		case 'ie' :
			ixf.obj.filters.alpha.opacity = ixf.count * 100;
			ixf.olddiv.style.filter = 'alpha(opacity='+ixf.obj.filters.alpha.opacity+')';
			ixf.newimg.filters.alpha.opacity = (1 - (ixf.count == 1 ? 0.5 : ixf.count )) * 100;
			ixf.newdiv.style.filter = 'alpha(opacity='+ixf.newimg.filters.alpha.opacity+')';
			break;

		case 'khtml' :
			ixf.olddiv.style.KhtmlOpacity = ixf.obj.style.KhtmlOpacity = ixf.count;
			ixf.newdiv.style.KhtmlOpacity = ixf.newimg.style.KhtmlOpacity = (1 - ixf.count);
			break;

		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.olddiv.style.MozOpacity = ixf.obj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newdiv.style.MozOpacity = ixf.newimg.style.MozOpacity = (1 - ixf.count);
			break;

		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.olddiv.style.opacity = ixf.obj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newdiv.style.opacity = ixf.newimg.style.opacity = (1 - ixf.count);
	}
}


//get real position method
ixf.getRealPosition = function()
{
	this.posX = arguments[0].offsetLeft;
	this.posY = arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null)
	{
		this.posX += this.tmp.offsetLeft;
		this.posY += this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	return {x:this.posX, y:this.posY};
};



//get real position method
ixf.setPosition = function()
{
	var target = arguments[0];
	target.style.left = ixf.obj.parentNode.offsetLeft + (ixf.obj.offsetWidth - target.offsetWidth) + "px";
	target.style.top = ixf.obj.parentNode.offsetTop + (ixf.obj.offsetHeight - target.offsetHeight) + "px";
};



//correct position on resize
ixf.onresize = function()
{
	if(ixf.type == 'none') return;
	ixf.pos = ixf.getRealPosition(ixf.obj);
	if(ixf.olddiv) {
		ixf.olddiv.style.left = ixf.pos.x + (ixf.obj.offsetWidth - ixf.olddiv.offsetWidth) + 'px';
		ixf.olddiv.style.top = ixf.pos.y + (ixf.obj.offsetHeight - ixf.olddiv.offsetHeight) + 'px';
	}
	if(ixf.newdiv) {
		ixf.newdiv.style.left = ixf.pos.x + (ixf.newimg.offsetWidth - ixf.newdiv.offsetWidth) + 'px';
		ixf.newdiv.style.top = ixf.pos.y + (ixf.newimg.offsetHeight - ixf.newdiv.offsetHeight) + 'px';
	}
	if(ixf.fotograf) {
		// ie6 special
		window.onerror = function(){return true;};
		ixf.fotograf.style.left = ixf.pos.x - ixf.obj.offsetLeft + "px";
		ixf.fotograf.style.top = ixf.pos.y + 326 + "px";
		window.onerror = null;
	}
	if(ixf.logo3) {
		// ie6 special
		window.onerror = function(){return true;};
		ixf.logo3.style.left = ixf.pos.x - ixf.obj.offsetLeft + "px";
		ixf.logo3.style.top = ixf.pos.y + "px";
		window.onerror = null;
	}
	if(ixf.logo) {
		// ie6 special
		window.onerror = function(){return true;};
		ixf.logo.style.left = ixf.pos.x - ixf.obj.offsetLeft + "px";
		ixf.logo.style.top = ixf.pos.y + 49 + "px";
		window.onerror = null;
	}
};

if(window.addEventListener){
	window.addEventListener('resize',ixf.onresize,false);
}else if(window.attachEvent){
	window.attachEvent('onresize',ixf.onresize);
}
