﻿Core = function() {
    var moduleData = {};
    var options = {};

    function createInstance(moduleId, options) {
        try {
            if (typeof options == 'undefined') options = new Array();
            var instance =
        moduleData[moduleId].creator(new Sandbox(this), options), 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); }
                            };
                        } (name, method);

                    }
                }

            }
            return instance;
        } catch (ex) {
            if (debug) {
                throw ex;
            } else {
                log(2, "createInstance() failed");
            }
        }
    }

    /*
    * display error message
    */
    function alertError(error) {
        txt = "Sorry, there was an error on this page.\n\n";
        txt += "Error description: " + error + "\n\n";
        txt += "Click OK to continue.\n\nThen refresh your page.";
        alert(txt);
    }

    return {
        includeFile: function(path, returnFunction) {
            try {
                $.ajax({
                    url: path,
                    dataType: "script",
                    async: false,
                    success: returnFunction
                });
            } catch (ex) {
                if (debug) {
                    throw ex;
                } else {
                    log(2, "includeFile: failed to load file: " + path);
                }
            }
        },
        register: function(moduleId, creator) {
            moduleData[moduleId] = {
                creator: creator,
                instance: null
            };
        },
        start: function(moduleId, options) {
            moduleData[moduleId].instance =
            //moduleData[moduleId].creator(new Sandbox(this));
            createInstance(moduleId, options);
            try {
                moduleData[moduleId].instance.init();
            } catch (ex) {
                if (debug) {
                    throw ex;
                } else {
                    log(2, "module " + moduleId + "failed to initialise");
                }
            }
        },
        stop: function(moduleId) {
            var data = moduleData[moduleId];
            if (data.instance) {
                data.instance.destroy();
                data.instance = null;
                delete data.instance;
            }
        },
        startAll: function() {
            for (var moduleId in moduleData) {
                if (moduleData.hasOwnProperty(moduleId)) {
                    this.start(moduleId);
                }
            }
        },
        stopAll: function() {
            for (var moduleId in moduleData) {
                if (moduleData.hasOwnProperty(moduleId)) {
                    this.stop(moduleId);
                }
            }
        },
        debug: function(message) {
            if (debug) (window.console) ? console.log(message) : alertError(message);
        },
        log: function(severity, message) {
            // post to db or log file - build ajax method into core for this
            this.debug("Severity: " + severity + " | Message: " + message);
        }
    }
} ();


// last chance error catching
window.onerror = function(message, url, line) {
    if (debug) {
        return false;
    } else {
        Core.log(1, message);
        return true;
    }
};
Core.includeFile("/Js/JAF/Sandbox.js");
