﻿var oBootLoader = null;

function BootLoader()
{
    this.ready = false;
    this.arrClassesToLoad = new Array();

    this.bulkIncludeScripts = true;
    this.oIncludedScripts = document.createDocumentFragment();
    
    // Used to register classes that have completely loaded
    this.classRegistry = new Array();
    
    this.onreadystatechange = function(){};
}

BootLoader.prototype = 
{
    registerClass : function(cn)
    {
        var bRegistered = false;
        
        // Check that this class is not already registered
        for(var i = 0; i < this.classRegistry.length; i++)
        {
            if(this.classRegistry[i] == cn)
            {
                bRegistered = true;
            }
        }
        
        // If this class has not already registered itself
        // then register it
        if(bRegistered == false)
        {
            this.classRegistry.push(cn);
        }
    },
    
    includeJavascript : function(src, bi)
    {   
        if (document.createElement && document.getElementsByTagName)
        {   
            var head_tag = document.getElementsByTagName('head')[0];   
            var script_tag = document.createElement('script');   
            
            script_tag.setAttribute('type', 'text/javascript');   
            script_tag.setAttribute('src', "js/" + src);


            if (bi == true)
            {
                this.oIncludedScripts.appendChild(script_tag);
            }
            else
            {
                head_tag.appendChild(script_tag);
            }
        }   
    },

    includeJavascripts: function ()
    {
        if (document.createElement && document.getElementsByTagName) 
        {
            var head_tag = document.getElementsByTagName('head')[0];

            head_tag.appendChild(this.oIncludedScripts);
        }
    },
    
    checkLoadCompleted : function()
    {
        // If the number of registered classes equals the the number 
        // of classess that were expected to load then the load is complete
        // otherwise continue to check for loading to finish
        
        if(this.classRegistry.length >= this.arrClassesToLoad.length)
        {
            this.ready = true;

            this.onreadystatechange();
        }
        else
        {
            this.ready = false;
            setTimeout(function(){oBootLoader.checkLoadCompleted();}, 500); 
            
            this.onreadystatechange();
        }
    },
    
    init : function()
    {
        // Activate Load Complete Listener
        this.checkLoadCompleted();
        
        // Include required javascript files
        for(var i = 0; i < this.arrClassesToLoad.length; i++)
        {
            this.includeJavascript(this.arrClassesToLoad[i] + ".js", true);
        }

        if (this.bulkIncludeScripts == true)
        {
            this.includeJavascripts();
        }
    }
}

var oBootLoader = new BootLoader();
    
