var ERH = new Array();
var ERH_Event = new Array();
var ERH_Delay = new Array();
var ERH_Interval = new Array();
var ERH_IntervalChanger = new Array();

/* A Dependant Function to create an automatic Event Repeater that customizes delay, interval and intervalChangerFunction.
Author : Alaa Sarhan (2010).
Depends On:
	- the functions { FireEventRepeater}.
	- the array { ERH, ERH_Event, ERH_Delay, ERH_Interval, ERH_IntervalChanger}.
Params:
	id : the unique identifier of the Event Repeater (ER).
	eventHandler : the function that handles the event that will be called by this ER. The ER id will be passed as an argument to this function.
	delay : The delay time (in milliseconds) before firing the event the first time after it has been stopped or hasn't started yet.
	interval : The Interval (in milliseconds) between successive event fires.
	intervalChanger : if set to a function, then it will be called each time the event is fired and the interval will be set to the returned
					  value. The current Interval will be sent as a parameter to this function.
	fireImmediatly : a boolean value that determines whether to fire the event for the first time immediatly after the creation or not.
Returns:
	A value of True on success, or an error message on error.
Error:
	the function return an error message on one of the following cases:
	- the given id is assigned to another ER.
	- the eventHandler is null or not a function
*/

function CreateEventRepeater(id, eventHandler, delay, interval, intervalChanger, fireImmediatly)
{
	if(id in ERH)
		return "Error! The id " + id + " is assigned to another Event Repeater";
	
	if(typeof(eventHandler) != "function")
		return "Error! The eventHandler can't be null and must be a function";
		
	if(delay < 0)
		delay = 0;
	
	if(interval < 1)
		interval = 1;
	
	ERH[id] = null;
	ERH_Event[id] = eventHandler;
	ERH_Delay[id] = delay;
	ERH_Interval[id] = interval;
	
	if(typeof(intervalChanger) == "function")
		ERH_IntervalChanger[id] = intervalChanger;
		
	if(fireImmediatly)
		FireEventRepeater(id);
		
	return true;
}

/* A Dependant Function to Fire an Event Repeater with the passed id if it is stopped or hasn't been started before.
Author : Alaa Sarhan (2010).
Depends On:
	- the functions { IsFired}.
	- the arrays { ERH}.
Params:
	- id : the id of the ER to Fire.
Returns:
	- True on success, Error Message on failure
Errors:
	An Error Message is returned on the following cases:
	- An ER with the passed id does not exist.
*/
function FireEventRepeater(id)
{
	if(!(id in ERH))
		return "Error! ER with id " + id + " does not exist";
	
	if(IsFired(id))
		return true;
		
	ERH[id] = setTimeout("RepeatEvent('" + id + "');", ERH_Delay[id]);
	
	return true;
}

/* A Dependant Function that checks whether an ER with the passed id is fired or not.
Author : Alaa Sarhan (2010).
Depends On:
	- the arrays { ERH}.
Params:
	- id : the id of the ER.
Returns:
	- if an ER with the passed id exists, a value of True is returned if the ER is Fired, else a value of False is returned.
Errors:
	An Error Message is returned on the following cases:
	- An ER with the passed id does not exist.
*/
function IsFired(id)
{
	if(!(id in ERH))
		return "Error! ER with id " + id + " does not exist.";
		
	if(ERH[id] != null)
		return true;
	else
		return false;
}

/* A Dependant Function to Put Out an Event Repeater with the passed id if it is Fired and Repeating.
Author : Alaa Sarhan (2010).
Depends On:
	- the functions { IsFired}.
	- the arrays { ERH}.
Params:
	- id : the id of the ER to Fire.
Returns:
	- True on success, Error Message on failure
Errors:
	An Error Message is returned on the following cases:
	- An ER with the passed id does not exist.
*/
function PutOutEventRepeater(id)
{
	if(!(id in ERH))
		return "Error! ER with id " + id + " does not exist";
	
	if(!IsFired(id))
		return true;
	
	clearTimeout(ERH[id]);
	ERH[id] = null;
	
	return true;
}

/* A Dependant Function that Destroys a previously created ER.
Author : Alaa Sarhan (2010).
Depends On:
	- the functions { IsFired}.
	- the arrays { ERH, ERH_Event, ERH_Delay, ERH_Interval, ERH_IntervalChanger}.
Params:
	- id : the id of the ER.
Returns:
	- True on success, Error Message on failure.
Errors:
	An Error Message is returned on the following cases:
	- An ER with the passed id does not exist.
*/
function DestroyEventRepeater(id)
{
	if(!(id in ERH))
		return "Error! ER with id " + id + " does not exist";
	
	PutOutEventRepeater(id);
	result = false;
	result = result || delete(ERH[id]);
	result = result || delete(ERH_Event[id]);
	result = result || delete(ERH_Delay[id]);
	result = result || delete(ERH_Interval[id]);
	result = result || delete(ERH_IntervalChanger[id]);
	
	return result;
}

/* A Dependant Function that is used to Handle and Repeat the Event Time outs.
Author : Alaa Sarhan (2010).
Depends On:
	- the functions { IsFired}.
	- the arrays { ERH_Event, ERH_Interval, ERH_IntervalChanger}.
*/
function RepeatEvent(id)
{
	if(IsFired(id) === true)
	{
		if(!ERH_Event[id](id))
		{
			DestroyEventRepeater(id);
			return;
		}
		
		ERH[id] = setTimeout("RepeatEvent('" + id + "')", ERH_Interval[id]);
		
		if(id in ERH_IntervalChanger)
			ERH_Interval[id] = ERH_IntervalChanger[id](ERH_Interval[id], id);
	}
}

