/*
  -------------------------------------------------------------------------------------------------

  : slide.js
  : ben miller : ben@hyl.co.uk : http://digital.hyl.co.uk

  : slideshow

  : requires
  : hyl_client/1_0/util.js

  -------------------------------------------------------------------------------------------------
*/

var hyl = hyl || {};
hyl.resourcePath = hyl.resourcePath || '/hyl_client/1_0/';

__hylSlideShow = [];

/*
  -------------------------------------------------------------------------------------------------
*/
hyl.SlideShow = function()
{
  this.index = __hylSlideShow.length;
  __hylSlideShow.push(this);

  this.slides = [];
  this.img = document.createElement('IMG');
  this.buffer = document.createElement('IMG');
  this.buffer.style.position = 'absolute';
  this.buffer.style.top = '0px';
  this.buffer.style.left = '0px';

  this.appendToNode = document.body;

  this.autoStart = true;
  this.loop = true;
  this.duration = 5000;
  this.fadeTransition = true;

  this.slideIdx = 0;
}

/*
  -------------------------------------------------------------------------------------------------
*/
hyl.SlideShow.prototype.load = function()
{
  this.appendToNode.style.position = 'relative';

  this.appendToNode.appendChild(this.img);
  this.appendToNode.appendChild(this.buffer);

  if(this.autoStart)
  {
    this.change();
  }
}

/*
  -------------------------------------------------------------------------------------------------
*/
hyl.SlideShow.prototype.change = function()
{
  this.img.src = this.slides[this.slideIdx].imgPath;

  this.slideIdx++;
  this.slideIdx = this.slideIdx % this.slides.length;

  var img = new Image();
  var _self = this;
  img.onload = function()
  {
    if(_self.fadeTransition)
    {
      _self.bufferOpacity = 0;
      hyl.opacity(_self.buffer, _self.bufferOpacity);
      _self.buffer.src = this.src;

      window.setTimeout('__hylSlideShow['+ _self.index +'].fade()', _self.duration);
    }
    else
    {
      window.setTimeout('__hylSlideShow['+ _self.index +'].change()', _self.duration);
    }
    _self.slides[_self.slideIdx].loaded = true;
  }
  img.src = this.slides[this.slideIdx].imgPath;
}

/*
  -------------------------------------------------------------------------------------------------
*/
hyl.SlideShow.prototype.fade = function()
{
  if(this.bufferOpacity < 1)
  {
    this.bufferOpacity += 0.05;
    hyl.opacity(this.buffer, this.bufferOpacity);
    hyl.opacity(this.img, 1 - this.bufferOpacity);

    window.setTimeout('__hylSlideShow['+ this.index +'].fade()', 50);
  }
  else
  {
    hyl.opacity(this.buffer, 0);
    hyl.opacity(this.img, 1);
    this.img.src = this.buffer.src;
    this.change();
  }
}

/*
  -------------------------------------------------------------------------------------------------
*/
hyl.opacity = function(_el, _amount)
{
  _el.style.opacity = _amount;
  _el.style.filter = 'alpha(opacity=' + _amount * 100 + ')';
}

/*
  -------------------------------------------------------------------------------------------------
*/
hyl.Slide = function(
  _imgPath)
{
  this.imgPath = _imgPath;
  this.loaded = false;
}
