//
// from http://www.barelyfitz.com/projects/slideshow/
//

function slide(src)
{
	this.src = src;

	if (document.images)
		this.image = new Image();

	this.loaded = false;

	this.load = function()
	{
		if (!document.images)
			return;

		if (!this.loaded)
		{
			this.image.src = this.src;
			this.loaded = true;
		}
	}
}

function slideshow( slideshowname )
{
	this.name = slideshowname;

	// Number of images to pre-fetch.
	// -1 = preload all images.
	//  0 = load each image is it is used.
	//  n = pre-fetch n images ahead of the current image.
	// I recommend preloading all images unless you have large
	// images, or a large amount of images.
	this.prefetch = -1;

	// IMAGE element on your HTML page.
	// For example, document.images.SLIDES1IMG
	this.image;

	// Milliseconds to pause between slides.
	// Individual slides can override this.
	this.timeout = 5000;

	// These are private variables
	this.slides = new Array();
	this.current = 0;
	this.timeoutid = 0;

	//--------------------------------------------------
	// Public methods
	//--------------------------------------------------

	this.add_slide = function(slide)
	{
		// Add a slide to the slideshow.
		// For example:
		// SLIDES1.add_slide(new slide("s1.jpg", "link.html"))

		var i = this.slides.length;

		// Prefetch the slide image if necessary
		if (this.prefetch == -1)
		{
			slide.load();
		}

		this.slides[i] = slide;
	}

	this.play = function(timeout)
	{
		// This method implements the automatically running slideshow.
		// If you specify the "timeout" argument, then a new default
		// timeout will be set for the slideshow.

		// Make sure we're not already playing
		this.pause();

		// If the timeout argument was specified (optional)
		// then make it the new default
		if (timeout)
		{
			this.timeout = timeout;
		}

		// If the current slide has a custom timeout, use it;
		// otherwise use the default timeout
		if (typeof this.slides[ this.current ].timeout != 'undefined')
		{
			timeout = this.slides[ this.current ].timeout;
		}
		else
		{
			timeout = this.timeout;
		}

		// After the timeout, call this.loop()
		this.timeoutid = setTimeout( this.name + ".loop()", timeout);
	}

	// This method stops the slideshow if it is automatically running.
	this.pause = function()
	{
		if (this.timeoutid != 0)
		{
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
	}

	this.update = function()
	{
		// This method updates the slideshow image on the page

		// Make sure the slideshow has been initialized correctly
		if (!this.valid_image())
			return;

		// Convenience variable for the current slide
		var slide = this.slides[ this.current ];

		// Determine if the browser supports filters
		var dofilter = false;
		if (this.image && typeof this.image.filters != 'undefined' && typeof this.image.filters[0] != 'undefined')
			dofilter = true;

		// Load the slide image if necessary
		slide.load();

		// Apply the filters for the image transition
		if (dofilter)
		{
			this.image.filters[0].Apply();
		}

		// Update the image.
		this.image.src = slide.image.src;

		// Play the image transition filters
		if (dofilter)
		{
			this.image.filters[0].Play();
		}
	}


	// This method advances to the next slide.
	this.next = function()
	{
		this.pause();

		// Increment the image number
		if (this.current < this.slides.length - 1)
		{
			this.current++;
		}
		else
		{
			this.current = 0;
		}

		this.update();
	}

	// This method goes to the previous slide.
	this.previous = function()
	{
		this.pause();

		// Decrement the image number
		if (this.current > 0)
		{
			this.current--;
		}
		else
		{
			this.current = this.slides.length - 1;
		}

		this.update();
	}

	//==================================================
	// Private methods
	//==================================================

	// This method is for internal use only.
	// This method gets called automatically by a JavaScript timeout.
	// It advances to the next slide, then sets the next timeout.
	// If the next slide image has not completed loading yet,
	// then do not advance to the next slide yet.
	this.loop = function()
	{
		// Make sure the next slide image has finished loading
		if (this.current < this.slides.length - 1)
		{
			next_slide = this.slides[this.current + 1];
			if (next_slide.image.complete == null || next_slide.image.complete)
			{
				this.next();
			}
		}
		else		// we're at the last slide
		{
			this.next();
		}

		// Keep playing the slideshow
		this.play();
	}

	// Returns 1 if a valid image has been set for the slideshow
	this.valid_image = function()
	{
		if (!this.image)
			return false;
		return true;
	}

	// This method returns the element corresponding to the id
	this.getElementById = function(element_id)
	{
		if (document.getElementById)
			return document.getElementById(element_id);

		if (document.all)
			return document.all[element_id];

		if (document.layers)
			return document.layers[element_id];

		return undefined;
	}
}

