/**
 * @fileoverview Script to manage server events.
 *
 * @author Enrico Orfert
 * @since 2007-12-04
 */

 /**
  * @class ServerEventManager class
  * @static
  *
  */
ServerEventManager =
{
	arrServerEvents : new Array(),

	objEventFunctions : new HashTable(),

	/**
	 * @private
	 */
	_strTargetUrl : '',

	LIFO : 1,
	FIFO : 2,

	/**
	 * Function to add an event to the event queue.
	 * @static
	 *
	 * @param (String) strEvent 		URL of the server event to add
	 * @param (String) objFunction		Function, which will be called, after the event was executed
	 * @param (Number) intMode			Mode of adding the event to the queue (LIFO, FIFO)
	 */
	addServerEvent : function ( strEvent, objFunction, intMode, bAsynchron)
	{
		bAsynchron  = bAsynchron == null ? false : bAsynchron;
		objFunction = objFunction === undefined ? null : objFunction;

		if ( strEvent.length == 0)
		{
			return;
		}

		if( intMode == null)
		{
			intMode = ServerEventManager.LIFO;
		}

		if ( bAsynchron && ServerEventManager._strTargetUrl.length)
		{
			strEvent = ServerEventManager._strTargetUrl + strEvent;
		}

		switch( intMode)
		{
			case ServerEventManager.LIFO:
				ServerEventManager.arrServerEvents.unshift( strEvent);
				break;

			case ServerEventManager.FIFO:
				ServerEventManager.arrServerEvents.push( strEvent);
				break;
		}

		var objHashTable_ = new HashTable();
		objHashTable_.set( 'function', objFunction);
		objHashTable_.set( 'asynchron', bAsynchron);

		ServerEventManager.objEventFunctions.set( strEvent, objHashTable_);
	},

	/**
	 * Function to execute the next event in the queue.
	 * @static
	 *
	 */
	executeServerEvents : function (arr)
	{
		if( ServerEventManager.arrServerEvents.length == 0)
		{
			if(arr instanceof Array)
			{
				runFuncs(arr);
			}
			return true;
		}
		strCurrentURL = ServerEventManager.arrServerEvents.shift();
		if ( strCurrentURL.length > 0)
		{
			var objHashTable_ = ServerEventManager.objEventFunctions.get( strCurrentURL);
			ScriptLoader.load( [strCurrentURL], objHashTable_.get('function'), true, Boolean(objHashTable_.get('asynchron')));
		}
		ServerEventManager.executeServerEvents(arr);
	},

	registerTargetUrl : function (strTargetUrl)
	{
		ServerEventManager._strTargetUrl = strTargetUrl;
	}
};
