window.addEventListener ? window.addEventListener("load", crossfade_init, false) : window.attachEvent("onload", crossfade_init);

var images = new Array();
var currentIndex = 0;

function crossfade_init() {
	if (!document.getElementById || !document.createElement)
		return;
		
	if (!document.getElementById("crossfadeImageArray"))
		return;
	
	images = document.getElementById("crossfadeImageArray").getElementsByTagName("img");
	
	for (i = 1; i < images.length; i++)
		images[i].xOpacity = 0;
		
	images[0].style.display = "block";
	images[0].xOpacity = .99;
	
	setTimeout(crossfade_fade, 5000);
}

function crossfade_fade() {
	var currentOpacity = images[currentIndex].xOpacity;
	var nextIndex = images[currentIndex + 1]? currentIndex + 1 : 0;
	var nextOpacity = images[nextIndex].xOpacity;
	
	currentOpacity -= .05;
	nextOpacity += .05;
	
	images[nextIndex].style.display = "block";
	images[currentIndex].xOpacity = currentOpacity;
	images[nextIndex].xOpacity = nextOpacity;
	
	setOpacity(images[currentIndex]); 
	setOpacity(images[nextIndex]);
	
	if(currentOpacity <= 0) {
		images[currentIndex].style.display = "none";
		currentIndex = nextIndex;
		setTimeout(crossfade_fade, 5000);
	} else {
		setTimeout(crossfade_fade, 50);
	}
	
	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) + ")";
	}
}
