/*
 * $Id: init.js 3820 2006-10-03 20:52:34Z jvinding $
 * $URL: https://chow/DestinationSearch/ds-core-web/tags/ds-core-web-1.5.12.1/src/main/webapp/js/init.js $
 * Init functions
 */


if( typeof DSInit === 'undefined' ) {
    window.DSInit = {};

    DSInit.initFunctions = [];

    DSInit.addFunction = function( func, pri ) {
        pri = pri || 50;
        var f = DSInit.initFunctions;
        var i = f.length - 1;
        while( i >= 0 && f[i][1] > pri ) { --i; }
        f.splice( i + 1, 0, [ func, pri ] );
    };
    /*
     * Init function, called when page is done loading
     */
    DSInit.Init = function() {
        // quit if this function has already been called
        if (arguments.callee.done) { return; }

        // flag this function so we don't do the same thing twice
        arguments.callee.done = true;

        if( typeof DOM2Event !== 'undefined' ) {
            DOM2Event.initRegistration();
        }


        var err = null;

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

        if( typeof DSLink !== 'undefined' ) {
            try {
                DSLink.addBehaviours();
            } catch( e ) {
                if( ! err ) {
                    err = e;
                }
            }
        }
        if( err ) {
            throw( err );
        }
    };


    /*
     * window.onload replacement
     * calls init function when the DOM tree is loaded and parsed
     */

    // moz
    if( document.addEventListener ) {
        document.addEventListener( "DOMContentLoaded", DSInit.Init, null );
    }

    // IE
    /*@cc_on
        @if (@_jscript_version >= 5.6)
           document.write( "<script defer src='/js/ie_init.js'><"+"/script>" );
        @end
    @*/

    // everyone else
    window.onload = DSInit.Init;
}