// OPC Rotating banner
// State of California
// 8/4/2009
// Modded by Besenty 4/7/2010 for OPC Banner

var OPCRotatingBanner = {

	//// These values can be customized

	delayMS: 3000, // Delay before displaying next image, in milliseconds
	imageWidth: '936px', // image width
	imageHeight: '285px', // image height

	//// You probably won't need to edit anything below this line

	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 () {

		OPCRotatingBanner.arrayImgs[OPCRotatingBanner.nextIndex].xOpacity = 0; // Set opacity of next image to 0
		OPCRotatingBanner.fSetOpacity(OPCRotatingBanner.arrayImgs[OPCRotatingBanner.nextIndex]);

		OPCRotatingBanner.counterZ++;
		OPCRotatingBanner.arrayImgs[OPCRotatingBanner.nextIndex].parentNode.style.zIndex = OPCRotatingBanner.counterZ; // Place next <a> on top

		OPCRotatingBanner.fCrossFade(); // do fade

		if (OPCRotatingBanner.bPlaying)
			OPCRotatingBanner.mainTimerID = setTimeout(OPCRotatingBanner.fMainLoop,OPCRotatingBanner.delayMS); // delay, recurse
	},

	fCrossFade:function () {
		OPCRotatingBanner.fadeTimerID = null;
		OPCRotatingBanner.arrayImgs[OPCRotatingBanner.nextIndex].xOpacity += .10; // fade in
		
		OPCRotatingBanner.fSetOpacity(OPCRotatingBanner.arrayImgs[OPCRotatingBanner.nextIndex]);
		
		if (OPCRotatingBanner.arrayImgs[OPCRotatingBanner.nextIndex].xOpacity >= .99) {
			// done with fade

			OPCRotatingBanner.currentIndex = OPCRotatingBanner.nextIndex;
	
			OPCRotatingBanner.nextIndex = (OPCRotatingBanner.currentIndex < OPCRotatingBanner.arrayImgs.length - 1) ? OPCRotatingBanner.currentIndex + 1 : -1; // index of next img

		} else {
			OPCRotatingBanner.fadeTimerID = setTimeout(OPCRotatingBanner.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
	},

	initialize:function () {
		if (document.getElementById && document.getElementById("opc_rotating_banner_container")) { // Make sure browser supports getElementById and div "opc_rotating_banner_container" exists

			document.getElementById("opc_rotating_banner_container").className += " javascript_enabled";
	
			// create array of all img nodes
			OPCRotatingBanner.arrayImgs = document.getElementById("opc_rotating_banner_container").getElementsByTagName("img");

			// Set width and height of each image. Fixes the FYP image problem, which has inline width and height styles. Problem occured in IE, click view > text size > larger.
			for (var counterA = 0; counterA < OPCRotatingBanner.arrayImgs.length; counterA++) {
				OPCRotatingBanner.arrayImgs[counterA].style.width = OPCRotatingBanner.imageWidth;
				OPCRotatingBanner.arrayImgs[counterA].style.height = OPCRotatingBanner.imageHeight;
			}
			
			// display first img
			OPCRotatingBanner.arrayImgs[OPCRotatingBanner.currentIndex].parentNode.style.zIndex = OPCRotatingBanner.counterZ; // Place first <a> on top

			OPCRotatingBanner.mainTimerID = setTimeout(OPCRotatingBanner.fMainLoop,OPCRotatingBanner.delayMS);
		}
	}
}

addLoadEvent(OPCRotatingBanner.initialize);

