'use strict';

/*
 * FastScroll
 * https://github.com/soenkekluth/fastscroll
 *
 * Copyright (c) 2014 Sönke Kluth
 * Licensed under the MIT license.
 */

var delegate = require('delegatejs');

var FastScroll = function(options) {
  this.options = (options || {});
  this.element = this.options.el ||  window;
  this.init();
};

FastScroll.prototype = {

  scrolling: false,
  scrollY: 0,
  scrollX: 0,
  lastScrollY: 0,
  lastScrollX: 0,
  speedY: 0,
  speedX: 0,

  _hasRequestedAnimationFrame: false,

  init: function() {
    this.element.addEventListener('scroll', delegate(this, this.onScroll), false);
  },

  getAttributes: function() {
    return {
      scrollY: this.scrollY,
      scrollX: this.scrollX,
      speedY: this.speedY,
      speedX: this.speedX,
      angle: 0,
      //TODO not save for now... do like checkScrollstop
      direction: this.speedY === 0 ? 'none' : (this.speedY > 0) ? 'up' : 'down'
    };
  },

  onScroll: function() {
    this.scrollY = this.element.scrollY;
    this.scrollX = this.element.scrollX;
    this.scrolling = true;

    if (!this._hasRequestedAnimationFrame) {
      this._hasRequestedAnimationFrame = true;
      window.requestAnimationFrame(delegate(this, this.onAnimationFrame));
      var attr = this.getAttributes();
      this.dispatchEvent('scroll:start', attr);
      if (this.options.start) {
        this.options.start.call(this, attr);
      }
    }
  },

  onAnimationFrame: function() {
    this.speedY = this.lastScrollY - this.scrollY;
    this.speedX = this.lastScrollX - this.scrollX;

    this.lastScrollY = this.scrollY;
    this.lastScrollX = this.scrollX;

    var attr = this.getAttributes();
    this.dispatchEvent('scroll:progress', this.getAttributes());

    if (this.options.scrolling) {
      this.options.scrolling.call(this, attr);
    }

    if (this.speedY === 0 && this.speedX === 0) {
      window.requestAnimationFrame(delegate(this, this.onCheckScrollStop));
    } else {
      window.requestAnimationFrame(delegate(this, this.onAnimationFrame));
    }
  },

  onCheckScrollStop: function() {
    this.speedY = this.lastScrollY - this.scrollY;
    this.speedX = this.lastScrollX - this.scrollX;
    if (this.speedY === 0 && this.speedX === 0) {
      this.scrolling = false;
      this._hasRequestedAnimationFrame = false;
      var attr = this.getAttributes();
      this.dispatchEvent('scroll:stop', attr);
      if (this.options.stop) {
        this.options.stop.call(this, attr);
      }
    } else {
      this.onAnimationFrame();
    }
  },

  dispatchEvent: function(type, eventObject) {
    // create and dispatch the event
    var event = new CustomEvent(type, {
      detail: eventObject
    });
    this.element.dispatchEvent(event);
  },

  on: function(event, listener) {
    if (this.element) {
      //TODO check for events: event
      this.element.addEventListener(event, listener, false);
    }
  },

  off: function(event, listener) {
    if (this.element) {
      //TODO check for events: event
      this.element.removeEventListener(event, listener);
    }
  }
};

module.exports = FastScroll;

@@ -347,16 +347,36 @@
 pe = %7B%0A%0A
+  destroyed: false,%0A
   scroll
@@ -379,32 +379,32 @@
 rolling: false,%0A
-
   scrollY: 0,%0A  
@@ -551,80 +551,342 @@
 his.
-element.addEventListener('scroll', delegate(this, this.onScroll), false)
+onScrollDelegate = delegate(this, this.onScroll);%0A    this.element.addEventListener('scroll', this.onScrollDelegate, false);%0A  %7D,%0A%0A  destroy:function() %7B%0A    this.element.removeEventListener('scroll', this.onScrollDelegate);%0A    this.onScrollDelegate = null;%0A    this.element = null;%0A    this.options = null;%0A    this.destroyed = true
 ;%0A  
@@ -1721,32 +1721,80 @@
 e: function() %7B%0A
+%0A    if (this.destroyed) %7B%0A      return;%0A    %7D%0A%0A
     this.speedY 

add destroy method