/* Last published: 10 Feb 2012 12:31 */
// Copyright 2009 Graduate Prospects
//
// This registers a function to be called when the onload event is triggered.
// Functions passed in successive calls are held in a queue and are invoked in
// order when the onload event is triggered.  Any exceptions thrown by these
// functions are caught to allow the other functions to be executed.  The first
// caught of these exceptions (if any) is thrown once all functions have been
// called.  Should FireBug be installed then the exceptions are all logged at
// the error level of console logging.
function addLoadEvent(func) {
    window.onload.lastCall.next = func
    window.onload.lastCall = func
}
// The onload function becomes a function that when invoked looks at its
// callChain property and invokes it and then recursively invokes the function
// in that function's next property.  When FireBug is in use this function logs
// all exceptions and never throws (to prevent duplicate log messages).  In all
// scenarios the first exception is thrown.
//
// Should there already be an existing onload handler then this is added onto
// the callChain via a call to addLoadEvent(func).  callChain and lastCall will
// both be null when the onload function exists.
var existingOnload = window.onload
window.onload = function() {
    var firstException
    var fn = window.onload.callChain
    while ( fn ) {
        try {
            fn()
        } catch (exception) {
            if ( window.console ) window.console.error(exception)
            else if ( !firstException ) firstException = exception
        }
        fn = fn.next
    }
    window.onload.callChain = null
    window.onload.lastCall = null
    if ( firstException ) throw firstException
}
window.onload.callChain = function() {}
window.onload.lastCall = window.onload.callChain
if ( existingOnload ) addLoadEvent( existingOnload )
