
function slideShow ( viewerImageName, secsPerImage, onChangeSlide )
{
	this.viewerImageName	= viewerImageName;
	this.msPerImage			= secsPerImage != null ? secsPerImage * 1000 : null;
	this.timerValue         = this.msPerImage;
	this.onChangeSlide      = onChangeSlide;
	
	this.slides				= new Array();	
	this.currentImage		= 0;
	this.allLoaded          = 0;
	this.timer              = null;
		
	return this;
}

slideShow.prototype.imageLoaded = function ( imageUrl )
{
	var index;
	var allLoaded = 1

	for ( index = 0; index < this.slides.length; index++ )
	{
		if ( this.slides [ index ] .url == imageUrl )
		{
			this.slides [ index ] .loaded = 1;
		}

		if ( ! this.slides [ index ] .loaded )
		{
			allLoaded = 0;
		}
	}

	this.allLoaded = allLoaded;
}

slideShow.prototype.addImage = function ( imageUrl )
{	
	this.slides [ this.slides.length ] = new Object ( );
	this.slides [ this.slides.length - 1 ] .url = imageUrl;
	this.slides [ this.slides.length - 1 ] .loaded = 0;
}


slideShow.prototype.goToSlide = function ( index )
{
	this.currentImage = index;
	
	if ( this.currentImage < 0 )
	{
		this.currentImage = this.slides.length - 1;
	}
	else if (this.currentImage == this.slides.length) 
	{
		this.currentImage = 0;
	}

	if ( this.onChangeSlide != null )
	{
		this.onChangeSlide ( this );
	}
	
	document.images[this.viewerImageName].src = this.slides[this.currentImage].url;	
}


slideShow.prototype.changeSlide = function ( offset )
{
	// first stop the timer so it doesn't trigger 
	// anything while this handling is in process

	if ( this.timer )
	{
		clearTimeout(this.timer);
		this.timer = null;
	}
	
	this.goToSlide ( this.currentImage + offset );
}

slideShow.prototype.handleTimer = function ( )
{
	if ( this.allLoaded )
	{
		this.timerValue = this.msPerImage;
		this.changeSlide ( 1 );
	}
	else
	{
		this.timerValue = 500;
	}

	this.myStartTimer ( );
}

slideShow.prototype.myStartTimer = function ( )
{
	if ( this.timer )
	{
		clearTimeout(this.timer);
		this.timer = null;
	}

	this.timer = setTimeout( "theSlideShow.handleTimer( )", this.timerValue );
}

