/*//////////////////////////////////////////////////////////////////////////////
// slideshow.js
////////////////////////////////////////////////////////////////////////////////
// javascript routines for running the slideshow
////////////////////////////////////////////////////////////////////////////////
// AUTHOR  : Paul Rees
// CONTACT : dev@page87.com
// CREATED : 12-11-06
// CVS     : $Id$
//////////////////////////////////////////////////////////////////////////////*/
function slide(src, title, objectInfo, customerInfo) 
{
	this.src = src;
	this.title = title;
	this.objectInfo = objectInfo;
	this.customerInfo = customerInfo;
	
	this.timeout = 4000;

	// Create an image object for the slide
	if (document.images) 
		this.image = new Image();

	// flag whether load() has already been called
	this.loaded = false;

	this.load = function() 
	{
		// loads the slide image
		if (! document.images)
			return;

		if (! this.loaded) 
		{
			this.image.src = this.src;
			this.loaded = true;
		}
	}
}

function slideshow(slideshowname) 
{
	this.name = slideshowname;

	// when we reach the last slide, should we loop around to start the slideshow again?
	this.repeat = true;

	// number of images to pre-fetch.
	// -1 = preload all images.
	//  0 = load each image as it is used.
	//  n = pre-fetch n images ahead of the current image.
	this.prefetch = -1;

	// name of the image element on the HTML page.
	this.image = document.images.slideshowImage;

	// IDs of various elements whose text is updated
	this.titleId = 'image-title';
	this.objectInfoId = 'image-object';
	this.customerInfoId = 'image-customer';
	this.slideNumber = 'image-number';
	this.slideCount = 'image-count';

	// private variables
	this.slides = new Array();
	this.current = 0;
	this.timeoutId = 0;

	////////////////////////////////////////////////////////////////////////////
	// Public methods
	////////////////////////////////////////////////////////////////////////////
	this.add_slide = function(slide) 
	{
		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.slides[this.current].timeout;
		this.timeoutId = setTimeout(this.name + ".loop()", timeout);
	}
	////////////////////////////////////////////////////////////////////////////
	this.update = function() 
	{
	    // updates the slideshow image

	    // 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;

		slide.load();
  
		// apply the filters for the image transition (IE)
		if (dofilter)
		{
			this.image.filters[0].Apply();
			this.image.filters[0].Play();
		}

		// update the image.
		crossfade(this.image, slide.image.src, '1', '');

		// call the post-update hook function if one was specified
		if (typeof this.post_update_hook == 'function')
			this.post_update_hook();

		// do we need to pre-fetch images?
		if (this.prefetch > 0) 
		{
			var next, prev, count;

			// pre-fetch the next slide image(s)
			next = this.current;
			prev = this.current;
			count = 0;
			do {
				// get the next and previous slide number
				// loop past the ends of the slideshow if necessary
				if (++next >= this.slides.length) next = 0;
				if (--prev < 0) prev = this.slides.length - 1;

				// preload the slide image
				this.slides[next].load();
				this.slides[prev].load();

				// keep going until we have fetched the designated number of slides

			} while (++count < this.prefetch - 1);
		}
		
		// update the text
		this.display_text();
	}
	////////////////////////////////////////////////////////////////////////////
	this.goto_slide = function(n) 
	{
		// This method jumpts to the slide number specified
		// If you use slide number -1, then it jumps to the last slide.
		// You can use this to make links that go to a specific slide,
		// or to go to the beginning or end of the slideshow.
		// Examples:
		// onClick="myslides.goto_slide(0)"
		// onClick="myslides.goto_slide(-1)"
		// onClick="myslides.goto_slide(5)"
  
		if (n == -1)
			n = this.slides.length - 1;
  
		if (n < this.slides.length && n >= 0)
			this.current = n;
  
		this.update();
	}
	////////////////////////////////////////////////////////////////////////////
	this.next = function()
	{
		// advances to the next slide.

		if (this.current < this.slides.length - 1)
			this.current++;
		else if (this.repeat)
			this.current = 0;

		this.update();
	}
	////////////////////////////////////////////////////////////////////////////
	this.previous = function() 
	{
		// goes to the previous slide.
  
		if (this.current > 0) 
			this.current--;
		else if (this.repeat) 
			this.current = this.slides.length - 1;
    
  
		this.update();
	}
	////////////////////////////////////////////////////////////////////////////
	this.display_text = function()
	{
		// changes the various texts for the current slide - if they have been 
		// set, otherwise they remain as they were
		title = this.slides[ this.current ].title;
		objectInfo = this.slides[ this.current ].objectInfo;
		customerInfo = this.slides[ this.current ].customerInfo;

		if (title)
		{
			x = this.getElementById(this.titleId);
			x.innerHTML = title;
		}
		
		if (objectInfo)
		{
			x = this.getElementById(this.objectInfoId);
			x.innerHTML = objectInfo;
		}
		
		if (customerInfo)
		{
			x = this.getElementById(this.customerInfoId);
			x.innerHTML = customerInfo;
		}
		
		x = this.getElementById(this.slideNumber);
		x.innerHTML = this.current + 1;
	}

	////////////////////////////////////////////////////////////////////////////
	// Private methods
	////////////////////////////////////////////////////////////////////////////
	this.loop = function() 
	{
		// 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 does not advance to the next slide yet.

		// 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 // last slide
			this.next();
    
		// keep playing the slideshow
		this.play( );
	}
	////////////////////////////////////////////////////////////////////////////
	this.valid_image = function() 
	{
		// returns 1 if a valid image has been set for the slideshow
  
		if (!this.image)
			return false;
		else
			return true;
	}
	////////////////////////////////////////////////////////////////////////////
	this.getElementById = function(element_id) 
	{
		// returns the element corresponding to the id

		if (document.getElementById)
			return document.getElementById(element_id);
		else if (document.all)
			return document.all[element_id];
		else if (document.layers)
			return document.layers[element_id];
		else 
			return undefined;
	}
}

