//= require "content_carousel"
AutoPlay = function(domElementNamespace, perWindow) {
  this.parent = ContentCarousel.prototype;

  // Call the parent class' initialize function
  this.parent.initialize.call(this, domElementNamespace, perWindow, this._fadeTransition);
  this.initialize(domElementNamespace, perWindow);
};

// Inherit from ContentCarousel
AutoPlay.prototype = new ContentCarousel();

AutoPlay.prototype.DEFAULT_INTERVAL = 8000;

AutoPlay.prototype.initialize = function(domElementNamespace, perWindow) {
  /* override some defaults */
  this._behaviourOptions.cyclicalWindowMovement = true;
  this._behaviourOptions.transitionEffectOnClick = false;

  this._setIndicator();
  this._autoplayTimer = null;
};

AutoPlay.prototype.start = function() {
  var items = this._items();
  for(var i = 0, len = items.length; i < len; i++) {
    $(items[i]).show();
  }

  this._play();
};

AutoPlay.prototype.next = function(event) {
  this._resetTimer(this._isPlaying());

  this.parent.next.call(this, event);
  this._setIndicator();
};

AutoPlay.prototype.previous = function(event) {
  this._resetTimer(this._isPlaying());

  this.parent.previous.call(this, event);
  this._setIndicator();
};

AutoPlay.prototype.pause = function() {
  if(this._autoplayTimer === null) { /* we're paused, let's play */
    this._play();
    this.next();
    this._pauseControlElement().pause(false);
  }
  else { /* we're playing, let's pause */
    clearInterval(this._autoplayTimer);
    this._autoplayTimer = null;

    this._pauseControlElement().pause(true);
  }
};

AutoPlay.prototype._play = function() {
  if(this._numItems() > 1) {
    var self = this;

    self._autoplayTimer = setInterval(function() {
      self.next();
    }, self.DEFAULT_INTERVAL);
  }
};

AutoPlay.prototype._resetTimer = function(conditional) {
  if(conditional) {
    clearInterval(this._autoplayTimer);
    this._play();
  }
};

AutoPlay.prototype._initEventListeners = function() {
  var self = this;

  self._pauseControlElement().onclick = function() {
    self.pause();
  };

  self._pauseControlElement().pause = function(paused) {
    var button = $(this);
    if (paused) {
      // Prototype
      if (button.addClassName) {
        $(this).addClassName('paused');
      // jQuery
      } else {
        $(this).addClass('paused');
      }
    } else {
      // Prototype
      if (button.removeClassName) {
        button.removeClassName('paused');
      // jQuery
      } else {
        button.removeClass('paused');
      }
    }
  };

  self.parent._initEventListeners.call(self);
};

AutoPlay.prototype._prepDomElements = function() {
  this.parent._prepDomElements.call(this);

  if(!this._needControls()) {
    $(this._pauseControlElement()).hide();
  }
};

AutoPlay.prototype._pauseControlElement = function() {
  return this._lazyLoad('__pauseControlElement', function() {
    return document.getElementById(this._elementName('pause'));
  });
};

AutoPlay.prototype._positionIndicatorElement = function() {
  return this._lazyLoad('__positionIndicatorElement', function() {
    return document.getElementById(this._elementName('item-number'));
  });
};

AutoPlay.prototype._isPlaying = function() {
  return this._autoplayTimer !== null;
};

AutoPlay.prototype._fadeTransition = function(carousel, leftValue) {
  var fadeElement = document.getElementById('autoplay-overlay');

  function next(direction, opacity, after) {
    setTimeout(function() { fade(direction,opacity,after); }, 50);
  }

  function fade(direction,opacity,after) {
    if( (direction == 1 && opacity <= 1) || (direction == -1 && opacity >= 0 )) {
      AutoPlay.setOpacity(fadeElement, opacity);
      next(direction, opacity + (direction * 0.05), after);
    }
    else {
      after();
    }
  }

  $(fadeElement).show();

  fadeElement.style.height = "100%";
  fadeElement.style.width = "100%";

  fade(1, 0, function() {
    carousel._itemContainer().style.left = leftValue.toString() + 'px';
    carousel._setIndicator();
    fade(-1, 1, function() { $(fadeElement).hide(); });
  });
};

AutoPlay.prototype._setIndicator = function() {
  if(this._numItems() == 1) {
    return;
  }

  var text = "";
  for(i = 0; i < this._numItems(); i++) {
    if(i == this._currentWindow()) {
      text += "<span style='color:#00a9eb;font-weight:bold;'>&bull;</span>";
    }
    else {
      text += "&bull;";
    }
  }

  this._positionIndicatorElement().innerHTML = text;
};

AutoPlay.setOpacity = function(element, opacity) {
  element.style.filter = "alpha(opacity=" + (opacity * 100).toString() + ")";
  element.style['-moz-opacity'] = opacity;
  element.style['-khtml-opacity'] = opacity;
  element.style.opacity = opacity;
};

