// FILE:  topPageSlideshow.js
// AUTHOR: Adam Yukio Toda
// -------------------
// Program that creates an automatic slideshow of X number of images

/* STATIC GLOBAL VARIABLES */

var TRANS_SPEED = 20;		// Speed of transparency in milliseconds 
var CHANGE_RATE = 2;		// Shades of Transparency change per TRANS_SPEED.
var HIDDEN_OPACITY = 0;
var SHOW_OPACITY = 100;
var INSTANT_CHANGE_RATE = 100;
var NUM_IMGS = 4;
var LENGTH_TO_SHOW_PIC = 10;


/* GLOBAL TIMER LENGTH VARIABLE */

var timerLength;
var viewableDIV;


/* COUNTDOWN TIMER VARIABLES */

var secs;
var timerID = null;
var timerRunning = false;
var delay = 500;		//Delay is in milliseconds

/* ARRAYS */
var imgs = new Array();  	//For Slideshow
var burmaImgs = new Array("burma-bg1.jpg", 
			"burma-bg2.jpg", 
			"LAB-bg1.jpg");
var seAsiaImgs = new Array("seAsia-bg1.jpg", 
			"seAsia-bg2.jpg", 
			"LAB-bg4.jpg");
var bgImgs = new Array("LAB-bg1.jpg", 
			"LAB-bg2.jpg", 
			"LAB-bg3.jpg", 
			"LAB-bg4.jpg");
var thaiImgs = new Array("thai-bg1.jpg",
			"thai-bg2.jpg",
			"thai-bg3.jpg",
			"thai-bg4.jpg");


/* BACKGROUND IMAGE
 * ----------------
 * Decides which background to use based on which section is being viewed.
 */
 
 function InitializeBackgroundImage(directory, sectionName, isIE7) {
 	 
 	 // Run the slideshow if this is the top page
 	 if (sectionName == OVERVIEW) {
 	 	 RunSlideshow(directory, isIE7);
 	 
 	 // If this is the About section
 	 } else if (sectionName == ABOUT) {
 	 	ChooseRandomImage(bgImgs, directory, isIE7);
 	 
 	 // If this is the Burma Section
 	 } else if (sectionName.substring(0,5) == BURMA || sectionName.substring(0,5) == "Burma") { 	
 	 	 ChooseRandomImage(burmaImgs, directory, isIE7);
 	 
 	 // If this is the Southeast Asia Section
 	 } else if (sectionName.substring(0,6) == SE_ASIA || sectionName.substring(0,5) == "South") {
 	 	ChooseRandomImage(seAsiaImgs, directory, isIE7);
 	 
 	 // If this is the Thailand Section
 	 } else if (sectionName.substring(0,8) == THAI || sectionName.substring(0,4) == "Thai") {
 	 	ChooseRandomImage(thaiImgs, directory, isIE7);
 	 
 	 // If this is the Contact Page
 	 } else if (sectionName == CONTACT) {
 	 	ChooseRandomImage(bgImgs, directory, isIE7);
 	 
 	 // For any other section of the site
 	 } else {
 	 	ChooseRandomImage(bgImgs, directory, isIE7);	 
 	 }
 }
 
 function ChooseRandomImage(imgArray, directory, isIE7) {
 	var imgNumber = Math.floor(Math.random() * imgArray.length);
 	
 	// Hide the DIVs used for the front page slideshow
 	getObject("backgroundIMG1").style.display = "none";
 	getObject("backgroundIMG2").style.display = "none";
 	getObject("backgroundIMG3").style.display = "none";
 	getObject("backgroundIMG4").style.display = "none";
 	
 	// Apply the background
 	if (isIE7) {
 		getObject("staticBackgroundIMG").style.background = "#e2e2e2 url('"+ directory + "/images/" + imgArray[imgNumber] + "') no-repeat center top";
 	} else {
 		document.body.style.background = "#e2e2e2 url('"+ directory + "/images/" + imgArray[imgNumber] + "') no-repeat center top";
 	}
 	
 	// OLD BACKGROUND CODE
 	/* getObject("backgroundIMG4").style.background = " url('"+ directory + "/images/" + imgArray[imgNumber] + "') top center no-repeat"; */	 
 }
 
 function SetBackgroundImage(image, directory) {
 	getObject("backgroundIMG4").style.background = "url('"+ directory + "/images/" + image + "') top center no-repeat";	 
 }


