module.exports.initBackboneLoader = function(loaderModule, failure) {
  var lumbarLoader = (loaderModule || module.exports).loader;

  // Setup backbone route loading
  var handlers = {
    routes: {}
  };

  var pendingModules = {};
  for (var moduleName in lumbarLoader.map.modules) {
    handlers['loader_' + moduleName] = (function(moduleName) {
      return function() {
        if (lumbarLoader.isLoaded(moduleName)) {
          // The module didn't implement the proper route
          failure && failure('missing-route', moduleName);
          return;
        } else if (pendingModules[moduleName]) {
          // Do not exec the backbone callback multiple times
          return;
        }

        pendingModules[moduleName] = true;
        lumbarLoader.loadModule(moduleName, function(err) {
          pendingModules[moduleName] = false;

          if (err) {
            failure && failure(err, moduleName);
            return;
          }

          // Reload with the new route
          Backbone.history.loadUrl();
        });
      };
    })(moduleName);
  }

  // For each route create a handler that will load the associated module on request
  for (var route in lumbarLoader.map.routes) {
    handlers.routes[route] = 'loader_' + lumbarLoader.map.routes[route];
  }

  new (Backbone.Router.extend(handlers));
};

// Automatically initialize the loader if everything is setup already
if (module.exports.loader && module.exports.loader.map && window.Backbone) {
  module.exports.initBackboneLoader();
}
