var RotatingBanner = {
	delayMS: 6000, // Delay before displaying next image, in milliseconds
	bPlaying: 1, // 1 = playing, 0 = paused

	arrayImgs: null, // Array of images
	currentIndex: 0, // Index of current image
	nextIndex: 1, // Index of next image
	mainTimerID: null,
	fadeTimerID: null,
	counterZ: 2, // counter, used for z-index of front image

	fMainLoop:function () {

		RotatingBanner.arrayImgs[RotatingBanner.nextIndex].xOpacity = 0; // Set opacity of next image to 0
		RotatingBanner.fSetOpacity(RotatingBanner.arrayImgs[RotatingBanner.nextIndex]);

		RotatingBanner.counterZ++;
		RotatingBanner.arrayImgs[RotatingBanner.nextIndex].parentNode.style.zIndex = RotatingBanner.counterZ; // Place next <a> on top

		RotatingBanner.fCrossFade(); // do fade

		if (RotatingBanner.bPlaying)
			RotatingBanner.mainTimerID = setTimeout(RotatingBanner.fMainLoop,RotatingBanner.delayMS); // delay, recurse
	},

	fCrossFade:function () {
		RotatingBanner.fadeTimerID = null;
		RotatingBanner.arrayImgs[RotatingBanner.nextIndex].xOpacity += .10; // fade in
		
		RotatingBanner.fSetOpacity(RotatingBanner.arrayImgs[RotatingBanner.nextIndex]);
		
		if (RotatingBanner.arrayImgs[RotatingBanner.nextIndex].xOpacity >= .99) {
			// done with fade

			RotatingBanner.currentIndex = RotatingBanner.nextIndex;
	
			RotatingBanner.nextIndex = (RotatingBanner.currentIndex < RotatingBanner.arrayImgs.length - 1) ? RotatingBanner.currentIndex + 1 : 0; // index of next img

		} else {
			RotatingBanner.fadeTimerID = setTimeout(RotatingBanner.fCrossFade,50); // short pause, recurse to continue fade.
		}

	},

	fSetOpacity:function (obj) {
		if (obj.xOpacity > .99) {
			obj.xOpacity = .99;
		}
		obj.style.opacity = obj.xOpacity; // the CSS3 method, for newer Mozilla, Safari, Opera
		obj.style.MozOpacity = obj.xOpacity; // older Mozilla
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ")"; // for IE
	},

	fControl:function (controlParam) {
	},

	initialize:function () {
		if (document.getElementById && document.getElementById("rotating_banner_container")) { // Make sure browser supports getElementById and div "rotating_banner_container" exists

			document.getElementById("rotating_banner_container").className += " javascript_enabled";
	
			// create array of all img nodes
			RotatingBanner.arrayImgs = document.getElementById("rotating_banner_container").getElementsByTagName("img");
			
			// display first img
			RotatingBanner.arrayImgs[RotatingBanner.currentIndex].parentNode.style.zIndex = RotatingBanner.counterZ; // Place first <a> on top

			RotatingBanner.mainTimerID = setTimeout(RotatingBanner.fMainLoop,RotatingBanner.delayMS);
		}
	}
}

addLoadEvent(RotatingBanner.initialize);
