// class to flip between a set of images

// ImageRotate.js
// Show a "slide show" using supplied images
// Tip: to show a random image only once, use Random() with no arguments.
function ImageFlipper(id)
{
	// private fields;
	// the target img element
	this._id = document.getElementById(id);
	// the internal list of images
	this._images = new Array();
	// these are used for slideshow
	this._position = 0;
	// used to indicate a slide show should be stopped
	this._stopSemaphore = true;
	
	// public fields
	// delay between images in milliseconds
	this.Delay = 3000;
	// number of times to rotate, 0 = forever
	this.Times = 0;
	// if true, images will be chosen randomly and total showed will be Times
	// if false, images will be showed in order and the whole set will be showed Times times
	this.Randomize = false;
	// In slide show mode going past the first/last images rolls to other end?
	this.Roll = true;
	
	// Put the image in a href wrapper unless it's already in one, then replace it FireFox needs this)
	var parent = this._id.parentNode;
	if (parent.nodeName == 'A')
	    this._a = parent;
	else
	{
	    this._a = document.createElement('a');
	    parent.insertBefore(this._a, this._id);
	    this._a.appendChild(this._id);
	}
	
	// object used internally to store images
	function _image(image, alt, href)
	{
		this.path = image;
		this.AltText = alt;
		this.href = href;
		// preload it
		this.preload = new Image();
		this.preload.src = this.path;
	}
	
	ImageFlipper.prototype.Add = 
	function Add(imageName, altText, href)
	{
		if(typeof href == 'undefined')
			href = null;
		var image = new _image(imageName, altText, href);
		this._images.push(image);
	}

	ImageFlipper.prototype.Rotate =
	function Rotate(delay, times)
	{
	    if (typeof delay != 'undefined')
	        this.Delay = delay;
	    if (typeof times != 'undefined')
	        this.Times = times;

	    this._stopSemaphore = false;
	    this._rotate(this, 0, 0);
	}

	ImageFlipper.prototype._rotate =
	function _rotate(owner, t, i)
	{
	    // stop if we've been told to do so
	    if (this._stopSemaphore == true)
	        return;

	    if (typeof i == 'undefined') i = 0;
	    if (this.Randomize)
	    {
	        var r = Math.floor(Math.random() * this._images.length);
	        this.Show(r);
	        if (t++ < this.Times || this.Times == 0)
	            setTimeout(function() { owner._rotate(owner, t); }, this.Delay);
	    }
	    else
	    {
	        this.Show(i);
	        if (++i >= this._images.length)
	        {
	            t++;
	            i = 0;
	        }
	        if (t < this.Times || this.Times == 0)
	            setTimeout(function() { owner._rotate(owner, t, i); }, this.Delay);
	    }
	}

    ImageFlipper.prototype.Stop =
	function Stop()
	{
	    this._stopSemaphore = true;
	}

	ImageFlipper.prototype.Show =
	function Show(number)
	{
	    this._id.src = this._images[number].path;
	    this._id.alt = this._images[number].AltText;
	    if (this._images[number].href != null)
	        this._a.setAttribute('href', this._images[number].href);
	}
	
	ImageFlipper.prototype.Random = 
	function Random(delay, times)
	{
		this.Times = 1; // if called with no params, show one random image
		this.Randomize = true;
		this.Rotate(delay, times);
	}
	
	ImageFlipper.prototype.SlideShowStart =
	function SlideShowStart()
	{
		this.Show(this._position);
	}

	ImageFlipper.prototype.SlideShowFirst =
	function SlideShowFirst()
	{
		this._position = 0;
		this.Show(this._position);
	}

	// Go to the next slide in the slide show.
	// Roll to the first slide if Roll is true
	ImageFlipper.prototype.SlideShowNext =
	function SlideShowNext()
	{
		if(this._position == this._images.length - 1)
			if(this.Roll)
				this._position = 0;
			else return;
		this.Show(++this._position);
	}
	
	// Go to the previous slide in the slide show.
	// Roll to the last slide if Roll is true
	ImageFlipper.prototype.SlideShowPrevious =
	function SlideShowPrevious()
	{
		if(this._position == 0)
			if(this.Roll)
				this._position = this._images.length - 1;
			else return;
		this.Show(--this._position);
	}
}