/*
	JavaScript image preloader script:
	This script and the slideshow script that calls it are based on a script by Guyon Roche, guyonroche@silver-daggers.co.uk.
	We've modified it in countless ways, but the primary change enables the slideshow to start playing
	once the first image has loaded, rather than having to wait for all of the images to load.
	Then the script waits (politely and sequentially) for the each image before attempting to display it.
	If you have questions about the slideshow scripts, please contact Craig Farris, cfarris@ucdavis.edu.
*/

function ImagePreloader(theImage, index, callback)
{
	// store the callback
	this.callback = callback;
	this.index = index;

	this.preload(theImage);
}

ImagePreloader.prototype.preload = function(image)
{
	// create new Image object and save it
	this.oImage = new Image;
	this.bLoaded = false;
	this.bError = false;
	this.bAbort = false;
	// assign pointer back to this.
	var oImagePreloader = this;
	
	// set up event handlers for the Image object
	this.oImage.onload = function()
	{
		oImagePreloader.bLoaded = true;
		oImagePreloader.onComplete();
	}
	
	this.oImage.onerror = function()
	{
		oImagePreloader.bError = true;
		oImagePreloader.onComplete();
	}

	this.oImage.onabort = function()
	{
		oImagePreloader.bAbort = true;
		oImagePreloader.onComplete();
	}

	// assign the .src property of the Image object
	this.oImage.src = image;
}

ImagePreloader.prototype.onComplete = function()
{
	this.callback(this.index, this.oImage, this.bLoaded);
}