/*****

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(), imgs2 = new Array(), zInterval = null, c=0, c2 = 0, pause=false,interval_top=20000,interval_bottom=5000;

function so_init() {
	if(!d.getElementById || !d.createElement)return;
	
	/*css = d.createElement("link");
	css.setAttribute("href","js/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);*/
	
	imgs = d.getElementById("imageContainer1").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;

	imgs2 = d.getElementById("imageContainer2").getElementsByTagName("img");
	for(i=1;i<imgs2.length;i++) imgs2[i].xOpacity = 0;
	imgs2[0].style.display = "block";
	imgs2[0].xOpacity = .99;
	
	setTimeout(fade2, interval_bottom);
	setTimeout(fade1, interval_top);
}

function fade1() {
	so_xfade(imgs, c, 1);
}

function fade2() {
	so_xfade(imgs2, c2, 2);
}

function so_xfade(theImg, current, which) {
	cOpacity = theImg[current].xOpacity;
	nIndex = theImg[current+1]?current+1:0;
	nOpacity = theImg[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	theImg[nIndex].style.display = "block";
	theImg[current].xOpacity = cOpacity;
	theImg[nIndex].xOpacity = nOpacity;
	
	setOpacity(theImg[current]); 
	setOpacity(theImg[nIndex]);
	
	if(cOpacity<=0) {
		theImg[current].style.display = "none";
		current = nIndex;
//		setTimeout(so_xfade,interval);

		if ( which == 2 ) { 
//			alert('setTimeout(fade2, interval)');
			setTimeout(fade2, interval_bottom);
			c2 = current;
		} else {
//			alert('setTimeout(fade1, interval)');
			setTimeout(fade1, interval_top);
			c = current;
		}

	} else {
//		setTimeout(so_xfade,50);
		if ( which == 2 ) {
			setTimeout(fade2, 50);
		} else {
			setTimeout(fade1, 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) + ")";
	}
}