/**
 * ON LOAD HANDLER (Oktavilla 2007)
 */
var OnLoadHandler = {
    isExecutingPageFunctions: false,
    functions: [],

    /**
     * appendFunction
     * Appends an on load function. The only method that should be used externally.
     * @param funcAsString The function to be executed, as a string (ie "func()")
     * @param executeOnDOMLoad Should the function be executed when the DOM (true) or when the page (false) is loaded 
     */
    appendFunction: function(funcAsString, executeOnDOMLoad) {
        this.functions.push(new OLH_Function(funcAsString, executeOnDOMLoad));
    },
    
    /**
     * executeDomFunctions
     * Starts the execution for all onDOMLoad functions
     */
    executeDOMFunctions: function() {
        if (!this.isExecutingPageFunctions) {
            OnLoadHandler.executeFunctions();
        }
    },

    /**
     * executePageFunctions
     * Starts the execution for all onPageLoad functions
     */
    executePageFunctions: function() {
        OnLoadHandler.isExecutingPageFunctions = true;
        OnLoadHandler.executeFunctions();
    },
    
    /**
     * executeFunctions
     * Executes all functions
     */
    executeFunctions: function() {
        for (var i = 0; i < this.functions.length; i++) {
            if (!this.functions[i].isExecuted && (this.functions[i].executeOnDOMLoad || this.isExecutingPageFunctions)) {
                this.functions[i].execute();
            }
        }        
    }
}

 /**
 * OLH_Function
 * A Load Function instance
 * @param funcAsString The function to be executed, as a string (ie "func()")
 * @param executeOnDOMLoad Should the function be executed when the DOM (true) or when the page (false) is loaded 
 */
function OLH_Function(funcAsString, executeOnDOMLoad) {
    this.funcAsString = funcAsString;
    this.executeOnDOMLoad = executeOnDOMLoad;
}
OLH_Function.prototype.execute = function() {
    this.isExecuted = true;
    eval(this.funcAsString);
}

/**
 * These lines adds the OnLoadHandler's functions to the different event handlers (depending on the client's browser)
 */

// for Mozilla
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", OnLoadHandler.executeDOMFunctions, false);
}
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        OnLoadHandler.executeDOMFunctions(); // call the onload handler
    }
};
/*@end @*/
// for Safari
if (/WebKit/i.test(navigator.userAgent)) {
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            clearInterval(_timer);
            OnLoadHandler.executeDOMFunctions(); // call the onload handler
        }
    }, 10);
}
window.onload = OnLoadHandler.executePageFunctions;