/*****
 
Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var fade_imgs = new Array(), zInterval = null, current=0, pause=false, xf_first_time=true;

function so_init()
{
	if(!document.getElementById || !document.createElement)return;

	// DON'T FORGET TO GRAB THIS FILE AND PLACE IT ON YOUR SERVER IN THE SAME DIRECTORY AS THE JAVASCRIPT!
	// http://slayeroffice.com/code/imageCrossFade/xfade2.css
	css = document.createElement("link");
	css.setAttribute("href","xfade.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	document.getElementsByTagName("head")[0].appendChild(css);

	fade_imgs = document.getElementById("imageContainer").getElementsByTagName("img");

	for(i=1;i<fade_imgs.length;i++)
	{
		fade_imgs[i].width = fade_imgs[0].width;
		fade_imgs[i].height = fade_imgs[0].height;
		fade_imgs[i].xOpacity = 0;
	}

	fade_imgs[0].style.display = "block";
	fade_imgs[0].xOpacity = .99;
	
	setTimeout(so_xfade,5000);  // Pause for two seconds before starting fade.  2000 milliseconds = 2 seconds
}

function so_xfade() {

	if(xf_first_time)
	{
		fade_imgs[1].src = "flash/img-2.jpg";
		fade_imgs[2].src = "flash/img-3.jpg";
		fade_imgs[3].src = "flash/img-4.jpg";		
		fade_imgs[4].src = "flash/img-5.jpg";
		fade_imgs[5].src = "flash/img-6.jpg";
		xf_first_time = false;
	}

	cOpacity = fade_imgs[current].xOpacity;
	nIndex = fade_imgs[current+1]?current+1:0;

	nOpacity = fade_imgs[nIndex].xOpacity;
	
	// if the fade is to last for 2 seconds and if the fade is called 20 times a second then the number multiplied by 40 = 1
	// 1 = 40 * .025
	// if the fade is to last 1 second and the fade is called 20 times a second then the number multiplied by 20 = 1
	// 1 = .05 * 20
	// So adjusting the number below and the fade timeout can slow down or speed up the fade and determine how long the fade will last.
	cOpacity-=.025; // how much to fade image
	nOpacity+=.025;
	
	fade_imgs[nIndex].style.display = "block";
	fade_imgs[current].xOpacity = cOpacity;
	fade_imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(fade_imgs[current]); 
	setOpacity(fade_imgs[nIndex]);

	// if finished with fade
	if(cOpacity<=0)
	{
		fade_imgs[current].style.display = "none";
		current = nIndex;
		setTimeout(so_xfade,2000);  // Pause for two seconds before call fade again.  2000 milliseconds = 2 seconds
	}
	else
	{
		setTimeout(so_xfade,50);  // Call fade every 50 milliseconds = 1/20 of a second
	}
	
	// fade image 
	function setOpacity(obj) {
		if(obj.xOpacity>.99)
		{
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}
