/*****

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(), titles = new Array(), zInterval = null, current=0, pause=false, nIndex=1;
var paramname, picinterval, fadespeed, fadefraction, fadepause, obj, captionObj, tts = new Array();
var picheight, captionheight, t, jumppic = 0, gotoPic=0, fadefraction, t1, t2, css, fontsizez = 'font-size';
var nOpacity, cOpacity;

function getparam(paramname) {
	var paramvalue;
	d.getElementById(paramname).style.visibility="hidden";
	d.getElementById(paramname).style.height="1px";
	d.getElementById(paramname).style.width="1px";
	paramvalue = d.getElementById(paramname).innerHTML;
	return paramvalue;
	}

function so_init() {
	if(!d.getElementById || !d.createElement)return;
	
	css = d.createElement("link");
	css.setAttribute("href","xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	
	picheight = getparam('picheight');
	captionheight = getparam('captionheight');
	
	d.getElementsByTagName("head")[0].appendChild(css);
	d.getElementById("imageContainer").style.display = "block";
	d.getElementById("imageContainer").style.border = "1px";
	d.getElementById("imageCaption").style.visibility = "visible";   //display = "block";
	d.getElementById("imageContainer").style.height = ''+picheight+'px';
	d.getElementById("imageCaption").style.height = ''+captionheight+'px';
	
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	for(i=0;i<imgs.length;i++) {
		imgs[i].xOpacity = 0; 	
		imgs[i].style.height = ''+picheight+'px';
	 	imgs[i].style.display = "none";}

	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;

	captionObj = document.getElementById('imageCaption');
	captionObj.style.display = "block";
	
	for(i=0;i<imgs.length;i++) {
		paramname = 'pic'+((i+1)+'');
		tts[i] = d.getElementById(paramname);
		tts[i].xOpacity =0;
		tts[i].style.height = (captionheight-10)+'px';
		}

	tts[0].style.visibility = "visible";   display = "block";
	tts[0].xOpacity = .99;

	if (document.documentMode==7 || document.documentMode==8)
		{
		for(i=0;i<imgs.length;i++) {
			paramname = 'h2text'+((i+1)+'');
			d.getElementById(paramname).style.fontSize = '0.9em';
			d.getElementById(paramname).style.fontWeight = 'normal';
			d.getElementById(paramname).style.color = '#990000';
			}
		}


	picinterval = getparam('picinterval');
	fadespeed = getparam('fadespeed');
	fadefraction = getparam('fadefraction')/100;
	fadepause = 1;
	setTimeout(so_xfade,1000);
}

function restart_xfade(gotoPic) {
	clearTimeout(t1);
	clearTimeout(t2);

	nOpacity = imgs[nIndex].xOpacity;  
	if (nOpacity > 0)
		{	
		imgs[nIndex].style.display = "none";
		imgs[nIndex].xOpacity = 0;
		tts[nIndex].style.visibility = "hidden";  //display = "none";
		tts[nIndex].xOpacity = 0;
		setOpacity(imgs[nIndex]);
		setOpacity(tts[nIndex]);
		}
	
	nIndex = gotoPic-1;
		
	cOpacity=0; 
	nOpacity=0.99;
	
	imgs[nIndex].style.display = "block";
	imgs[nIndex].xOpacity = 1;
	imgs[current].xOpacity = 0;
	imgs[current].style.display = "none";
	
	tts[nIndex].style.visibility = "visible";   //display = "block";
	tts[nIndex].xOpacity = 1;
	tts[current].xOpacity = 0;
	tts[current].style.visibility = "hidden";  //display = "none";
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	setOpacity(tts[current]); 
	setOpacity(tts[nIndex]);
	

	paramname = "but"+(current+1);
	d.getElementById(paramname).style.color = "#888888";

	current = nIndex;

	paramname = "but"+(current+1);
	d.getElementById(paramname).style.color = "#000000";
	t1=setTimeout(so_xfade,picinterval);

	function setOpacity(obj) {
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";  
	}
}

function so_xfade() {

	cOpacity = imgs[current].xOpacity;  
	if (fadepause == 1)
		nIndex = imgs[current+1]?current+1:0;
	
	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=fadefraction; 
	nOpacity+=fadefraction;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	tts[nIndex].style.visibility = "visible";   //display = "block";
	tts[current].xOpacity = cOpacity;
	tts[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	setOpacity(tts[current]); 
	setOpacity(tts[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		tts[current].style.visibility = "hidden";  //display = "none";

		paramname = "but"+(current+1);
		d.getElementById(paramname).style.color = "#888888";

	//	if (gotoPic != 0)
	//		nIndex = (gotoPic - 1);	
	//	fadefractiont = fadefraction;
	//	gotoPic = 0;
		current = nIndex;

		paramname = "but"+(current+1);
		d.getElementById(paramname).style.color = "#000000";

		//setTimeout(function() {so_xfade()}, picinterval);
		t1=setTimeout(so_xfade,picinterval);
	} else {
		//setTimeout(function() {so_xfade()}, fadespeed);
		t2=setTimeout(so_xfade,fadespeed);
	}
//	obj = document.getElementById('imageCaption');
//	obj.innerHTML = titles[nIndex];
	
	
	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) + ")";  
	}
}