var DN = {};
( function ($) {

  // assume DN.bootLoader is an already declared namespace
  // else we can define it here as well, un-comment the following line

  if (typeof DN === 'undefined') {
    if (typeof DN.bootLoader === 'undefined') {
      DN.bootLoader = {};
    }
  }

  DN.Modules = {
    // DEFINE ALL THE COMMON AND GROUP RELATED CALLS HERE.
    common: {
      // application wide code
      init: function () {

      }
    }
  };

  DN.App = {};
  DN.Widgets = {};
  DN.Cells = {};
  DN.Utils = {};
  DN.bootLoader = {

    /**
     * Utility method to execute module specific code in a seamless manner
     * this method is being used internally, don't call this method from
     * outside.
     *
     * @private
     * @param controller
     * @param action
     */
    _exec: function (controller, action) {
      controller || ( controller = 'common' );
      action || ( action = 'init' );

      var ns = DN.Modules; // HERE WE CAN CHANGE THIS BY PASSING THE NAMESPACE AS WELL // @TODO
      // inner function to execute the target method
      var execute = function(_execController, _execAction) {
        if (ns[_execController]) {
          var fn = ns[_execController][_execAction];

          if ($.isFunction(fn)) {
            try {
              // set DNApp.bootLoader as 'this' inside the functions
              fn.call(DN.bootLoader);
            } catch(ex) {
            }
          }
        }

      };

      execute(controller, action);
    }
  };


  // For the following to work you need to add the following code to your <body> tag in the HTML
  // <body data-controller="<%= controller_name %>" data-action="<%= action_name %>">

  $(document).ready(function() {
    var body = $(document.body),
      action = '_' + body.attr('data-action'),
      controller = body.attr('data-controller');
    DN.bootLoader._exec(); // execute site wide code
    DN.bootLoader._exec(controller); // execute controller wide code
    DN.bootLoader._exec(controller, action); // execute action specific code
  });
})(jQuery);


