// JavaScript Document

// Global variables
var gNumSlides = 0;
var gCurrentSlide = 0;
var gOldSlide = 0;

var gSlides = new Array();

function Slideshow(initialWait, followingWait, timerSpeed, numAnimationFrames)
{
	this.initialWait = new Number(initialWait);			// The itnitial wait time in millisecond
	this.followingWait = new Number(followingWait);		// The following wait time in millisecond
	this.timerSpeed = new Number(timerSpeed);			// The animation interval in millisecond
	this.numAnimationFrames = new Number(numAnimationFrames);	// Number of the animation frames
	this.currentAnimationFrame = 0;
	this.angle = 180;
	this.cosValue = -1.0;	// cosine for the given angle.
	this.sinValue = 0.0;	// sine for the given angle.
}

var gSlideshow = new Slideshow("6000", "4000", "10", "20");

function setSlideshowParameter(name)
{
	var len = 0;
	var str = null;
	var val = null;
	var obj = document.getElementById(name);
	if(obj != null) {
		var len = obj.style.width.length;
		var str = obj.style.width.substring(0, len-2);	// Remove "px" from the string.
		var val = new Number(str);
		if(val != null) {
			if(name == "ssData_InitialWait")
				gSlideshow.initialWait = val;
			else if(name == "ssData_FollowingWait")
				gSlideshow.followingWait = val;
			else if(name == "ssData_TimerSpeed")
				gSlideshow.timerSpeed = val;
			else if(name == "ssData_NumAnimationFrames")
				gSlideshow.numAnimationFrames = val;
		}
	}
}

function InitSlideshowParameters() {
	setSlideshowParameter("ssData_InitialWait");
	setSlideshowParameter("ssData_FollowingWait");
	setSlideshowParameter("ssData_TimerSpeed");
	setSlideshowParameter("ssData_NumAnimationFrames");
}

//-------------------------------------------------------------------------------------------
// MakeSlideShow
//
// This sets up the slide show images.
//-------------------------------------------------------------------------------------------
function MakeSlideShow() {
	// Intitialize slideshow parameters.
	InitSlideshowParameters();
	
	// Find all images with the class "slide"
	var imgs = document.getElementsByTagName("img");
	
	var numSlides = 0;
	for(var i = 0; i < imgs.length; i++) {
		if(imgs[i].className != "slide")
			continue;
		
		gSlides[numSlides] = imgs[i];
		
		// Stack images with first slide on top.
		if(numSlides == 0) {
			imgs[i].style.zIndex = 10;
		}
		else {
			imgs[i].style.zIndex = 0;
		}
		imgs[i].onclick = NextSlide;
		numSlides++;
	}
	
	gNumSlides = numSlides;

	// Set the last slide as the old slide and the first one to the current slide.
	gOldSlide = gNumSlides - 1;
	gCurrentSlide = 0;
	
	window.setTimeout("NextSlide();", gSlideshow.initialWait);	
}

//-------------------------------------------------------------------------------------------
// NextSlide
//
// Sets the current slide to the old slide and make the next slide to the current slide.
// This uses setOpacity() from prototype.js.
//-------------------------------------------------------------------------------------------
function NextSlide() {
	// Move older slide to the bottom of the stack
	gSlides[gOldSlide].style.zIndex = 0;
	
	// The current slide becomes the old slide.
	gOldSlide = gCurrentSlide;

	// Keep the old slide as a base
	gSlides[gOldSlide].style.zIndex = 9;
	gSlides[gOldSlide].setOpacity(1.0);
	gSlides[gOldSlide].style.left = "0px";
	
	// Get the next slide.
	gCurrentSlide++;
	if(gCurrentSlide >= gNumSlides)
		gCurrentSlide = 0;
	
	// Make the new slide to the top
	gSlides[gCurrentSlide].style.zIndex = 10;
	gSlides[gCurrentSlide].setOpacity(0.0);
	
	// Slide the current slide to the left
	gSlideshow.currentAnimationFrame = 0;
	window.setTimeout("AnimateSlide();", gSlideshow.followingWait);
}

function AnimateSlide() {
	// Get the width of the current slide.
	var w = gSlides[gCurrentSlide].width;
	var h = gSlides[gCurrentSlide].height;
	var n = gSlideshow.currentAnimationFrame;
	var nmax = gSlideshow.numAnimationFrames;
	
	var opacity = 1.0*(n + 1)/nmax;
	opacity = opacity > 1 ? 1.0: (opacity > 0 ? opacity: 0);
	gSlides[gCurrentSlide].setOpacity(opacity);

	var left = (opacity - 1.0)*(opacity - 1.0)*w*gSlideshow.cosValue;
	var top = (opacity - 1.0)*(opacity - 1.0)*h*gSlideshow.sinValue;
	gSlides[gCurrentSlide].style.left = left.toString() + "px";
	gSlides[gCurrentSlide].style.top = top.toString() + "px";
	
	n++;
	if(n < nmax) {
		gSlideshow.currentAnimationFrame++;
		window.setTimeout("AnimateSlide();", gSlideshow.timerSpeed);
	}
	else {
		gSlideshow.currentAnimationFrame = 0;
		gSlideshow.angle = 360*Math.random();	// Set the next angle.
		var theta = Math.PI*gSlideshow.angle/180;
		gSlideshow.cosValue = Math.cos(theta);
		gSlideshow.sinValue = Math.sin(theta);

		NextSlide();
	}
}

// Call MakeSlideShow() each time a new page is loaded.
// MakeSlideShow is called in DWStyle.js.
//window.onload = MakeSlideShow;
