//----------------------------------------------------------------------
// You must define a global var and also setSlideShowTimeout().
//
// Example:
//   var gallery = new SlideShow( idImgElem, imagePath, idCaptionElem, ms );
// 
//   function setSlideShowTimeout()
//   {
//     if ( gallery ) {
//       gallery.next();
//       gallery.start();  // call setSlideShowTimeout() again or stops when condition is met
//     }
//   }
//
// Methods provided:
//   add( fname, caption )
//   advance( n )
//   show( n )  // advance() and show
//   first(), previous(), next(), last()  // manual navigation (stops running slideshow)
//   start( loop ), stop(), continueLoop( loop )  // slideshow control
//----------------------------------------------------------------------
// Revision History:
//   8/3/2008: [Will Chio] Created.
//----------------------------------------------------------------------

var LOOP_ONCE = 1;
var LOOP_FOREVER = 2;

var FIRST_SLIDE = 0;
var PRIOR_SLIDE = -1;
var NEXT_SLIDE = 99;
var LAST_SLIDE = 100;

//----------------------------------------------------------------------

function SlideShow( idImgElem, imagePath, idCaptionElem, ms )
{
  // Required...
  this.imgElem = document.getElementById( idImgElem );
  this.imagePath = imagePath;
  this.captionElem = document.getElementById( idCaptionElem );

  // Default, may override...
  this.slideInterval = ( !ms || ms < 1000 || ms > 10000 )? 5000 : ms;
  
  // Private...
  this.arraySlides = []; 
  this.currSlideIndex = -1;
  this.isRunning = false;
  this.windowTimer = null;
  this.autoCount = 0;
}

//----------------------------------------------------------------------

SlideShow.prototype.add = function( fname, caption )
{
  this.arraySlides.push( [ fname, ( (caption)? caption : "" ) ] );
}

//----------------------------------------------------------------------

SlideShow.prototype.advance = function( n )
{
  switch( n )
  {
    case FIRST_SLIDE :
      this.currSlideIndex = 0;
      break;

    case PRIOR_SLIDE :
      this.currSlideIndex = ( this.currSlideIndex-1 < 0 )? this.arraySlides.length-1 : this.currSlideIndex-1;
      break;
    
    case LAST_SLIDE :
      this.currSlideIndex = this.arraySlides.length - 1;
      break;

    case NEXT_SLIDE :
      this.currSlideIndex = ( this.currSlideIndex+1 == this.arraySlides.length )? 0 : this.currSlideIndex+1;
      break;

    default:
      if( n >= 0 && n < this.arraySlides.length )
        this.currSlideIndex = n;

      break;
  }
}

//----------------------------------------------------------------------

SlideShow.prototype.show = function( n )
{
  if( this.imgElem ) {
    this.advance( n );

    if( this.arraySlides.length && this.arraySlides[this.currSlideIndex].length ) {
      this.imgElem.src = this.imagePath + "/" + this.arraySlides[this.currSlideIndex][0];

      if( this.captionElem )
        this.captionElem.innerHTML = this.arraySlides[this.currSlideIndex][1];
    }
  }
}

//----------------------------------------------------------------------

SlideShow.prototype.first = function()
{
  this.stop();
  this.show( FIRST_SLIDE );
}

//----------------------------------------------------------------------

SlideShow.prototype.previous = function()
{
  this.stop();
  this.show( PRIOR_SLIDE );
}

//----------------------------------------------------------------------

SlideShow.prototype.next = function()
{
  this.stop();
  this.show( NEXT_SLIDE );
}

//----------------------------------------------------------------------

SlideShow.prototype.last = function()
{
  this.stop();
  this.show( LAST_SLIDE );
}

//----------------------------------------------------------------------

SlideShow.prototype.start = function( loop )
{
  if( !this.isRunning && window.setSlideShowTimeout ) {
    this.isRunning = true;
    this.autoCount = 0;
  }

  if( this.isRunning ) {
    if( this.continueLoop( loop ) )
      this.windowTimer = window.setTimeout( "setSlideShowTimeout()", this.slideInterval );
    else
      this.stop();
  }
}

//----------------------------------------------------------------------
// 
// SlideShow.prototype.restart = function()
// {
//   if( !this.isRunning && window.setSlideShowTimeout )
//     this.isRunning = true;
// 
//   this.windowTimer = window.setTimeout( "setSlideShowTimeout()", this.slideInterval );
// }
// 
//----------------------------------------------------------------------
// 
// SlideShow.prototype.pause = function()
// {
//   this.isRunning = false;
//   window.clearTimeout( this.windowTimer );
// }
// 
//----------------------------------------------------------------------

SlideShow.prototype.stop = function()
{
  if( this.isRunning ) {
    this.isRunning = false;
    window.clearTimeout( this.windowTimer );
  }
}

//----------------------------------------------------------------------

SlideShow.prototype.continueLoop = function( loop )
{
  if( loop && loop == LOOP_FOREVER )
    return true;
  else {
    this.autoCount++;
    return( this.autoCount < this.arraySlides.length );
  }
}

//----------------------------------------------------------------------


