/*
 * $Id: init.js 38592 2008-05-29 22:33:42Z mmoore $
 * $URL: https://build/subversion/DestinationSearch/ds-core-web/tags/ds-core-web-2.0.115/src/main/webapp/js/init.js $
 */


/**
 * parse urls
 * @class LMI.Url
 */
LMI.Init = (function() {
    var el,
        inited = false,
        funcs = [],
        I = {
        /**
         * @method addFunction
         * @param func the function to call
         * @param pri priority, lower numbers run before higher numbers
         */
        addFunction: function( func, pri ) {
            pri = pri || 50;
            var i = funcs.length - 1;
            while( i >= 0 && funcs[i][1] > pri ) {
                --i;
            }
            funcs.splice( i + 1, 0, [ func, pri ] );
        },
        /**
         * run all the init functions and destroy the init array
         * @method init
         * @private
         */
        init: function() {
            // quit if this function has already been called
            if( inited ) {
                return;
            }
            inited = true;

            var err = null;

            for( var i = 0; i < funcs.length; ++i ) {
                try {
                    funcs[i][0]();
                } catch( e ) {
                    if( ! err ) {
                        err = e;
                    }
                }
            }
            funcs = null;  // clean up to free up some memory
            if( err ) {
                throw( err );
            }
        }
    };

    // try to run the functions after the dom tree is ready, but before the onload event
    if( document.addEventListener ) {
        document.addEventListener( "DOMContentLoaded", I.init, null );
    } else if( document.all && ! window.opera ) {
        document.write('<scr'+'ipt id="_lm_init_check" defer="true" src="//:"><'+'/script>');
        el = document.getElementById ("_lm_init_check" );
        if (el) {
            el.onreadystatechange = function() {
                if( 4 === this.readyState ) {
                    el.parentNode.removeChild( el );
                    I.init();
                }
            };
        }
    }

    return I;
})();

// fall back for browsers that don't support the above methods
window.onload = LMI.Init.init;
