"use strict";

var View = require("./view");
var Router = require("./router");
var util = require("substance-util");
var _ = require("underscore");

// Substance.Application
// ==========================================================================
//
// Application abstraction suggesting strict MVC

var Application = function(config) {
  View.call(this);
  this.config = config;
};

Application.Prototype = function() {
  
  // Init router
  // ----------

  this.initRouter = function() {
    this.router = new Router();

    _.each(this.config.routes, function(route) {
      this.router.route(route.route, route.name, _.bind(this.controller[route.command], this.controller));
    }, this);

    Router.history.start();
  };

  // Start Application
  // ----------
  //

  this.start = function() {
    this.initRouter();
    this.$el = $('body');
    this.el = this.$el[0];
    this.render();
  };
};

// Setup prototype chain

Application.Prototype.prototype = View.prototype;
Application.prototype = new Application.Prototype();

module.exports = Application;

@@ -808,26 +808,41 @@
     
-this.initRouter();
+// First setup the top level view
 %0A   
@@ -909,16 +909,230 @@
 ender();
+%0A%0A    // Now the normal app lifecycle can begin%0A    // Because app state changes require the main view to be present%0A    // Triggers an initial app state change according to url hash fragment%0A    this.initRouter();
 %0A  %7D;%0A%7D;

Change order of initial app render and router activation.