/*****

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 d=document, imgs = new Array(), zInterval = null, current=0, pause=false;

var swapTimeout=5000   // Time in milliseconds before image will be swapped
var fadeTimeout=100    // Time in milliseconds for each step in fading the image
var fadeStep=0.02
var direction=-1		// Fade in if positive, out if -ve.

function so_init() {
	if(!d.getElementById || !d.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 = d.createElement("link");
	css.setAttribute("href","xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);

	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) { imgs[i].xOpacity = 0; imgs[i].style.display = "none";}
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	setTimeout(so_xfade,swapTimeout);
}

function so_xfade() {	
	img =  imgs[current];
	
	cOpacity = img.xOpacity;
	cOpacity += (fadeStep * direction);
	img.xOpacity = cOpacity;
	
	if (direction>0) { // Fading the current image in
		if ( cOpacity > 0.99 ) { // Finished fading in - flip direction to start fading out
			direction = -1;
		}
	}
	else {	// fading the current image out
		if ( cOpacity <= 0 ) { // Finished fading out - get the next image and start fading in
			setOpacity(img); 
			img.style.display = "none";
			
			current = imgs[current+1]?current+1:0;
			img = imgs[current];
			setOpacity(img); 
			
			img.style.display = "block";
			
			direction = 1;
		}
	}
	
	setOpacity(img);
	setTimeout(so_xfade,fadeTimeout);


	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) + ")";
	}
	
}