/*****

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 = window.document, imgs = new Array(), zInterval = 3000, current = 0, timer = null, pause = false, prev = false, cOpacity = null;

function so_init(){
	if(!d || !d.getElementById || !d.createElement) return;
	
	imgs = Array();
	arr = d.getElementById("xfadeContainer").getElementsByTagName("div");
	for(i = 0; i < arr.length; i++){
		if(arr[i].className == "item"){
			imgs.push(arr[i]);
		}
	}
	
	for(i = 1; i < imgs.length; i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	timer = setTimeout(so_xfade,zInterval);
}
function so_display(id){
	current = id;
	imgs[id].style.display = "block";
	imgs[id].xOpacity = .99;
	setOpacity(imgs[id]);
	for(i = 0; i < imgs.length; i++){
		if(i != id){
			imgs[i].style.display = "none";
			imgs[i].xOpacity = 0;
			setOpacity(imgs[i]);
		}
	}
	so_pause();
}
function so_pause(){
	pause = true;
}
function so_xfade(){
	if(!pause){
		cOpacity = imgs[current].xOpacity;
		if(prev){
			if(current > 0){ nIndex = current-1; }else{ nIndex = imgs.length-1; }
		}else{
			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 && !pause){
		imgs[current].style.display = "none";
		current = nIndex;
		timer = setTimeout(so_xfade,zInterval);
		prev = false;
	}else{
		timer = setTimeout(so_xfade,5);
	}
}
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) + ")";
}
