/*****

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, divs = new Array(), zInterval = null, current=0, pause=false, timeout, canFade = false, StopFade = false, next = "";

function so_init() {
	if(!d.getElementById || !d.createElement)return;
	
	canFade = true;
	css = d.createElement("link");
	css.setAttribute("href","xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);
	so_begin();
}

function so_begin(){
	if (!canFade) return;
	if (timeout) clearTimeout(timeout);
	if (!d.getElementById("pieces")) return;
	var navBar = d.createElement("div");
	navBar.setAttribute("id","navBar");
	divs = d.getElementById("pieces").getElementsByTagName("div");
	var navNum;
	for(i=0;i<divs.length;i++) 
	{
		if (i>0) divs[i].xOpacity = 0;
		navNum = d.createElement("a");
		navNum.setAttribute("id","pieceLink"+i)
		navNum.setAttribute("href","javascript:void(showPiece(" + i + "));");
		if (d.getElementsByTagName("body")[0].innerText)
		{
			navNum.innerText = i+1;
		} else
		{
			navNum.textContent = i+1;
		}
		if (i==0) 
		{
			navNum.style.borderLeftWidth = "2px";
			navNum.style.backgroundColor = "#cccc99";
			navNum.style.borderBottomColor = "#cccc99";
		}
		if (i+1 == divs.length) navNum.style.borderRightWidth = "2px";
		navNum.style.zIndex = 100;
		navBar.appendChild(navNum);
		navNum = "";
	}
	if (i > 1) d.getElementById("content").appendChild(navBar);
	divs[0].style.display = "block";
	divs[0].xOpacity = .99;
	
	timeout = setTimeout(so_xfade,4000);
}

function showPiece(i)
{
	clearTimeout(timeout);
	next = i;
	if (next == current) return false;
	StopFade = true;
	so_xfade();
	return false;
}

function so_xfade() {
	cOpacity = divs[current].xOpacity;
	if (next.toString() != "")
	{
		nIndex = next;
		next="";
	} else {
		nIndex = divs[current+1]?current+1:0;		
	}
	nOpacity = divs[nIndex].xOpacity;
	
	var tmpObj = d.getElementById("pieceLink" + current).style
	tmpObj.backgroundColor = "#999966";
	tmpObj.borderBottomColor = "#999966";
	tmpObj = d.getElementById("pieceLink" + nIndex).style;
	tmpObj.backgroundColor = "#cccc99";
	tmpObj.borderBottomColor = "#cccc99";

	if (!StopFade)
	{
	  cOpacity-=.25; 
		nOpacity+=.25;
	} else {
		cOpacity = 0;
		nOpacity = .99;
	}
	
	divs[nIndex].style.display = "block";
	divs[current].xOpacity = cOpacity;
	divs[nIndex].xOpacity = nOpacity;
	
	setOpacity(divs[current]); 
	setOpacity(divs[nIndex]);
	
	if(cOpacity<=0) {
		divs[current].style.display = "none";
		last = current;
		current = nIndex;
		if (!StopFade) {
			timeout = setTimeout(so_xfade,6000);
		}
	} else {
		timeout = setTimeout(so_xfade,25);
	}
}	

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) + ")";
}
