/*****

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

Navigational buttons added by Aaron Harp - http://www.aaronharp.com.

*****/


window.addEventListener?window.addEventListener("load",ss_init,false):window.attachEvent("onload",ss_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false; var direction=0;

function ss_init() {
	if(!d.getElementById || !d.createElement)return;
			
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = 1.00;
}

function so_change(dir) {
  if(imgs[current].xOpacity != 1.00) {
    return;
  }
  direction = dir;
  ss_xfade();
}

function ss_xfade() {		
	if(current+direction < 0) {
	  nIndex = imgs.length - 1;
	} else if (current+direction >= imgs.length) {
	  nIndex = 0;
	} else {
    nIndex = current+direction;    
	}
	
	
	cOpacity = imgs[current].xOpacity;
	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.1; 
	nOpacity+=.1;
		
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		imgs[current].xOpacity = 0;
		setOpacity(imgs[current]);

		imgs[nIndex].xOpacity = 1;
		setOpacity(imgs[current]);
		
		current = nIndex;
	} else {
		setTimeout(ss_xfade,50);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity>1.0) {
			obj.xOpacity = 1;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}
