/*=========================================================================
** File:    onload-handlers.js
** Library: FSCMS.OnloadHandlers
** Author:  Jon Maybury <jmaybury@firesnacks.com>
** Date:    2007-07-12
**-------------------------------------------------------------------------
** This library provides a way for different scripts to attach functions
** to the OnLoad event without conflicting with each other.
**
** Wherever you would normally put window.onload = foo, instead put
** FSCMS.OnloadHandlers.Register(foo)
**
** DEV NOTE:
** There are, in fact, built-in JavaScript functions to do this.
** addEventListener() is FF-specific and attachEvent() is IE-specific
** (Lord only knows what, if anything, Opera etc. support).
** For one thing, I'm not sure how these methods will interact with
** direct window.onload assignments; and for another thing, "our" solution
** below is nicely cross-platform compatible.
*/

// Namespace object for FiresnacksCMS custom javascript stuff
if (typeof FSCMS == 'undefined'){
   var FSCMS = function(){};
}

FSCMS.OnloadHandlers = new function() {
   // Browser Detection
   var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
   var IEmac = ((document.all) && (isMac)) ? true : false;
   var IE4   = ((document.all) && (navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
   
   // List of registered onload functions
   var onload_handlers = new Array();

   function RunHandlers() {
         for (var i=0; i<onload_handlers.length; i++) {
            onload_handlers[i]();
         }
   };
   
   //-------------------------- Public methods --------------------------//
   return {
      Register : function(f) {
         if (IEmac && IE4) {
            // IE 4.5 blows out on testing window.onload
            window.onload = RunHandlers;
            onload_handlers.push(f);
         } else {
            if  (window.onload) {
               if (window.onload != RunHandlers) {
                  onload_handlers.push(window.onload);
                  window.onload = RunHandlers;
               }
               
               onload_handlers.push(f);
            } else {
               // If there are no previously-registered handlers, just attach this one in the normal way
               window.onload = f;
            }
         }
      }
   };
};

