/// class PD.Plugins.PluginRouter

PD.Plugins.PluginRouter =
Class.extend({
    
    init: function () {
        this.addRoutes();
    },

    routeHandler: function (e) {

        var plugin = null;
        var module = null;
        var pluginId = this.params["pluginId"];
        var moduleId = this.params["moduleId"];
        var fqnId = pluginId + (moduleId == null ? "" : ":" + moduleId);
        
        if (pluginId == null) {
            return;
        }

        plugin = PD.Plugins.PluginManager.getPlugin(pluginId);

        if (plugin != null) {
            if (moduleId == null) {
                module = plugin.get_scripts().get_default();
            }
            else {
                module = plugin.get_scripts().get(moduleId);
            }
        }
        
        if (module == null) {
            return;
        }

        module.route = window.location.hash;

        PD.context
            .get_global()
            .get_events()
            .publish("Module:Request", $.extend({}, module, {
                id: fqnId
            }));

    },

    addRoutes: function () {

        Path.map("#/plugin/:pluginId(/:moduleId)").to(this.routeHandler);

    }

});