var g_bRunOnceQueueOnlyPL = false;

if( typeof g_aRfgCommandQueue   == 'undefined') g_aRfgCommandQueue = new Array();
if( typeof g_bRfgOnLoadComplete == 'undefined') g_bRfgOnLoadComplete = false;

//Window event handlers
if( !window.onload || (window.onload.toString().indexOf( 'RfgOnPageLoad' ) == -1) ) {
   g_pOldOnLoad = window.onload;
   window.onload = RfgOnPageLoad;
}


/*----------------------------------------------------------------------\
| FUNCTION:    RfgQueueCommand                                          |
| RETURNS:     N/A                                                      |
| PARAMETERS:  Command text to later execute.                           |
| PURPOSE:     Requests a command be executed after the page has fully  |
|              loaded, or if the page is already loaded, now.           |
| ****HACKED VERSION**** DO NOT USE                                     |
\----------------------------------------------------------------------*/
function RfgQueueCommand( strEncodedCommand )
{
   var strCommand = null;
   if( typeof strEncodedCommand === "string" ) {
      strCommand = unescape(strEncodedCommand.replace(/\+/g," "));
   } else {
      strCommand = strEncodedCommand;
   }
   if( !g_bRfgOnLoadComplete ) {
      g_aRfgCommandQueue.push( strCommand );
   } else {
      eval( strCommand );
   }
}

/*----------------------------------------------------------------------\
| FUNCTION:    RfgOnPageLoad                                            |
| RETURNS:     N/A                                                      |
| PARAMETERS:  N/A                                                      |
| PURPOSE:     Internal document.onload handler -- saves previous       |
|              onload events and fires them after our onload events are |
|              complete.                                                |
\----------------------------------------------------------------------*/
function RfgOnPageLoad()
{
   if( g_bRunOnceQueueOnlyPL ) return;
   g_bRunOnceQueueOnlyPL = true;

   g_bRfgOnLoadComplete = true;
   while( g_aRfgCommandQueue.length > 0 ) {
      var strCmd = g_aRfgCommandQueue.shift();
      try{
	//This is a bad hack, and will not work with the Microsoft mapping services which is where this originates, since the command
	//Is sent to a WEB SERVICE, and round trips to the server and back before being executed.  You can not pass a pointer reference in
	//an XML file.  (sigh) this won't work.
            if(typeof strCmd==="string")
               eval(strCmd);
            else
               strCmd();
      } catch( xE ) {
         //Do not error on user defined code.
      }
   }

   if( g_pOldOnLoad ) g_pOldOnLoad();
}

/**
 * Manages the queue for onDomReady functions
 */
var RfgQueueOnDomReady = new function() {
   // The queued functions
   var aQueues = {};

   /**
    * Checks the status of IE to verify the state
    */
   var checkExplorerReadyState = function(fn) {
      // Dom is ready for interaction
      if ((document.readyState == "interactive") || (document.readyState == "complete")) {
         fn();
      }
   };

   /**
    * Attaches a function to the dom ready event
    */
   var attachFunction = function(fn) {
      if (document.addEventListener) {
         document.addEventListener('DOMContentLoaded', fn, false);
      } else if (document.attachEvent) {
         document.attachEvent('onreadystatechange', function(){
            checkExplorerReadyState(fn)
         });
      } else {
         // Unable to attach event
         throw('Browser unsupported.');
      }
   };

   /**
    * Start running through the queue
    * @param strQueueName
    */
   var executeQueue = function(strQueueName) {
      var aQueue = aQueues[strQueueName];
      for (var i = 0; i < aQueue.length; i++) {
         aQueue[i]();
      }
   };

   /**
    * Adds a function to the queue
    * @param fn the function to execute
    * @param strQueueName (optional) the name of the queue to add the item to
    */
   this.add = function(fn, strQueueName) {
      if (strQueueName) {
         // If the queue does not exist
         if (!aQueues[strQueueName]) {
            // Initialize the queue
            aQueues[strQueueName] = [];
            attachFunction(function() {
               executeQueue(strQueueName);
            });
         }

         // Add the item to the queue
         aQueues[strQueueName].push(fn);
      } else {
         attachFunction(fn);
      }
   }
}

