text: // Generated by CoffeeScript 1.8.0
  (function() {
    var $, Spine,
      __hasProp = {}.hasOwnProperty,
      __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
      __slice = [].slice,
      __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  
    Spine = this.Spine || require('spine');
  
    $ = Spine.$;
  
    Spine.Manager = (function(_super) {
      __extends(Manager, _super);
  
      Manager.include(Spine.Events);
  
      function Manager() {
        this.controllers = [];
        this.bind('change', this.change);
        this.add.apply(this, arguments);
      }
  
      Manager.prototype.add = function() {
        var cont, controllers, _i, _len, _results;
        controllers = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
        _results = [];
        for (_i = 0, _len = controllers.length; _i < _len; _i++) {
          cont = controllers[_i];
          _results.push(this.addOne(cont));
        }
        return _results;
      };
  
      Manager.prototype.addOne = function(controller) {
        controller.bind('active', (function(_this) {
          return function() {
            var args;
            args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
            return _this.trigger.apply(_this, ['change', controller].concat(__slice.call(args)));
          };
        })(this));
        controller.bind('release', (function(_this) {
          return function() {
            var c;
            return _this.controllers = (function() {
              var _i, _len, _ref, _results;
              _ref = this.controllers;
              _results = [];
              for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                c = _ref[_i];
                if (c !== controller) {
                  _results.push(c);
                }
              }
              return _results;
            }).call(_this);
          };
        })(this));
        return this.controllers.push(controller);
      };
  
      Manager.prototype.deactivate = function() {
        return this.trigger.apply(this, ['change', false].concat(__slice.call(arguments)));
      };
  
      Manager.prototype.change = function() {
        var args, cont, current, _i, _len, _ref;
        current = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
        _ref = this.controllers;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          cont = _ref[_i];
          if (cont !== current) {
            cont.deactivate.apply(cont, args);
          }
        }
        if (current) {
          return current.activate.apply(current, args);
        }
      };
  
      return Manager;
  
    })(Spine.Module);
  
    Spine.Controller.include({
      active: function() {
        var args;
        args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
        if (typeof args[0] === 'function') {
          this.bind('active', args[0]);
        } else {
          args.unshift('active');
          this.trigger.apply(this, args);
        }
        return this;
      },
      isActive: function() {
        return this.el.hasClass('active');
      },
      activate: function() {
        this.el.addClass('active');
        return this;
      },
      deactivate: function() {
        this.el.removeClass('active');
        return this;
      }
    });
  
    Spine.Stack = (function(_super) {
      __extends(Stack, _super);
  
      Stack.prototype.controllers = {};
  
      Stack.prototype.routes = {};
  
      Stack.prototype.className = 'spine stack';
  
      function Stack() {
        this.release = __bind(this.release, this);
        var key, value, _fn, _ref, _ref1, _ref2;
        Stack.__super__.constructor.apply(this, arguments);
        this.manager = new Spine.Manager;
        this.router = (_ref = Spine.Route) != null ? _ref.create() : void 0;
        _ref1 = this.controllers;
        for (key in _ref1) {
          value = _ref1[key];
          if (this[key] != null) {
            throw Error("'@" + key + "' already assigned");
          }
          this[key] = new value({
            stack: this
          });
          this.add(this[key]);
        }
        _ref2 = this.routes;
        _fn = (function(_this) {
          return function(key, value) {
            var callback;
            if (typeof value === 'function') {
              callback = value;
            }
            callback || (callback = function() {
              var _ref3;
              return (_ref3 = _this[value]).active.apply(_ref3, arguments);
            });
            return _this.route(key, callback);
          };
        })(this);
        for (key in _ref2) {
          value = _ref2[key];
          _fn(key, value);
        }
        if (this["default"]) {
          this[this["default"]].active();
        }
      }
  
      Stack.prototype.add = function(controller) {
        this.manager.add(controller);
        return this.append(controller);
      };
  
      Stack.prototype.release = function() {
        var _ref;
        if ((_ref = this.router) != null) {
          _ref.destroy();
        }
        return Stack.__super__.release.apply(this, arguments);
      };
  
      return Stack;
  
    })(Spine.Controller);
  
    if (typeof module !== "undefined" && module !== null) {
      module.exports = Spine.Manager;
    }
  
    if (typeof module !== "undefined" && module !== null) {
      module.exports.Stack = Spine.Stack;
    }
  
  }).call(this);
  