/* ------------------------------------------------------------------------------- +

	File:		custom.js
	Author:		Ben Nadel
	Date:		February 05, 2005
	Desc:		These are functions that are custom to this application and no
				where else.

+ ------------------------------------------------------------------------------- */

// This the SlideShow class object 
function SlideShowObj(strImageName, strTextName, intIntervalTime, strInstanceName){
	this.ImageName = strImageName;
	this.TextName = strTextName;
	this.IntervalTime = intIntervalTime;
	this.Interval = null;
	this.InstanceName = strInstanceName;
	this.Slides = new Array();
	this.SlideIndex = 0;
	this.Stopped = true;
}


// This adds a slide to the slide show object. It does not load the 
// slide yet, just places it in the array.
SlideShowObj.prototype.AddImage = function(strImageSrc, strDescription){
	var objImage = new Object();
	
	// Set up this image object 
	objImage.Src = strImageSrc;
	objImage.Img = null;
	objImage.Text = strDescription;
	
	// Add the image object back to the slides array
	this.Slides[this.Slides.length] = objImage;
} 


// This preloads a slide at the given index 
SlideShowObj.prototype.PreloadImage = function(intIndex){
	// Check to make sure we are in a valid index
	if ((0 <= intIndex) && (intIndex < this.Slides.length)){
	
		// Check to see if the image has already been loaded
		if (this.Slides[intIndex].Img == null){
			this.Slides[intIndex].Img = new Image();
			this.Slides[intIndex].Img.src = this.Slides[intIndex].Src;
		}
		
	}
}


// Set the current slide
SlideShowObj.prototype.SetSlide = function(){
	var objImage = document.images[this.ImageName];
	var objText = document.getElementById(this.TextName);
	
	// Clear the interval 
	clearInterval(this.Interval);

	// Preload the current image
	this.PreloadImage(this.SlideIndex);
	
	// Check to see if the image is loaded 
	if (this.Slides[this.SlideIndex].Img.complete){
	
		// The image has loaded, so set it in the document
		objImage.src = this.Slides[this.SlideIndex].Img.src;
		
		// Replace the text if we have an node
		if (objText){
			objText.replaceChild(document.createTextNode(this.Slides[this.SlideIndex].Text), objText.childNodes[0]);
		}
		
		// Check to see if there is a next image to preload
		if (this.SlideIndex < (this.Slides.length - 1)){
			this.PreloadImage(this.SlideIndex + 1);
		}
		
		// Check to see if we are running the slide show. If so, call the next shortly
		if (!this.Stopped){
			this.Interval = setInterval(this.InstanceName + ".Next()", this.IntervalTime);
		}
	
	} else {
	
		// The slide has not yet loaded, so call this function again shortly
		this.Interval = setInterval(this.InstanceName + ".SetSlide()", 50);
		
	}
}


// This calls the next image
SlideShowObj.prototype.Next = function(){
	// Increment the slide
	this.IncrementSlideIndex(1);
	
	// Set the slide image
	this.SetSlide();
}


// This calls the prev image
SlideShowObj.prototype.Prev = function(){
	// Increment the slide
	this.IncrementSlideIndex(-1);
	
	// Set the slide image
	this.SetSlide();
}


// This increments the current slide index
SlideShowObj.prototype.IncrementSlideIndex = function(intCount){
	// Check to see if we are in bounds
	if ((this.SlideIndex + intCount) < 0){
		this.SlideIndex = (this.Slides.length - 1);
	} else if ((this.SlideIndex + intCount) > (this.Slides.length - 1)){
		this.SlideIndex = 0;
	} else {
		this.SlideIndex = (this.SlideIndex + intCount);
	}
}


// Starts the slide show
SlideShowObj.prototype.Start = function(){
	// Set this slide show as running
	this.Stopped = false;
	
	// Set the interval for slide change
	this.Next();
}


// Start on a random image
SlideShowObj.prototype.StartRandom = function(){
	// Set the random index
	this.SlideIndex = Math.round(Math.random() * this.Slides.length);
	
	// Start the slide show
	this.Start();
}


// This stops the slide show
SlideShowObj.prototype.Stop = function(){
	// Set the slide show as stopped
	this.Stopped = true;
	
	// Clear the interval
	clearInterval(this.Interval);
}
