/*

   slideshow.js
   ben miller : ben@hyl.co.uk : http://digital.hyl.co.uk
   v2.0 : 2/18/2004

   - Objects and helper functions for DHTML slide show.

   - Object "SlideShow" defines a collection of slides and
     methods to display them.

   - Object "Slide" defines a slide.

   - Function "InitSlideShows" calls "LoadSlideShow" method
     on each slide show object in "SlideShowCollection" array.
     Should be loaded into window.onload function.

   - Markup should have at the very least an image
     to act as "slideImage"

*/



// -- Slide Show object --------------------------------------------------

function SlideShow(
  _slides,     // array   : slides array
  _imgPlay,    // string  : image url for play button
  _imgStop,    // string  : image url for stop button
  _duration,   // int     : amount of time in thousandsth's of a second each slide is displayed
  _action,     // string  : initial command for slide show. can be 'stop', 'start' or 'random'
  _loop,       // boolean : slide show should repeat after last slide. only if action is 'start'
  _slideIndex, // int     : slide to start on
  _cssFilter,  // string  : style transition. IE only
  _link,       // string  : default url for image
  _title       // string  : default text for image
  )
{
  this.index       = SlideShowCollection.length;
  this.slides      = _slides;
  this.imgPlay     = new Image();
  this.imgPlay.src = isUndefined(_imgPlay)?'':_imgPlay;
  this.imgStop     = new Image();
  this.imgStop.src = isUndefined(_imgStop)?_imgPlay:_imgStop;
  this.duration    = isUndefined(_duration)?4000:_duration;
  this.action      = ((_action=='stop')||(_action=='start')||(_action=='random'))?_action:'start';
  this.loop        = isUndefined(_loop)?true:_loop;
  this.slideIndex  = isUndefined(_slideIndex)?0:((_slideIndex<0)||(_slideIndex>this.slides.length))?0:_slideIndex;
  this.cssFilter   = isUndefined(_cssFilter)?'':_cssFilter;
  this.link        = isUndefined(_link)?'':_link;
  this.title       = isUndefined(_title)?'':_title;
  this.playing     = false;
  this.timeout     = 0;

  this.slideImage  = null;
  this.btnPlayStop = null;
  this.slideTitle  = null;

  SlideShowCollection[this.index] = this;

  this.LoadSlideShow = function()
  {
    if(this.action=='start')
    {
      // hack
      this.slideIndex = (this.slideIndex==0)?this.slides.length-1:this.slideIndex-1;
    }
    else if(this.action=='random')
    {
      this.slideIndex = Math.round((this.slides.length-1)*(Math.random()));
    }

    if(this.action!='stop')
    {
      this.TogglePlayStop();
    }
  }

  this.TogglePlayStop = function()
  {
    this.playing?this.StopSlide():this.PlaySlide();
  }

  this.StopSlide = function()
  {
    if(this.btnPlayStop!=null)
    {
      this.btnPlayStop.src = this.imgPlay.src;
      this.btnPlayStop.alt = 'play';
    }

    this.playing = false;

    this.ClearSlideTimeout();
  }

  this.PlaySlide = function()
  {
    if(this.btnPlayStop!=null)
    {
      this.btnPlayStop.src = this.imgStop.src;
      this.btnPlayStop.alt = 'stop';
    }

    this.playing = true;

    this.ChangeSlide();
  }

  this.ChangeSlide = function()
  {
    var _action = isUndefined(arguments.length)?(this.action=='random')?'random':'':arguments[0];

    if((!_action=='')&&(isInt(_action)))
    {
      if((_action>0)&&(_action<this.slides.length))
      {
        this.slideIndex = _action;
      }
    }
    else
    {
      switch(_action)
      {
        case 'first':
        {
          this.slideIndex = 0;
          break;
        }
        case 'previous':
        {
          this.slideIndex>0?this.slideIndex--:this.slideIndex=this.slides.length-1;
          break;
        }
        case 'random':
        {
          this.slideIndex = Math.round((this.slides.length-1)*(Math.random()));
          break;
        }
        case 'next':
        default:
        {
          if(this.slideIndex<this.slides.length-1)
          {
            this.slideIndex++;
          }
          else
          {
            this.slideIndex = 0;
            if(!this.loop)
            {
              this.StopSlide();
            }
          }
          break;
        }
        case 'last':
        {
          this.slideIndex = this.slides.length-1;
          break;
        }
      }
    }

    if((this.slides[this.slideIndex].slideImage.complete == null)||(this.slides[this.slideIndex].slideImage.complete))
    {
      var _cssFilter = (this.slides[this.slideIndex].cssFilter!='')?this.slides[this.slideIndex].cssFilter:this.cssFilter;
      var _isCssFilterEnabled = ((!isUndefined(this.slideImage.style))&&(!isUndefined(this.slideImage.style.filter)))
      if((_cssFilter!='')&&(_isCssFilterEnabled))
      {
        this.slideImage.style.filter = _cssFilter;
        this.slideImage.filters.item(0).Apply();
      }

      this.slideImage.src = this.slides[this.slideIndex].slideImage;

      if((_cssFilter!='')&&(_isCssFilterEnabled))
      {
        this.slideImage.filters.item(0).Play();
      }

      if(this.slideImage.style)
      {
        this.slideImage.style.cursor = (((this.slides[this.slideIndex].link!='')?this.slides[this.slideIndex].link:this.link)!='')?'hand':'default';
      }

      var _title = (this.slides[this.slideIndex].title!='')?this.slides[this.slideIndex].title:this.title;
      this.slideImage.alt = _title;
      if(this.slideTitle!=null)
      {
        this.slideTitle.innerHTML = _title;
      }

      if(this.slides[this.slideIndex].action=='stop')
      {
        this.StopSlide();
      }

      if(this.playing)
      {
        this.SetSlideTimeout();
      }
    }
  }

  this.SetSlideTimeout = function()
  {
    this.ClearSlideTimeout();
    var _duration = (this.slides[this.slideIndex].duration>0)?this.slides[this.slideIndex].duration:this.duration;
    this.timeout = window.setTimeout("SlideShowCollection["+this.index+"].ChangeSlide()", _duration);
  }

  this.ClearSlideTimeout = function()
  {
    if(this.timeout!=0)
    {
      clearTimeout(this.timeout);
      this.timeout = 0;
    }
  }

  this.SlideShowImageClick = function()
  {
    var _link = (this.slides[this.slideIndex].link!='')?this.slides[this.slideIndex].link:this.link;
    if(_link!='')
    {
      document.location.href = _link;
    }
  }
}



// -- Slide object -------------------------------------------------------

function Slide(
  _imgSlide,  // string : image url for slide
  _link,      // string : url for image
  _title,     // string : text for image
  _duration,  // int    : amount of time in thousandsth's of a second slide is displayed
  _action,    // string : command for slide. can be 'stop' or 'start'
  _cssFilter  // string : style transition. IE only
  )
{
  this.slideImage = _imgSlide;
  this.link       = isUndefined(_link)?'':_link;
  this.title      = isUndefined(_title)?'':_title;
  this.duration   = isUndefined(_duration)?0:_duration;
  this.action     = ((_action=='stop')||(_action=='start'))?_action:'start';
  this.cssFilter  = isUndefined(_cssFilter)?'':_cssFilter;
}



// -- Initialise ---------------------------------------------------------

function InitSlideShows()
{
  for(var i=0;i<SlideShowCollection.length;i++)
  {
    SlideShowCollection[i].LoadSlideShow();
  }
}

var SlideShowCollection = new Array();



// -- Utils --------------------------------------------------------------

function isUndefined(o)
{ return(typeof(o)=='undefined'); }

function isInt(s)
{ return((''+parseInt(s))==s); }
