/*****

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(), imgs1 = new Array(), titles = new Array(), zInterval = null, current=0, pause=false;
var paramname, picinterval, fadespeed, fadefraction, fadepause, obj, captionObj, tts = new Array(), tts1 = new Array();
var picheight, captionheight, timerP = 0, timerR = 0, timerS = 0, nOpacity, cOpacity, xOpacity, nIndex, css, i, faderun = 1;
var picwidth;

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;

	// 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
	fadepause = 1;
	css = d.createElement("link");
	css.setAttribute("href","xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	
	picheight = getparam('picheight');
	picwidth = getparam('picwidth');
	captionheight = getparam('captionheight');
	
	d.getElementsByTagName("head")[0].appendChild(css);
	d.getElementById("imageContainer").style.display = "block";
	d.getElementById("imageContainer").style.border = "1px";
	d.getElementById("imageContainer").style.height = ''+picheight+'px';
	d.getElementById("imageContainer").style.width = ''+picwidth+'px';
	d.getElementById("imageCaption").style.display = "block";
	d.getElementById("imageCaption").style.height = ''+captionheight+'px';
	d.getElementById("fadingtext").style.display = "none";
	
	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+'px';
		}

	tts[0].style.display = "block";
	tts[0].xOpacity = .99;
	
	picinterval = getparam('picinterval');
	fadespeed = getparam('fadespeed');
	fadefraction = getparam('fadefraction')/100;
	fadepause = 1;
	timerR = setTimeout(so_xfade,1000);
}


function so_xfade() {

	cOpacity = imgs[current].xOpacity;  
	if (faderun == 1)
		{
		faderun = 0;
		fadepause = 1;
		}
	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.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.display = "none";
		current = nIndex;
		timerS = setTimeout(so_xfade,picinterval);
	} else {
		timerP = setTimeout(so_xfade,fadespeed);
	}
	
	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) + ")";
	}
}
