
/*****
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

adapted with additional code for text and onClick events added at worldallergy.org 7/08
*****/

window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, imgs = new Array(), text = new Array(), zInterval = null, current=0, textcurrent=0, pause=false, blnReboot = false; var currTO = 0; // added a current timeout id so it can be cleared onClick

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

	imgs = d.getElementById("homeprogramsFade").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	
	text = d.getElementById("homeprogramsFade").getElementsByTagName("p");
	for(i=1;i<text.length;i++) text[i].xOpacity = 0;
	text[0].style.display = "block";
	text[0].xOpacity = .99;
	
	currTO = setTimeout(so_xfade,10000);
}

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

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	
	ctOpacity = text[textcurrent].xOpacity;
	ntIndex = text[textcurrent+1]?textcurrent+1:0;

	ntOpacity = text[ntIndex].xOpacity;
	
	ctOpacity-=.05; 
	ntOpacity+=.05;
	
	text[ntIndex].style.display = "block";
	text[textcurrent].xOpacity = ctOpacity;
	text[ntIndex].xOpacity = ntOpacity;
	
	setOpacity(text[textcurrent]); 
	setOpacity(text[ntIndex]);
	
		
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		text[textcurrent].style.display = "none"; 
		current = nIndex;
		textcurrent = ntIndex;
		if(!blnReboot){
			currTO = setTimeout(so_xfade,10000);
		}
	} else {
		if(!blnReboot){
			currTO = setTimeout(so_xfade,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 clearProgram () {
	// reset Timeout on so_xfade
	clearTimeout(currTO);
	blnReboot = true;
	
	// clear image
	imgs[current].style.display = "none";
	nIndex = imgs[current+1]?current+1:0;
	imgs[nIndex].style.display = "none";
	// clear text
	text[textcurrent].style.display = "none";
	ntIndex = text[textcurrent+1]?textcurrent+1:0;
	text[ntIndex].style.display = "none";

	imgs[current].xOpacity = 0;
	imgs[current].cOpacity = 0;
	imgs[current].nOpacity = 0;
	imgs[nIndex].xOpacity = 0;
	imgs[nIndex].cOpacity = 0;
	imgs[nIndex].nOpacity = 0;

	text[textcurrent].xOpacity = 0;
	text[textcurrent].ctOpacity = 0;
	imgs[current].ntOpacity = 0;
	text[ntIndex].xOpacity = 0;
	text[ntIndex].ctOpacity = 0;
	text[ntIndex].ntOpacity = 0;
	
	// added these to set
	setOpacity(text[textcurrent]); 
	setOpacity(text[ntIndex]);
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
}

	
function selectProgram (newcurrent,newnIndex) {	

	clearProgram ();
	
	current = newcurrent;
	nIndex = newnIndex;
	textcurrent = newcurrent;
	ntIndex = newnIndex;
	
	imgs[current].style.display = "block";
	imgs[nIndex].style.display = "block";	
	text[textcurrent].style.display = "block";
	text[ntIndex].style.display = "block";	
	
	
	imgs[current].xOpacity = .99;
	imgs[current].cOpacity = .99;
	imgs[current].nOpacity = .99;
	imgs[nIndex].xOpacity = .99;
	imgs[nIndex].cOpacity = .99;
	imgs[nIndex].nOpacity = .99;

	text[textcurrent].xOpacity = .99;
	text[textcurrent].ctOpacity = .99;
	text[textcurrent].ntOpacity = .99;
	text[ntIndex].xOpacity = .99;
	text[ntIndex].ctOpacity = .99;
	text[ntIndex].ntOpacity = .99;
	
	// added these to set
	setOpacity(text[textcurrent]); 
	setOpacity(text[ntIndex]);
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	// restart the so_xfade timer'
	blnReboot = false;
	currTO = setTimeout(so_xfade,10000);
	
}