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