/*****

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(), zInterval = null, current=0, pause=false;

var marks = new Array(), titles = new Array(), timer, nIndex, timerMin;

function so_init() {
	if(!d.getElementById || !d.createElement)return;

	// DON'T FORGET TO GRAB THIS FILE AND PLACE IT ON YOUR SERVER IN THE SAME DIRECTORY AS THE JAVASCRIPT!
	// http://slayeroffice.com/code/imageCrossFade/xfade2.css
	/*
	css = d.createElement("link");
	css.setAttribute("href","xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);
	*/



	imgs = d.getElementById("mainVisualImage").getElementsByTagName("div");

	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	marks = d.getElementById("mainVisualCurrentList").getElementsByTagName("li");
	
	titles = d.getElementById("mainVisualTitle").getElementsByTagName("p");
	//for(i=1;i<titles.length;i++) titles[i].xRight = 700;
	titles[0].style.display = "block";
	//titles[0].xRight = 0;
	
	timer = setTimeout("mix()", 10000);
}

function mix(nextNum){
	if (nextNum != undefined) {
		nextNum--;
	}
	so_xfade(nextNum);
	titleChange(nextNum);
	markChange(nextNum);
}

function so_xfade(nextNum) {
	cOpacity = imgs[current].xOpacity;
	
	if(nextNum == undefined){
		nIndex = imgs[current+1]?current+1:0;
	} else {
		nIndex = nextNum;
	}

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.01;
	nOpacity+=.01;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	//title_slide(current, nIndex);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		//titles[current].style.display = "none";
		//titles[current].style.right = "700px";
		//titles[current].xRight = 700;
		current = nIndex;
		nIndex = undefined;
		clearTimeout(timer);
		timer = setTimeout("mix()", 10000);
	} else {
		timerMin = setTimeout('so_xfade('+nIndex+')',10);
	}
	
}
	
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) + ")";
}

/*
function title_slide(current, nIndex){
	cRight = titles[current].xRight;
	
	nRight = titles[nIndex].xRight;
	
	cRight -= 7;
	nRight -= 7;
	
	titles[nIndex].style.display = "block";
	titles[current].xRight = cRight;
	titles[nIndex].xRight = nRight;
	
	titles[current].style.right = titles[current].xRight + 'px';
	titles[nIndex].style.right = titles[nIndex].xRight + 'px';
	
}
*/

function titleChange(nextNum){
	titles[current].style.display = "none";
	
	var nTitle;
	if(nextNum != undefined){
		nTitle = titles[nextNum];
	} else {
		nTitle = titles[current+1] ? titles[current+1] : titles[0];
	}
	nTitle.style.display = "block";
}

function markChange(nextNum){
	marks[current].className = '';
	
	var nMark;
	if(nextNum != undefined){
		nMark = marks[nextNum];
	} else {
		nMark = marks[current+1] ? marks[current+1] : marks[0];
	}
	nMark.className = 'current';
}

function imageChange(num){
	//alert(nIndex+','+(num-1));
	if((num-1 != current) && (num-1 != nIndex)){
		if(nIndex || nIndex==0){
			nOpacity = 0.99;
			imgs[nIndex].xOpacity = nOpacity;
			setOpacity(imgs[nIndex]);
			cOpacity = 0;
			imgs[current].xOpacity = cOpacity;
			setOpacity(imgs[current]);
			current = nIndex;
			nIndex = num;
			clearTimeout(timerMin);
		}
		clearTimeout(timer);
		mix(num);
	}
}


