var SlideShow = {
	//delayMS: 9000, // Delay before displaying next image, in milliseconds
	delayMS: 20000, // 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 () {

		for(counterI = 0; counterI < arrayImgs.length; counterI++) {
			document.getElementById("slide_show_control_" + (counterI+1)).className = "";
		}
		document.getElementById("slide_show_control_" + (SlideShow.nextIndex+1)).className = "slide_show_control_highlight";

		arrayImgs[SlideShow.nextIndex].xOpacity = 0; // Set opacity of next image to 0
		SlideShow.fSetOpacity(arrayImgs[SlideShow.nextIndex]);

		SlideShow.counterZ++;
		arrayImgs[SlideShow.nextIndex].parentNode.style.zIndex = SlideShow.counterZ; // Place next <a> on top

		SlideShow.fCrossFade(); // do fade

		if (SlideShow.bPlaying)
			SlideShow.mainTimerID = setTimeout(SlideShow.fMainLoop,SlideShow.delayMS); // delay, recurse
	},

	fCrossFade:function () {
		SlideShow.fadeTimerID = null;
		arrayImgs[SlideShow.nextIndex].xOpacity += .05; // fade in
		
		SlideShow.fSetOpacity(arrayImgs[SlideShow.nextIndex]);
		
		if (arrayImgs[SlideShow.nextIndex].xOpacity >= .99) {
			// done with fade

			SlideShow.currentIndex = SlideShow.nextIndex;
	
			SlideShow.nextIndex = (SlideShow.currentIndex < arrayImgs.length - 1) ? SlideShow.currentIndex + 1 : 0; // index of next img

		} else {
			SlideShow.fadeTimerID = setTimeout(SlideShow.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) {
		if (controlParam == "prev"){
			clearTimeout (SlideShow.mainTimerID);
			if (SlideShow.fadeTimerID) {
				clearTimeout (SlideShow.fadeTimerID);
				SlideShow.nextIndex = (SlideShow.nextIndex > 0) ? SlideShow.nextIndex - 1 : arrayImgs.length - 1; // index of prev img
			} else {
				SlideShow.nextIndex = (SlideShow.currentIndex > 0) ? SlideShow.currentIndex - 1 : arrayImgs.length - 1; // index of prev img
			}
			SlideShow.fMainLoop();
		} else if (controlParam == "play"){
			SlideShow.bPlaying = !SlideShow.bPlaying;
			if (SlideShow.bPlaying) {
				document.getElementById("slide_show_control_play").style.backgroundPosition = "0px -82px"; // show pause icon
				SlideShow.fMainLoop();
			} else {
				document.getElementById("slide_show_control_play").style.backgroundPosition = "0px -56px"; // show play icon
				clearTimeout (SlideShow.mainTimerID);
			}
		} else if (controlParam == "next"){
			clearTimeout (SlideShow.mainTimerID);
			if (SlideShow.fadeTimerID) {
				clearTimeout (SlideShow.fadeTimerID);
				SlideShow.currentIndex = SlideShow.nextIndex;
				SlideShow.nextIndex = (SlideShow.nextIndex < arrayImgs.length - 1) ? SlideShow.nextIndex + 1 : 0; // index of next img
			}
			SlideShow.fMainLoop();
		} else {
			if (SlideShow.fadeTimerID) {
				clearTimeout (SlideShow.fadeTimerID);
			}
			SlideShow.nextIndex = controlParam - 1;
			clearTimeout (SlideShow.mainTimerID);
			SlideShow.fMainLoop();
		}
	},

	initialize:function () {

		if (document.getElementById && document.getElementById("slide_show_container")) { // Make sure browser supports getElementById and div "slide_show_container" exists

			var newAnchor;

			document.getElementById("slide_show_container").className += " javascript_enabled";
	
			// create array of all img nodes
			arrayImgs = document.getElementById("slide_show_container").getElementsByTagName("img");

			// append a linked number for each image
			for(counterI = 0; counterI < arrayImgs.length; counterI++) {
				newAnchor = document.createElement('a');
				newAnchor.appendChild(document.createTextNode(counterI + 1)); // insert the image number as text
				newAnchor.href = "#";
				newAnchor.onclick = new Function("SlideShow.fControl(" + (counterI + 1) + ");this.blur();return false;"); // added blur to remove outlines
				newAnchor.id = "slide_show_control_" + (counterI + 1);
				document.getElementById('slide_show_controls').appendChild(newAnchor);
			}

			document.getElementById("slide_show_control_" + (SlideShow.currentIndex+1)).className = "slide_show_control_highlight";
			
			// display first img
			arrayImgs[SlideShow.currentIndex].parentNode.style.zIndex = SlideShow.counterZ; // Place first <a> on top

			SlideShow.mainTimerID = setTimeout(SlideShow.fMainLoop,SlideShow.delayMS);
		}
	}
}

addLoadEvent(SlideShow.initialize);



