// JavaScript Document

/*
	To use this imageRotation cross fade function you need to have the following:
	1) On the page where the images are being crossfaded you need to have all the <img> tags inside a <div id="imageContainer"> div.  The 
	   JavaScript will pickup all the <img> tags inside the imageContainer div and xfade through them sequentially.
	2) All the images in the imageContainer div should have their display property set to "none" except the first image, it should be "block"
	3) Pass in the length of time you want the first image to hold for in milliseconds (1000 ms in 1 sec)
	4) Pass in the hold time for all subsiquent images before the next crossfade start in ms
	5) Pass in the amount of time the from start to finish the cross fade should take in ms.
*/

var d=document;
var imgs = new Array();
var current=0;

function imageRotator_init(firstImageHoldLength, secondaryImagesHoldLength, xfadeTime) {
	if(!d.getElementById || !d.createElement) {
		return;
	}
	
	//Use defaults if values where not passed in
	if(firstImageHoldLength == null) {
		firstImageHoldLength = 4000;
	}
	if(secondaryImagesHoldLength == null) {
		secondaryImagesHoldLength = 4000;
	}
	if(xfadeTime == null) {
		xfadeTime = 2000;
	}
		
	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 = .99;
	
	//There are 20 intervals in the xfade so we devide the xfadeTime by 20
	xfadeCallString = "xfade(" + secondaryImagesHoldLength + "," + (xfadeTime / 20) + ")";
	setTimeout(xfadeCallString, firstImageHoldLength);
}

//This function is only called from the init function
function xfade(holdLength, intervalLength) {
	
	xfadeCallString = "xfade(" + holdLength + "," + intervalLength + ")";
	
	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]);
	
	
	if(cOpacity<=0) {
		//xfade done so set the old image to display = none
		imgs[current].style.display = "none";
		//Set up for the next xfade by moving the current index
		current = nIndex;
		//Hold... then call this function again
		setTimeout(xfadeCallString, holdLength);
	} else {
		setTimeout(xfadeCallString, intervalLength);
	}
	
	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) + ")";
	}
	
}