/*
    This file is part of JonDesign's SmoothSlideshow v2.0.
    modified by Andreas Albarello

    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.slideShowData = data;
		this.slideShowInit = 1;
		this.slideElements = Array();
		this.slideShowDelay = 4000;
		element.style.display = "block";
    
    var globalLink = document.createElement('a');
    globalLink.setAttribute('id', 'globalLink');
    globalLink.className = 'global';
    globalLink.onclick = function () { myLightbox.end(); }
    element.appendChild(globalLink);    

		this.maxIter = data.length;
		for(i=0;i<data.length;i++)
		{
  		var currentImg = document.createElement('div');
  		currentImg.setAttribute('id', 'slideelement' + parseInt(i));
  		currentImg.style.position = "absolute";
  		currentImg.style.left = "0px";
  		currentImg.style.top = "0px";
  		currentImg.style.margin = "0px";
  		currentImg.className = "slideelement";
  	
      var myImg = document.createElement('img');
      myImg.src = data[i][0];
      currentImg.appendChild(myImg);
      myImg = null;
      
      globalLink.appendChild(currentImg);
  		currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
  		currentImg.setStyle('opacity', 0);
			this.slideElements[i] = currentImg;
      currentImg = null;
		}
	
		this.doSlideShow();
	},
  
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		element.parentNode.replaceChild(newElement, element);
	},
  
  cleanup: function() {
    for(i=0; i<this.slideElements.length; i++) {
      this.slideElements[i].removeChild(this.slideElements[i].firstChild);
      this.slideElements[i].currentOpacity.options.onComplete = null;
      this.slideElements[i].currentOpacity = null;
      $('globalLink').removeChild(this.slideElements[i]);
      this.slideElements[i] = null;
    }
    $('globalLink').onclick = null;
    this.slideShowElement.removeChild($('globalLink'));
  },
  
	startSlideShow: function() {
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.slideShowInit = 0;
		this.slideElements[parseInt(this.currentIter)].setStyle('opacity', 1);
    this.stop = 0;
    this.timer = null;
    this.block = false;
		this.timer = setTimeout(this.nextSlideShow.bind(this), this.slideShowDelay);
	},
  
  stopSlideShow: function() {
    this.stop = 1;
    this.slideShowElement.style.display = "none";
  },
  
  prevSlideShow: function() {
    if (this.block)
      return;
		this.lastIter = this.currentIter;
		this.currentIter--;  
    if (this.currentIter <= -1)
    {
      this.currentIter = this.maxIter - 1;
      this.lastIter = 0;
    }
    this.slideShowInit = 0;	
	  if (this.stop == 0)
      this.doSlideShow.bind(this)();
  },
  
	nextSlideShow: function() {
    if (this.block)
      return;
    this.lastIter = this.currentIter;
    this.currentIter++;
    if (this.currentIter >= this.maxIter)
    {
      this.currentIter = 0;
      this.lastIter = this.maxIter - 1;
    }
    this.slideShowInit = 0;
    if (this.stop == 0)
      this.doSlideShow.bind(this)();
	},
  
	doSlideShow: function() {
		if (this.slideShowInit == 1) {
			imgPreloader = new Image();
			imgPreloader.onload = function () {
				setTimeout(this.startSlideShow.bind(this), 10);
        imgPreloader.onload = null;
        imgPreloader = null;
			}.bind(this);
			imgPreloader.src = this.slideShowData[0][0];    
  	} else {
      this.block = true;
      $('slideelement' + parseInt(this.currentIter)).currentOpacity.options.onComplete = function() {
        this.block = false;  
      }.bind(this);
      $('slideelement' + parseInt(this.currentIter)).currentOpacity.custom(0, 0.99);	
      $('slideelement' + parseInt(this.lastIter)).currentOpacity.custom(0.99, 0);
      if (this.timer != null)
        clearTimeout(this.timer);
      this.timer = setTimeout(this.nextSlideShow.bind(this), this.slideShowDelay);
    }
	}
  
};
