/*
  -------------------------------------------------------------------------------------------------

  : slideshow.js
  : ben miller : ben@hyl.co.uk : http://digital.hyl.co.uk
  : 11 June 2004
  : v 3.1

  -------------------------------------------------------------------------------------------------
*/

// -- Initialise ---------------------------------------------------------

var __aSlideShows = [];

function __initSlideShows()
{
  for(var i=0;i<__aSlideShows.length;i++)
  {
    __aSlideShows[i].Init();
  }
}

safeAddEventListener(window, 'load', __initSlideShows, false);

/*
  returns calendar with id of parameter _id
*/
function __getSlideShow(_id)
{
  for(var i=0;i<__aSlideShows.length;i++)
  {
    if(__aSlideShows[i].id==_id)
    {
      return __aSlideShows[i];
    }
  }
  return false;
}

// -- Slide Show --------------------------------------------------

function SlideShow(_id)
{
  if(!document.images){ return; }

  //var args = arguments;

  this.id = _id;
  this.index = __aSlideShows.length;
  __aSlideShows.push(this);

  this.slides     = getArg(arguments, 1, null);  // array of slide objects

  var args = arguments;

  this.slideIndex = (args.length>2)?args[2]:0; // slide to start on
  this.cssClass   = (args.length>3)?args[3]:'';                             // default style class
  this.style      = (args.length>4)?args[4]:'';                                // default style

  document.write('<img border=0 id="', this.id, '" name="', this.id, '"');
  document.write(args.length>1?' src="' + this.slides[this.slideIndex].slideImage + '">':' style="display:none;">');

  this.slideImage  = document.images[_id];  // image to change on page

  setElementCss(this.slideImage, this.cssClass, this.style);

  this.imgPlay     = new Image();           // image object for play event
  this.imgPlay.src = '';                    // image source for play button
  this.imgStop     = new Image();           // image object for stop event
  this.imgStop.src = this.imgPlay.src;      // image source for stop button
  this.playing     = false;                 // flag indicating state of slide show
  this.timeout     = 0;                     // timeout holder
  this.action      = 'start';               // action to start slide show
  this.loop        = true;                  // repeat slide show when finished
  this.cssFilter;                           // default IE visual filter to apply
  this.link;                                // default link when clicked
  this.title       = '';                    // default title
  this.duration    = 5000;                  // default time in msecs to show each slide
  this.btnPlayStop;                         // control button
  this.slideTitle;                          // current slide title

  this.Init = function()
  {
    if((isUndefined(this.slides))||(this.slides.length<1)) { return; }

    this.slideImage.style.display = '';

    setElementCss(this.slideImage, this.cssClass, this.style);

    if(this.action=='start')
    {
      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.Stop():this.Play();
  }

  this.Stop = function()
  {
    if(this.btnPlayStop!=null)
    {
      this.btnPlayStop.src = this.imgPlay.src;
      this.btnPlayStop.alt = 'play';
    }

    this.playing = false;

    this.ClearTimeout();
  }

  this.Play = function()
  {
    if(this.btnPlayStop!=null)
    {
      this.btnPlayStop.src = this.imgStop.src;
      this.btnPlayStop.alt = 'stop';
    }

    this.playing = true;

    this.Change();
  }

  this.Change = 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.Stop();
            }
          }
          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;

      if((_cssFilter!=null)&&(this.slideImage.filters))
      {
        this.slideImage.style.filter = _cssFilter;
        this.slideImage.filters.item(0).Apply();
      }

      this.slideImage.src = this.slides[this.slideIndex].slideImage;

      if((_cssFilter!=null)&&(this.slideImage.filters))
      {
        this.slideImage.filters.item(0).Play();
      }

      if(this.slideImage.style)
      {
        this.slideImage.style.cursor = ((this.link==null)&&(this.slides[this.slideIndex].link==null))?'default':'hand';
      }

      var _link = (this.slides[this.slideIndex].link!=null)?this.slides[this.slideIndex].link:this.link;
      if(_link!=null)
      {
        if(this.slideImage.style) { this.slideImage.style.cursor = 'hand'; }
        this.slideImage.onclick = function()
        {
          document.location.href = _link;
        }
      }
      else
      {
        if(this.slideImage.style) { this.slideImage.style.cursor = 'default'; }
        this.slideImage.onclick = function()
        {
          return false;
        }
      }

      var _title = (this.slides[this.slideIndex].title!=null)?this.slides[this.slideIndex].title:this.title;
      this.slideImage.alt = _title.replace('<br>', '&#10;').replace('&nbsp;', ' ');
      if(this.slideTitle!=null)
      {
        this.slideTitle.innerHTML = _title;
      }

      if(this.slides[this.slideIndex].action=='stop')
      {
        this.Stop();
      }

      if(this.playing)
      {
        this.SetSlideTimeout();
      }
    }
  }

  this.SetSlideTimeout = function()
  {
    this.ClearTimeout();
    var _duration = (this.slides[this.slideIndex].duration>0)?this.slides[this.slideIndex].duration:this.duration;
    this.timeout = window.setTimeout("__aSlideShows["+this.index+"].Change()", _duration);
  }

  this.ClearTimeout = function()
  {
    if(this.timeout!=0)
    {
      clearTimeout(this.timeout);
      this.timeout = 0;
    }
  }
}


// -- 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)?null:_link;
  this.title      = isUndefined(_title)?null:_title;
  this.duration   = isUndefined(_duration)?0:_duration;
  this.action     = ((_action=='stop')||(_action=='start'))?_action:'start';
  this.cssFilter  = isUndefined(_cssFilter)?'':_cssFilter;
}