/* TIMER CODE
 * ----------
 * Timer that delays the fading process.  Fading process starts when timer runs out.
 *
 * code courtesy of http://www.geocities.com/technofundo/tech/js/showhide.html
 * and http://www.mcfedries.com/JavaScript/timer.asp
 */

function RunSlideshow(directory, isIE7) {
	HideOtherBackgroundImages(isIE7);
	InitializeTimer(directory);
	StartTheTimer();	
} 

function HideOtherBackgroundImages(isIE7) {
	document.body.style.background = "#e2e2e2";
	if (isIE7) getObject("staticBackgroundIMG").style.background = "#e2e2e2";
}
 
function InitializeTimer(directory) {
    // Set the length of the timer, in seconds
    timerLength = (LENGTH_TO_SHOW_PIC*NUM_IMGS) + LENGTH_TO_SHOW_PIC/2;
    secs = timerLength;
   
    LoadImageObjects(directory);
    StopTheClock();
}

function LoadImageObjects(directory) {
	
	// The last image is shown first, so it should be set as opaque
	for (var i=1; i < NUM_IMGS + 1; i++) {
		if (i == NUM_IMGS) imgs[i] = new image("backgroundIMG" + i, SHOW_OPACITY);
		else imgs[i] = new image("backgroundIMG" + i, HIDDEN_OPACITY);
		
		// Load the image into each background DIV
		getObject(imgs[i].id).style.display = "inline";
		getObject(imgs[i].id).style.background = "url('"+ directory + "/images/LAB-bg" + i + ".jpg') top center no-repeat";
	}
}

function StartTheTimer() {
	
	// Shows pictures in reverse order, starting from the highest numbered picture.
	if (secs % LENGTH_TO_SHOW_PIC == 0 && secs != 0 && secs/LENGTH_TO_SHOW_PIC <= NUM_IMGS) {
		var imgNum = secs/LENGTH_TO_SHOW_PIC;
	
		//alert(getObject(imgs[imgNum].id).style.background);
		// Pushes the current image to a back layer to allow the next image to be seen
		getObject(imgs[imgNum].id).style.zIndex = 1;
		
		// If this is not the last image
		if (imgNum - 1 >= 1) {
			getObject(imgs[(imgNum-1)].id).style.zIndex = 2;
			window.clearInterval(imgs[(imgNum-1)].timer);
			// hides previous image
			imgs[imgNum].timer = setInterval("imgs["+ imgNum +"].changeOpacity(" + HIDDEN_OPACITY + ", " + CHANGE_RATE + ")", TRANS_SPEED);
			// shows current images
			imgs[(imgNum-1)].timer = setInterval("imgs["+ (imgNum-1) +"].changeOpacity(" + SHOW_OPACITY + ", " + CHANGE_RATE + ")", TRANS_SPEED);
			
		// If this is the last image in the slideshow
		} else {
			window.clearInterval(imgs[NUM_IMGS].timer);
			// hides last image
			imgs[1].timer = setInterval("imgs["+ 1 +"].changeOpacity(" + HIDDEN_OPACITY + ", " + CHANGE_RATE + ")", TRANS_SPEED);
			// shows the first image
			imgs[NUM_IMGS].timer = setInterval("imgs["+ NUM_IMGS +"].changeOpacity(" + SHOW_OPACITY + ", " + CHANGE_RATE + ")", TRANS_SPEED);
			getObject(imgs[NUM_IMGS].id).style.zIndex = 2;
		}

	}
	if (secs==0) {
		// Starts the slideshow over again from the beginning
		secs = timerLength - LENGTH_TO_SHOW_PIC/2;
		timerID = self.setTimeout("StartTheTimer()", delay);
		//StopTheClock();
    	} else {
        	secs = secs - .5;
        	timerRunning = true;
        	timerID = self.setTimeout("StartTheTimer()", delay);
    	}
}

function StopTheClock() {
    	if(timerRunning)  clearTimeout(timerID);
    	timerRunning = false;
}


/* FUNCTION getObject
 * ------------------
 * Helper function used to retrieve objects in web space.  Shorter form
 * of using the document.getElementById command.
 */

function getObject(obj) {
	if (document.getElementById) return document.getElementById(obj);
	else if (document.all) return document.all[obj];
}


/* END OF JAVASCRIPT CODE */

