/*
    This file is part of JonDesign's SmoothSlideshow v2.0.

    JonDesign's SmoothSlideshow is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    JonDesign's SmoothSlideshow is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Main Developer: Jonathan Schemoul (JonDesign: http://www.jondesign.net/)
    Contributed code by:
    - Christian Ehret (bugfix)
    - Simon Willison (addLoadEvent)
*/

// declaring the class
var timedSlideShow = Class.create();

// implementing the class
timedSlideShow.prototype = {
  initialize: function(element, data) {
    this.currentIter = 0;
    this.lastIter = 0;
    this.maxIter = 0;
    this.slideShowElement = element;
    this.slideShowInit = 1;
    this.slideElements = Array();
    this.slideShowDelay = 4000;

    element.style.display="block";

    this.maxIter = data.length;
    for(i=0;i<data.length;i++)
    {
      var currentImg = document.createElement('div');
      currentImg.className = "slideElement";
      currentImg.style.backgroundImage="url('" + data[i] + "')";
      currentImg.style.backgroundPosition="center center";

      element.appendChild(currentImg);
      currentImg.currentOpacity = new Fx.Style(currentImg, 'opacity', {duration:400});
      currentImg.setStyle('opacity',0);
      this.slideElements[parseInt(i)] = currentImg;
    }
    this.doSlideShow();
  },
  startSlideShow: function() {
    this.lastIter = this.maxIter - 1;
    this.currentIter = 0;
    this.slideShowInit = 0;
    this.slideElements[parseInt(this.currentIter)].setStyle('opacity', 1);
    setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
  },
  nextSlideShow: function() {
    this.lastIter = this.currentIter;
    this.currentIter++;
    if (this.currentIter >= this.maxIter)
    {
      this.currentIter = 0;
      this.lastIter = this.maxIter - 1;
    }
    this.slideShowInit = 0;
    this.doSlideShow.bind(this)();
  },
  doSlideShow: function() {
    if (this.slideShowInit == 1)
    {
      setTimeout(this.startSlideShow.bind(this),10);
    } else {
      if (this.currentIter != 0) {
        this.slideElements[parseInt(this.currentIter)].currentOpacity.options.onComplete = function() {
          this.slideElements[parseInt(this.lastIter)].setStyle('opacity',0);
        }.bind(this);
        this.slideElements[parseInt(this.currentIter)].currentOpacity.custom(0, 1);
      } else {
        this.slideElements[parseInt(this.currentIter)].setStyle('opacity',1);
        this.slideElements[parseInt(this.lastIter)].currentOpacity.custom(1, 0);
      }
      setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
    }
  }
};


/*** init ***/

function startSlideshow()
{
  if($('slideshow'))
  {
    var slideshow = new timedSlideShow($('slideshow'), slidedata);
  }
}

Window.onDomReady(function()
{
  startSlideshow();
});