// Generated by CoffeeScript 1.3.3

define(['environ', 'jquery', 'underscore', 'backbone'], function(environ, $, _, Backbone) {
  var ROUTING, loadedRoutes, registeredRoutes;
  ROUTING = false;
  registeredRoutes = {};
  loadedRoutes = [];
  App.router = new Backbone.Router;
  return App.register = function(route, name, view) {
    var _this = this;
    if (registeredRoutes[name] != null) {
      throw new Error("" + name + " view already registered");
    }
    registeredRoutes[name] = view;
    if (route === false) {
      if (typeof view.load === "function") {
        view.load();
      }
      if (typeof view.resolve === "function") {
        view.resolve();
      }
      return;
    }
    this.router.route(route, name, function() {
      var _base, _base1, _base2, _base3, _i, _len, _name;
      if (!ROUTING) {
        ROUTING = true;
        for (_i = 0, _len = loadedRoutes.length; _i < _len; _i++) {
          _name = loadedRoutes[_i];
          if (typeof (_base = registeredRoutes[_name]).unload === "function") {
            _base.unload();
          }
          if (typeof (_base1 = registeredRoutes[_name]).pending === "function") {
            _base1.pending();
          }
        }
        loadedRoutes = [];
        _.defer(function() {
          return ROUTING = false;
        });
      }
      if (typeof (_base2 = registeredRoutes[name]).load === "function") {
        _base2.load();
      }
      if (typeof (_base3 = registeredRoutes[name]).resolve === "function") {
        _base3.resolve();
      }
      return loadedRoutes.push(name);
    });
    return this.router.on("route:" + name, function() {
      var links;
      links = $('[data-route]');
      links.parent().removeClass('active');
      return links.filter("[data-route=" + name + "]").parent().addClass('active');
    });
  };
});
