// Generated by CoffeeScript 1.3.3
var __slice = [].slice;

define(['environ', 'jquery', 'underscore', 'backbone', 'router', 'models/datafield', 'models/dataconcept'], function(environ, $, _, Backbone, Router) {
  var modules, routes;
  routes = App.routes;
  modules = _.pluck(routes, 'module');
  return require(modules, function() {
    var config, head, i, klasses, path, pathlen, root, routelen, _i, _len;
    klasses = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
    root = App.root;
    head = App.head;
    path = window.location.pathname;
    pathlen = path.length;
    for (i = _i = 0, _len = routes.length; _i < _len; i = ++_i) {
      config = routes[i];
      if (!config.module) {
        continue;
      }
      if (!(head != null) && config.route !== false) {
        head = config.route;
      }
      Router.register(config.route, config.name, new klasses[i](config.options));
      if (config.route && !(root != null)) {
        routelen = config.route.length;
        if (path.substr(pathlen - routelen) === config.route) {
          root = path.substr(0, pathlen - routelen);
        }
      }
    }
    if (root == null) {
      root = path;
    }
    if (head == null) {
      head = '';
    }
    require(['session', 'models/datacontext', 'models/dataview']);
    return $(function() {
      var li, nav, _j, _len1;
      if (!Backbone.history.start({
        root: root,
        pushState: true
      })) {
        Router.navigate(head, {
          replace: true,
          trigger: true
        });
      }
      nav = $('<ul class="nav pull-left" />');
      for (_j = 0, _len1 = routes.length; _j < _len1; _j++) {
        config = routes[_j];
        if (!config.module || config.route === false) {
          continue;
        }
        li = $("<li><a href=\"" + root + config.route + "\"                    data-route=\"" + config.route + "\">" + config.label + "</a></li>");
        nav.append(li);
      }
      nav.appendTo('header .container-fluid');
      return $('body').on('click', '[data-route]', function(event) {
        var route;
        event.preventDefault();
        route = $(this).data('route');
        return Router.navigate(route, {
          trigger: true
        });
      });
    });
  });
});
