// FILE:  topPageSlideshow.js
// AUTHOR: Adam Yukio Toda

/* 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 = 99;
var INSTANT_CHANGE_RATE = 99;
var NUM_IMGS = 5;
var TIMER_LENGTH = 36;
var LENGTH_TO_SHOW_PIC = 6;


/* GLOBAL TIMER VARIABLES */

var img1Timer;
var img2Timer;
var img3Timer;
var img4Timer;


/* GLOBAL OPACITY VARIABLES */

var img1opacity = 100;
var img2opacity = 0;
var img3opacity = 0;
var img4opacity = 0;
var img5opacity = 0;


/* COUNTDOWN TIMER VARIABLES */

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

/* ARRAYS */
var imgs = new Array();


/* 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() {
	InitializeTimer();
	StartTheTimer();
	
	
		/*
		for (var i=1; i <= 2; i++) {
			if (i =1) { 
				getObject(imgs[NUM_IMGS].id).style.zindex = 1;
				if (imgs[NUM_IMGS].opacity != HIDDEN_OPACITY) imgs[NUM_IMGS].timer = setInterval("imgs[" + i + "].changeOpacity(" + HIDDEN_OPACITY + ", " + INSTANT_CHANGE_RATE + ")", TRANS_SPEED);
			} else {
				getObject(imgs[i - 1].id).style.zindex = 1;
				imgs[i - 1].timer = setInterval("imgs[" + i + "].changeOpacity(" + HIDDEN_OPACITY + ", " + INSTANT_CHANGE_RATE + ")", TRANS_SPEED);
			}
			getObject(imgs[i].id).style.zindex = 10;
			imgs[i].timer = setInterval("imgs[" + i + "].changeOpacity(" + SHOW_OPACITY + ", " + CHANGE_RATE + ")", TRANS_SPEED);	
			pausecomp(500);
		} */
	
} 
 
function InitializeTimer() {
    // Set the length of the timer, in seconds
    secs = TIMER_LENGTH;
    LoadImageObjects();
    StopTheClock();
}

function LoadImageObjects() {
	for (var i=1; i < NUM_IMGS + 1; i++) {
		if (i == 5) imgs[i] = new image("topPagePainting" + i, SHOW_OPACITY);
		else imgs[i] = new image("topPagePainting" + i, HIDDEN_OPACITY);
	}
}

function StartTheTimer() {
	
	if (secs % LENGTH_TO_SHOW_PIC == 0 && secs != 0 && secs/LENGTH_TO_SHOW_PIC <= NUM_IMGS) {
		var imgNum = secs/LENGTH_TO_SHOW_PIC;
		
		// 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 the first image in the slideshow
		if (imgNum - 1 >= 1) {
			getObject(imgs[(imgNum-1)].id).style.zindex = 10;
			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);
			// 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 = 10;
		}
	}
	if (secs==0) {
		// Starts the slideshow over again from the beginning
		secs = TIMER_LENGTH - LENGTH_TO_SHOW_PIC;
		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 pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
} 


/* 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 */

