// Generated by CoffeeScript 1.3.3
var __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; };

define(['environ', 'jquery', 'underscore', 'backbone'], function(environ, $, _, Backbone) {
  var ROUTING, Router, router;
  ROUTING = false;
  Router = (function(_super) {

    __extends(Router, _super);

    function Router() {
      return Router.__super__.constructor.apply(this, arguments);
    }

    Router.prototype.registeredRoutes = {};

    Router.prototype.loadedRoutes = [];

    Router.prototype.register = function(route, name, view) {
      if (this.registeredRoutes[name] != null) {
        throw new Error("" + name + " view already registered");
      }
      this.registeredRoutes[name] = view;
      if (route === false) {
        if (typeof view.load === "function") {
          view.load();
        }
        if (typeof view.resolve === "function") {
          view.resolve();
        }
        return;
      }
      return this.route(route, name, function() {
        var _base, _base1, _base2, _base3, _i, _len, _name, _ref;
        if (!ROUTING) {
          ROUTING = true;
          _ref = this.loadedRoutes;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            _name = _ref[_i];
            if (typeof (_base = this.registeredRoutes[_name]).unload === "function") {
              _base.unload();
            }
            if (typeof (_base1 = this.registeredRoutes[_name]).pending === "function") {
              _base1.pending();
            }
          }
          this.loadedRoutes = [];
          _.defer(function() {
            return ROUTING = false;
          });
        }
        if (typeof (_base2 = this.registeredRoutes[name]).load === "function") {
          _base2.load();
        }
        if (typeof (_base3 = this.registeredRoutes[name]).resolve === "function") {
          _base3.resolve();
        }
        return this.loadedRoutes.push(name);
      });
    };

    return Router;

  })(Backbone.Router);
  return router = new Router;
});
