PD.onBoot(function (sender) {

    PD.modules = new PD.Manage.ModuleList();
    PD.pluginRouter = new PD.Plugins.PluginRouter();
    PD.monitor = new PD.Web.UI.MutationManager();
    
    Path.map("#/module/:moduleId(/:targetId)").to(function (e) {

        var module = null,
            moduleId = this.params["moduleId"],
            targetId = this.params["targetId"],
            fqId = null;

        if (moduleId) {

            fqId = moduleId + (targetId != null ? "/" + targetId : "");

            module = PD.modules.findById(fqId);
        
            if (module == null) {
                module = PD.modules.findById(moduleId);
            }
            
            if (module) {
                module.id = fqId;
                module.route = window.location.hash;
                module.targetId = targetId;
                PD.context
                    .get_global()
                    .get_events()
                    .publish("Module:Request", $.extend({}, module, module.attributes));
            }

        }

    });

    $(document).ready(function () {

        var moduleService = new PD.Manage.Services.ModuleService();

        moduleService.loadModuleDefinitions(function () {

            var requestedHash = window.location.hash;

            if (requestedHash == null || requestedHash == "") {
                window.location.hash = "#/module/overview";
            }
            
            Path.listen();

            if (requestedHash) {
                window.location.hash = requestedHash;
            }

        });

    });

});


// Register command groups
PD.onBoot(function (sender) {

    PD.Commands.CommandRegistrar.registerGroup("pd/entity-type", PD.Manage.Modules.ObjectModel.EntityTypeCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/entity-type/context-menu", PD.Manage.Modules.ObjectModel.EntityTypeCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/entity-types", PD.Manage.Modules.ObjectModel.EntityTypesCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/entity-types/context-menu", PD.Manage.Modules.ObjectModel.EntityTypesCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/system-setting", PD.Manage.Modules.ObjectModel.SystemSettingCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/system-setting/context-menu", PD.Manage.Modules.ObjectModel.SystemSettingCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/process", PD.Manage.Modules.ObjectModel.ProcessCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/process/context-menu", PD.Manage.Modules.ObjectModel.ProcessCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/process-model", PD.Manage.Modules.ObjectModel.ProcessModelCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/process-model/context-menu", PD.Manage.Modules.ObjectModel.ProcessModelCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/resources", PD.Manage.Modules.Resources.ResourcesCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/resources/context-menu", PD.Manage.Modules.Resources.ResourcesCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/resource-editor", PD.Manage.Modules.Resources.ResourceEditorCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/rulesets", PD.Manage.Modules.Rules.RulesetsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/rulesets/context-menu", PD.Manage.Modules.Rules.RulesetsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/rulesets-editor", PD.Manage.Modules.Rules.RulesetsEditorCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/data-model", PD.Manage.Modules.DataModelCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/data-model/context-menu", PD.Manage.Modules.DataModelCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/entity-management", PD.Manage.Modules.Entities.EntityManagementCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/entity-management/context-menu", PD.Manage.Modules.Entities.EntityManagementCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/selection-list", PD.Manage.Modules.SelectionListCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/selection-list/context-menu", PD.Manage.Modules.SelectionListCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/document-type", PD.Manage.Modules.DocumentTypeCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/document-type/context-menu", PD.Manage.Modules.DocumentTypeCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/mail-queue", PD.Manage.Modules.MailQueueCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/mail-queue/context-menu", PD.Manage.Modules.MailQueueCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/recycle-bin", PD.Manage.Modules.RecycleBinCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/recycle-bin/context-menu", PD.Manage.Modules.RecycleBinCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/reports", PD.Manage.Modules.ReportsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/reports/context-menu", PD.Manage.Modules.ReportsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/groups", PD.Manage.Modules.GroupsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/groups/context-menu", PD.Manage.Modules.GroupsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/users", PD.Manage.Modules.UsersCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/users/context-menu", PD.Manage.Modules.UsersCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/view", PD.Manage.Modules.ViewCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/view/context-menu", PD.Manage.Modules.ViewCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/data-load", PD.Manage.Modules.DataLoadCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/data-load/context-menu", PD.Manage.Modules.DataLoadCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/modules/context-menu", PD.Manage.ModuleCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/eventlog", PD.Manage.Modules.EventCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/eventlog/context-menu", PD.Manage.Modules.EventCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/tags", PD.Manage.Modules.TagsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/tags/context-menu", PD.Manage.Modules.TagsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/expressions", PD.Manage.Modules.ExpressionsCommands);
    PD.Commands.CommandRegistrar.registerGroup("pd/expressions/context-menu", PD.Manage.Modules.ExpressionsCommands);

});