// Geek Web Design Javascript Timed Actions Framework
// Copyright 2006, Stephen Terhune (steve@geekwebdesign.com)

// ---------- action object ----------
	
	function _Action_Start(replace)
	{
		if (replace == true)
		{
			for (var i = 0; i < this.ActionList.length; i++)
			{
				if (this.ActionList[i] && this.ActionList[i].ObjPointer == this.ObjPointer)
					this.ActionList[i].Stopped = true;
			}
		}
		this.Stopped = false;
		this.ContinueAction();
	}
	
	function _Action_Continue()
	{
		if (this.Stopped != true)
		{
			try
			{
				this.Method();
				var me = this;
				setTimeout(function(){me.ContinueAction();},this.Frequency);
			}
			catch (err)
			{
				this.Stopped = true;
				this.ReportError(err);
			}
		}
	}
	
	function _Action_Error(err)
	{
		if (this.ErrorContainer != null)
			this.ErrorContainer.innerHTML += err.description + "<br>";
	}
	
	function Action(actionList, objPointer, method, frequency, args)
	{
		this.Stopped = true;						// indicates a stopped action - set to true to cancel an action
		this.ID = 0;										// can be used to identify this action, when set
		this.ActionList = actionList;		// pointer to the parent array that this action is part of
																		// used for lookup when actions are started in order to prevent
																		// more than one action per pointer object
		this.ObjPointer = objPointer;		// pointer to the object that will be affected by this action
		this.Method = method;						// method to run each time the action happens
		this.Args = args;								// arguments to use with this.Method
		this.Frequency = frequency;			// how often in milliseconds to run this action
		this.ErrorContainer = null;     // what container to write error messages to
	}
  Action.prototype.Start = _Action_Start;
	Action.prototype.ContinueAction = _Action_Continue;
	Action.prototype.ReportError = _Action_Error;
	
// ---------- action manager ----------
	
	function _ActionManager_AddAction(objPointer, method, frequency, args)
	{
		var objAction = new Action(this.ActionList, objPointer, method, frequency, args);
		objAction.ID = this.NextActionID++;
		this.ActionList[this.ActionList.length] = objAction;
		return objAction;
	}
	
	function _ActionManager_RemoveAction(actionID)
	{
		for (var i = 0; i < this.ActionList.length; i++)
		{
			if (this.ActionsList[i] && this.ActionList[i].ID == actionID)
			{
				delete this.ActionList[i];
				break;
			}
		}
	}
	
	function _ActionManager_StartAction(actionID, replace)
	{
		for (var i = 0; i < this.ActionList.length; i++)
		{
			if (this.ActionList[i] && this.ActionList[i].ID == actionID)
			{
				this.ActionList[i].Start(replace);
				break;
			}
		}
	}
	
	function ActionManager()
	{
		this.ActionList = new Array();
		this.NextActionID = 0;
	}
  ActionManager.prototype.AddAction = _ActionManager_AddAction;
  ActionManager.prototype.RemoveAction = _ActionManager_RemoveAction;
  ActionManager.prototype.StartAction = _ActionManager_StartAction;

