/**
 * Javascript
 * 
 * @author     banhthidiem <banhthidiem@gmail.com>
 * @copyright  2007 Bach Khoa Computer Inc.
 * @version    $Id: ManagerTimer$BTD.js, v1.0 2007/11/1
 */

ActionTimer = function(id, cmdAction, timeDo)
{
	this.id = id;
	this.cmdAction = cmdAction;
	this.timeDo = timeDo;
	this.nextTime = 0;
};

function ManagerTimer$BTD(myName, listActions)
{
	this.myName = myName;
	this.listActions = [];
	if (typeof listActions != "undefined") this.listActions = listActions;
	this.oTimer = null;
	this.timerCur = 0;
};

ManagerTimer$BTD.prototype = 
{
	addAction : function(action)
	{
		action.nextTime = action.timeDo + this.timerCur; 
		this.listActions[action.id] = action;
	},
	
	removeAction : function(action)
	{
		if (this.listActions[action.id])
		{
			delete this.listActions[action.id];
		}
	},
	
	doAction : function()
	{
		for (var id in this.listActions)
		{
			var action = this.listActions[id];
			if (action.nextTime == this.timerCur)
			{
				// setTimeout("eval(\"" + action.cmdAction + "\");", 0);
				// action.cmdAction();
				eval(action.cmdAction);
				action.nextTime = action.timeDo + this.timerCur;
			}
		}
	},
	
	clearTimer : function()
	{
		if(this.oTimer)
		{
			clearTimeout(this.oTimer);
			this.oTimer = null;
		}
	},

	run : function()
	{
		this.doAction();
		this.timerCur++;
		this.oTimer = setTimeout(this.myName + ".run();", 10);
	}
};