function Timer(parentObject)
{
	this.obj = parentObject || window;

	function getUnusedCollectionIndex()
	{
		var i = 0;
		while (Timer.Collection[i])
		{
			i++;
		}
		return i;
	}

	function buildCall(funcObj, i, func, funcArgs)
	{
		var callState = { obj : null, callString : "", args : funcArgs };
	    
		var t = "";
	    
		if (funcObj != window)
		{
			callState.obj = funcObj;
			t = "Timer.Collection[" + i + "].obj.";
		}
		t += func + "(";
	  
		if (funcArgs.length > 0)
		{
			t += "Timer.Collection[" + i + "].args[0]";
			for (var j = 1; j < funcArgs.length; j++)
			{
				t += ", Timer.Collection[" + i + "].args[" + j + "]";
			}
		}
		t += ");";
		callState.callString = t;
	    
		return callState;
	}
	
	function setInterval(func, msec)
	{
		var i = getUnusedCollectionIndex();
		var args = [];
		for (var j = 2; j < arguments.length; j++)
		{
			args[j - 2] = arguments[j];
		}
		Timer.Collection[i] = buildCall(this.obj, i, func, args);
		Timer.Collection[i].timer = window.setInterval(Timer.Collection[i].callString, msec);
		return i;
	}
	
	function setTimeout(func, msec)
	{
		var i = getUnusedCollectionIndex();
		var args = [];
		for (var j = 2; j < arguments.length; j++)
		{
			args[j - 2] = arguments[j];
		}
		Timer.Collection[i] = buildCall(this.obj, i, func, args);
		Timer.Collection[i].timer = window.setTimeout("Timer.callOnce(" + i + ");", msec);
		return i;
	}

	function clearInterval(i)
	{
		if (!Timer.Collection[i])
		{
			return;
		}
		window.clearInterval(Timer.Collection[i].timer);
		Timer.Collection[i] = null;
	}

	function clearTimeout(i)
	{
		if (!Timer.Collection[i])
		{
			return;
		}
		window.clearTimeout(Timer.Collection[i].timer);
		Timer.Collection[i] = null;
	}

	this.setInterval = setInterval;
	this.setTimeout = setTimeout;
	this.clearInterval = clearInterval;
	this.clearTimeout = clearTimeout;
	
	return this;
}

Timer.Collection = [];

Timer.callOnce = function(i)
{
    if (!Timer.Collection[i])
    {
		return;
	}
    eval(Timer.Collection[i].callString);
    Timer.Collection[i] = null;
}