/*
	App Core
*/
var Core = function(){
    // Variable privadas
    var debug = true;
    var modules = {};
    var sandboxInstance = null;

    function init(){
        sandboxInstance = new Sandbox(this);
    }

    // Cremos la instancia
	function createInstance(moduleID){
        var instance = modules[moduleID].creator(sandboxInstance),
		name, method;

		if (!debug) {
			for (name in instance){
				method = instance[name];
				if (typeof method == "function") {
					instance[name] = function(name, method) {
						return function(){
							try { return method.apply(this, arguments);	}
							catch(ex) { log(1, name + "(): " + ex.message);	}
						};
					};
				}
			}
		}
		return instance;
	}

    // Método públicos
    return {
        register: function(moduleID, creator) {
            modules[moduleID] = {
            creator: creator,
            instance: null
            };
        },
        start: function(moduleID) {
            modules[moduleID].instance = createInstance(moduleID);
            modules[moduleID].instance.init();
        },
        stop: function(moduleID){
            var data = modules[moduleID];
            if (data.instance) {
                data.instance.destroy();
                data.instance = null;
            }
        },
        startAll: function(){
            init();
            for (var moduleID in modules) {
                if (modules.hasOwnProperty(moduleID)) {
                    this.start(moduleID);
                }
            }
        },
        stopAll: function() {
            for (var moduleID in modules) {
                if (modules.hasOwnProperty(moduleID)) {
                    this.stop(moduleID);
                }
            }
        },
        getModules: function(){
            return modules;
        }
    };
}();

