﻿// ---------- SCRIPT MANAGER ---------- //

/* To use this file, common.js should be included! */

//(function (window, undefined) {

    function ScriptManager() {

        this.ScriptFileCollection = [];
        this.LoadedScriptFileCollection = [];

        this.InjectedFunctions = [];
    }

    ScriptManager.prototype.GetScriptText = function (parElement) {
        if (parElement && parElement.innerHTML)
            return $('<pre/>').html(parElement).text();
        else
            return null;
    }

    ScriptManager.prototype.FindScriptFile = function (parFileName, parFileCollection) {
        if (!parFileCollection) parFileCollection = this.ScriptFileCollection;

        var varScriptFile = null;
        // TODO GEERT : tolowercase + add '/scripts/'
        for (var varIterator = 0; varIterator < parFileCollection.length; varIterator++) {
            if (parFileCollection[varIterator].FileName == parFileName) {
                varScriptFile = parFileCollection[varIterator];
                break;
            } // if (this.ScriptFileCollection[varIterator].FileName == parFileName){ 
        } // for (var varIterator = 0; varIterator < this.ScriptFileCollection.length; varI++)
        return varScriptFile;
    }; // ScriptManager.prototype.FindLoadScript

    ScriptManager.prototype.AddScriptFile = function (parFileName, parinnerHTML) {
        if (!this.FindScriptFile(parFileName, this.ScriptFileCollection))
            this.ScriptFileCollection.add(new ScriptFile(parFileName, parinnerHTML));
    }; // ScriptManager.prototype.AddScript




    ScriptManager.prototype.ExecuteRequestCompleteScript = function () {

        var varScriptFileCount = this.ScriptFileCollection.length;

        this.InjectFiles();

        if (this.ScriptFileCollection.length == varScriptFileCount)
            this.ExecuteFilesLoadedScript();

    } // function ExecuteRequestCompleteScript()

    ScriptManager.prototype.ExecuteFilesLoadedScript = function () {

        this.InjectFunctions();
        this.InjectScripts();
    }

    // InjectFiles = function to inject script file tag into the head section of the page
    ScriptManager.prototype.InjectFiles = function () {

        var varFilesToInclude;
        varFilesToInclude = $('pre.javascriptfile');
        for (var i = 0; i < varFilesToInclude.length; i++) {
            if (!varFilesToInclude[i].disabled) {
                this.AddScriptFile(varFilesToInclude[i].getAttribute('name'), this.GetScriptText(varFilesToInclude[i]));
            }
        }

        $('pre.javascriptfile').remove();

        if (this.ScriptFileCollection.length == 0)
            this.ExecuteFilesLoadedScript();
    }


    // InjectFunctions = function to globally register functions
    ScriptManager.prototype.InjectFunctions = function () {
        var varFunctionsToInject;
        var varFunctionAlreadyInjected;

        varFunctionsToInject = $('pre.javascriptfunction');
        for (var i = 0; i < varFunctionsToInject.length; i++) {
            varFunctionAlreadyInjected = false;

            // find out if the function was already injected
            for (var j = 0; j < this.InjectedFunctions.length; j++) {
                if (this.InjectedFunctions[j] == varFunctionsToInject[i].id) {
                    varFunctionAlreadyInjected = true;
                    break;
                } // if (varInjectedFunctions[j] == varFunctionsToInject[i].name)
            }  // for (var j in varInjectedFunctions)

            // if not, inject it now
            if (!varFunctionAlreadyInjected) {
                new Function(this.GetScriptText(varFunctionsToInject[i]))();
                this.InjectedFunctions[this.InjectedFunctions.length] = varFunctionsToInject[i].id;
            } // if (!varFunctionAlreadyInjected)

        }  // for (var i in varFunctionsToInject)

        $('pre.javascriptfunction').remove();

    }  // ScriptManager.prototype.InjectFunctions



    // InjectScripts = function to execute scripts :)
    ScriptManager.prototype.InjectScripts = function () {
        var varScriptsToInject;

        varScriptsToInject = $('pre.javascript');
        for (var i = 0; i < varScriptsToInject.length; i++) {
            if (!varScriptsToInject[i].disabled) {
                new Function(this.GetScriptText(varScriptsToInject[i]))();
                varScriptsToInject[i].disabled = true;
            } // if (!varScriptsToInject[i].executed)
        }  // for (var i in varScriptsToInject)

        //$('div.javascript').remove();

    } // function InjectScripts()



    function ScriptFile(parFileName, parinnerHTML) {
        this.FileName = parFileName;

        this.Loaded = false;

        var varHeadElement = $("head")[0];
        var varScriptElement = document.createElement("script");

        var varRandomizer = new Date().getTime().toString();

        //varScriptElement.src = this.FileName + "?rnd=" + varRandomizer;
        varScriptElement.src = this.FileName;
        //alert("loading " + varScriptElement.src);
        varScriptElement.ScriptFile = this;
        try { varScriptElement.innerHTML = parinnerHTML; }
        catch (err) { varScriptElement.text = parinnerHTML; }
        varScriptElement.onload = varScriptElement.onreadystatechange = function () {

            if (!this.ScriptFile.Loaded && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {

                this.ScriptFile.Loaded = true;

                $.ScriptManager.LoadedScriptFileCollection.add(this.ScriptFile);

                if ($.ScriptManager.ScriptFileCollection.length == $.ScriptManager.LoadedScriptFileCollection.length)
                    $.ScriptManager.ExecuteFilesLoadedScript();

                // Handle memory leak in IE
                this.onload = varScriptElement.onreadystatechange = null;

            } // if (!this.ScriptFile.Loaded && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete"))

        }  // varScriptElement.onload = varScriptElement.onreadystatechange = function
        varHeadElement.appendChild(varScriptElement);
    }; // function ScriptFile()




    $.ScriptManager = new ScriptManager();




    $(document).ready(function () {

        $.ScriptManager.ExecuteRequestCompleteScript();

    });


//})(window);
