(function () {
    window.techDays = { handler: {}, img: {} };

    // techDays.initialize
    (function() {
        var initialized = false;
        techDays.initialize = function() {
            initialized = initialized || (
                techDays.ready(),
                Microsoft.Dynamics.NAV.InvokeExtensibilityMethod("ControlReady", []),
                true);
        };
    })();

    // techDays.require
    (function () {
        techDays.handler.require = function (data) {
            var script = document.createElement("script");
            script.id = data.id;
            script.text = data.script;
            script.onload = function () {
                requireAL.complete(data.id);
            };
            document.body.appendChild(script);
        };
    })();

    // Defining subscope for techDays.methodCall
    (function() {
        techDays.methodCall = function(call) {
            if (!call.hasOwnProperty("method"))
                throw new Error("An invalid call has been made by C/AL. Data:" + JSON.stringify(call));
            if (typeof techDays.handler[call.method] !== "function")
                throw new Error("An unknown call has been made by C/AL: [" + call.method + "]");

            var handler = techDays.handler[call.method];
            if (typeof handler !== "function")
                throw new Error("Invalid configuration for C/AL call handler [" + call.method + "]. This is a programming bug, not a user error.");

            handler(call);
        };
        window.MethodCall = techDays.methodCall;
    })();

    // techDays.ready
    (function () {
        techDays.ready = function () {
            c$ = $(document.body).empty();
            c$.append("<div class=\"loading\"><div>Loading, please wait...</div></div>");
        };
    })();

    // techDays.handler.showDialog
    (function () {
        techDays.handler.showDialog = function (data) {
            alert(data.caption, data.message);
        };
    })();

    // techDays.handler -> sendSript, sendStyle, sendHtml, sendImage
    (function () {
        techDays.handler.sendScript = function (data) {
            setTimeout(function () {
                $ && $(document.body).append("<script" + (data.id && (" id=\"" + data.id + "\"")) + ">" + data.script + "</script>");
            });
        };
        techDays.handler.sendStyle = function (data) {
            setTimeout(function () {
                $ && $(document.body).append("<style type=\"text/css\">" + data.style + "</style>");
            });
        };
        techDays.handler.sendHtml = function (data) {
            setTimeout(function () {
                data.empty && c$ && c$.empty();
                c$ && c$.append(data.html);
            });
        };
        techDays.handler.sendImage = function (data) {
            techDays.img[data.name] = data.dataUri;
        };
    })();

    // Navigating away behavior
    (function () {
        var unloaded = false;
        function unload() {
            unloaded || (unloaded = true, Microsoft.Dynamics.NAV.InvokeExtensibilityMethod("CloseRequested", []));
        };

        window.top.onbeforeunload = unload;
        window.onbeforeunload = unload;
    })();
})();

