/**
 * Includes extended UI.Carousel
 *
 * @link       http://www.capital-immobilien.de/
 * @package    immokompass
 * @version    0.1
 */


/**
 * Do some checks
 */
if (typeof UI == "undefined") {
	throw("UI.ImmoKompassCarousel requires Prototype-UI");
}


/**
 * Class UI.ImmoKompassCarousel, uses easeout effect.
 *
 * @class    UI.ImmoKompassCarousel
 * @extends  UI.Carousel
 * @requires Prototype, Prototype-UI and Scriptaculous
 */
UI.ImmoKompassCarousel = Class.create(UI.Carousel, {

  /**
   * Return new pos for the easeout
   */
  _easeOut: function(pos) {
    return Math.pow(pos, 0.25);
  },


  /**
   * Overwrite parents method to provide the easeout feature
   */
  scroll: function(deltaPixel) {
    if (this.animating)
      return this;

    // Compute new position
    var position =  this.currentPosition() + deltaPixel;

    // Check bounds
    position = this.checkScroll(position, false);

    // Compute shift to apply
    deltaPixel = position - this.currentPosition();
    if (deltaPixel != 0) {
      this.animating = true;
      this.fire("scroll:started");

      var that = this;
      // Move effects
//      this.container.morph("opacity:1", {duration: 0.2, afterFinish: function() {
//        that.container.morph(that.posAttribute + ": " + position + "px", {
        this.container.morph(that.posAttribute + ": " + position + "px", {
          duration: 1.0,
          delay: 0.3,
          transition: that._easeOut,
          afterFinish: function() {
            that.container.morph("opacity:1", {
              duration: 0.2,
              afterFinish: function() {
                that.animating = false;
                that.updateButtons()
                  .fire("scroll:ended", { shift: deltaPixel / that.currentSize() });
              }
            });
          }
        });
//      }});
    }
    return this;
  }

});