window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);
var boxcount=0;
var d=document, imgs = new Array(), zInterval = null, current = new Array(), pause=false;

function so_init(){
	if (!d.getElementById || !d.createElement) 
		return;
	//css = d.createElement("link");
	//css.setAttribute("href","/stylesheets/screen.css");
	//css.setAttribute("rel", "stylesheet");
	//css.setAttribute("type", "text/css");
	//d.getElementsByTagName("head")[0].appendChild(css);

    for (var j = 1; j <= boxcount; j++) {
		var container = d.getElementById("animBoxFader" + j);
		if (container != null) {
			imgs[j - 1] = container.getElementsByTagName("img");
			for (i = 1; i < imgs[j - 1].length; i++) 
				imgs[j - 1][i].xOpacity = 0;
			imgs[j - 1][0].style.display = "block";	
			imgs[j - 1][0].xOpacity = .99;
			current[j - 1] = 0;
			container.parentNode.style.backgroundImage = 'none';
		}
	}
	
	setTimeout(so_xfade,5500);
}

function so_xfade() {
	for (var j = 1; j <= imgs.length; j++) {
		var cOpacity = imgs[j - 1][current[j - 1]].xOpacity;
		var nIndex = imgs[j - 1][current[j - 1] + 1] ? current[j - 1] + 1 : 0;
		
		var nOpacity = imgs[j - 1][nIndex].xOpacity;
		
		cOpacity -= .05;
		nOpacity += .05;
		
		imgs[j - 1][nIndex].style.display = "block";
		imgs[j - 1][current[j - 1]].xOpacity = cOpacity;
		imgs[j - 1][nIndex].xOpacity = nOpacity;
		
		setOpacity(imgs[j - 1][current[j - 1]]);
		setOpacity(imgs[j - 1][nIndex]);
		
		if (cOpacity <= 0) {
			imgs[j - 1][current[j - 1]].style.display = "none";
			current[j - 1] = nIndex;
		}
		
		if (j == 1) { // Use the first image to set the timeout for all the images
			if (cOpacity <= 0) {
				setTimeout(so_xfade, 5500);
			}
			else {
				setTimeout(so_xfade, 80);
			}
		}
	}
		
	function setOpacity(obj) {
		if(obj.xOpacity>100) {
			obj.xOpacity = .99;
			return;
		}
		if(obj.xOpacity<0)
			obj.xOpacity = 0;
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}
