/**
 * NM_EventLoader.js
 *
 * classes for handling onload/domready events
 *
 * @author	Steffen Heinzelmann <shi@dmc.de>
 * @version $Id: NM_EventLoader.js 87094 2011-12-05 13:14:54Z sheinzelmann $
 **/


/**
 * handling and fireing of event objects.
 *
 * @author	Steffen Heinzelmann <shi@dmc.de>
 * @version	$Id: NM_EventLoader.js 87094 2011-12-05 13:14:54Z sheinzelmann $
 */
var NM_EventLoader = {

	/**
	 * mootools hash object
	 *
	 * @var object objHash
	 */
	objHash: new Hash(),

	/**
	 * priority array
	 *
	 * @var array arrPrio
	 */
	arrPrio: [],

	/**
	 * register events
	 *
	 * @param 	string strKey
	 * @param 	object objEvent (NM_Event)
	 * @return 	void
	 */
	register: function( strKey, objEvent )
	{
		this.arrPrio.push( objEvent.priority + '_' + strKey );
		this.objHash.set( strKey, objEvent );
	},

	/**
	 * fire all event objects
	 *
	 * @return void
	 */
	fire: function()
	{
		var strEvent = ( 'https:' == document.location.protocol ) ? 'load' : 'domready';

		window.addEvent( strEvent, function() {

			NM_Console.time( new NM_ConsoleMsg( 'EventLoader', 'execution time' ) );

			NM_Console.group( new NM_ConsoleMsg( 'EventLoader', 'fire (event: '+strEvent+' / registered calls: '+this.objHash.length+')' ) );

			this.arrPrio.sort();

			this.arrPrio.each( function( strKey, intIndex )
			{
				var objEvent	= this.objHash.get( strKey.substr( strKey.indexOf( '_' ) + 1 ) );

				NM_Console.info( new NM_ConsoleMsg( '', '> '+strKey + ' prio: ' + objEvent.priority ) );

				try
				{
					if( $type( objEvent.param ) == 'object' )
					{
						var mixParam = Json.toString( objEvent.param );
					}
					else
					{
						var mixParam = objEvent.param;
					}

					eval( objEvent.object+'.'+objEvent.method+'('+mixParam+')' );

					/**
					 * timer
					 * If you want, that your event will be effected automatically
					 * after a few microseconds, you can define a timer for the
					 * event.
					 * If the event object have a valid timer object, the timer
					 * will be registered.
					 */
					if( $type( objEvent.timer ) == 'object' )
					{
						//NM_Console.dir( new NM_ConsoleMsg( '', '> '+strKey+ ' > timer: ' ), objEvent.timer );
						eval( objEvent.object+'.'+objEvent.timer.method+'.bind('+objEvent.object+').delay('+objEvent.timer.ms+')' );
					}
				}
				catch( e )
				{
					NM_Console.error( new NM_ConsoleMsg( 'EventLoader', 'fire: > ' + strKey + ' : ' + e ) );
				}

			}.bind( this ) );

			NM_Console.groupEnd();

			NM_Console.timeEnd( new NM_ConsoleMsg( 'EventLoader', 'execution time' ) );

		}.bind( this ) );

	},

	/**
	 * get all  registered object names
	 *
	 * @return array
	 */
	getRegisteredObjectNames: function()
	{
		return this.objHash.keys();
	}

};//end: NM_EventLoader



/**
 * Event objects handled by the event loader object
 *
 * @author	Steffen Heinzelmann <shi@dmc.de>
 * @version	$Id: NM_EventLoader.js 87094 2011-12-05 13:14:54Z sheinzelmann $
 */
var NM_Event = new Class( {

	/**
	 * object name
	 * @var string object
	 */
	object: null,

	/**
	 * object name
	 * @var string object
	 */
	method: '',

	/**
	 * object name
	 * @var string object
	 */
	param: {},

	/**
	 * priority
	 * @var integer priority
	 */
	priority: 30,

	/**
	 * timer object
	 * If you want, that your event will be effected automatically
	 * after a few microseconds, you can define a timer for the
	 * event.
	 *
	 * method: the method that shoul be effected
	 * ms: micro seconds
	 *
	 * @var object timer
	 */
	timer: { method: '', ms: 0 },

	/**
	 * constructor
	 *
	 * @param string	strObject	(component object name)
	 * @param string	strMethod	(method, wich will be called after the "onload")
	 * @param object	objParam	(Json objet for the called method)
	 * @param object	objTimer	(Jsont timer object, for calling a method after a time period)
	 * @param integer	intPriority	priority (10(highest)-99(lowest), default: 30)
	 * @return void
	 */
	initialize: function( strObject, strMethod, objParam, objTimer, intPriority )
	{

		this.object		= strObject;
		this.method		= strMethod;
		this.param		= objParam;
		this.timer		= objTimer;

		var intPriority = parseInt( intPriority );

		if( false == isNaN( intPriority )
				&& intPriority > 9
					&& intPriority < 100
						)
		{
			this.priority = intPriority;
		}
	}

} );
