
/*
 */

(function() {
  var EventEmitter, SLICE, root;

  root = exports || this;

  SLICE = Array.prototype.slice;

  root.EventEmitter = EventEmitter = (function() {
    function EventEmitter(iMaxListener) {
      if (iMaxListener == null) {
        iMaxListener = 10;
      }
      this._maxListener = iMaxListener;
      this._events = {};
    }

    EventEmitter.prototype.setMaxListeners = function(iMaxListener) {
      return this._maxListener = iMaxListener;
    };

    EventEmitter.prototype.getMaxListeners = function() {
      return this._maxListener;
    };

    EventEmitter.prototype.emit = function(type) {
      var args, sub, subs, _i, _len;
      if (this._events && (subs = this._events[type])) {
        args = SLICE.call(arguments, 1);
        for (_i = 0, _len = subs.length; _i < _len; _i++) {
          sub = subs[_i];
          sub.apply(this, args);
        }
      }
      return this;
    };

    EventEmitter.prototype.on = function(type, listener) {
      this._events[type] = this._events[type] || [];
      this._events[type].push(listener);
      return this;
    };

    EventEmitter.prototype.once = function(type, listener) {
      var fired, g;
      fired = false;
      g = function() {
        this.removeListener(type, g);
        if (!fired) {
          fired = true;
          return listener.apply(this, arguments);
        }
      };
      g.listener = listener;
      this.on(type, g);
      return this;
    };

    EventEmitter.prototype.off = function(type, listener) {
      var i, length, list, pos, _i;
      list = this._events[type];
      if (!list) {
        return;
      }
      length = list.length;
      pos = -1;
      for (i = _i = 0; 0 <= length ? _i <= length : _i >= length; i = 0 <= length ? ++_i : --_i) {
        if (list[i] === listener || (list[i] && list[i].listener === listener)) {
          pos = i;
          break;
        }
      }
      if (pos < 0) {
        return this;
      }
      return this.emit('off', type, listener);
    };

    EventEmitter.prototype.offAll = function(type) {
      var key, _i, _len, _ref, _results;
      if (arguments.length === 0) {
        _ref = this._events;
        _results = [];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          key = _ref[_i];
          _results.push(delete this._events[key]);
        }
        return _results;
      } else {
        return delete this._events[type];
      }
    };

    EventEmitter.prototype.listenerCount = function(type) {
      var events;
      if (events = this._events[type]) {
        return events.length;
      } else {
        return 0;
      }
    };

    EventEmitter.prototype.listener = function(type) {
      return this._events[type];
    };

    return EventEmitter;

  })();

}).call(this);
