/*****

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


*****/

var d=document, imgs = new Array(), current=0, interval=4000, timer;

function so_init( ) 
{
	if(!d.getElementById || !d.createElement)return;
	
	// Fill the array with the images
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	
	// Hide all of the images
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	
	// Show the first image
	imgs[current].style.display = "block";
	imgs[current].xOpacity = .99;
	
	// Start the timer to switch to next picture
	timer=setTimeout(so_xfade,interval);	
}
function so_xfade_reset(imgNum) 
{
	// Stop the timer
	clearTimeout(timer);
	
	// Hide the 'current' image that is being displayed
	imgs[current].style.display = "none";
	
	// Set the current image to the one that is selected
	current=imgNum;
	
	// Show the image that is selected
	imgs[current].xOpacity = .99;
	setOpacity(imgs[current]);
	
	// Initialize the state of all images
	so_init( );
}
function so_xfade() 
{
	// Fade out the 'current' image and fade in the next image ('nIndex')  
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
		timer=setTimeout(so_xfade,interval);
	} else {
		imgs[current].style.display = "none";
		timer=setTimeout(so_xfade,5);
	}
}

function setOpacity(obj) 
{
	// Sets the browser opacity to the value of xOpacity
	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) + ")";
}

