
// This is an array of AnimInfo objects that contain all the info for animating a particular
// object.
var arAnimatedObjects = null;

// Object that keeps our timer interval cookie
var intervalID = null;

// Handy wrappers
function MoveToX(object, posX)
{
	MoveTo(object, posX, object.offsetTop);
}

function MoveToY(object, posY)
{
	MoveTo(object, object.offsetLeft, posY);
}

function MoveTo(object, posX, posY)
{
	var animInfo = new AnimInfo(object, posX, posY);

	// Create the array if we need to
	if(arAnimatedObjects == null)
		arAnimatedObjects = new Array();

	// If this object is already being animated, update that animinfo with the new values
	var found = 0;
	for(var i = 0; i < arAnimatedObjects.length; ++i)
	{
		var a = arAnimatedObjects[i];
		if(a.obj == object)
		{
			a.update(animInfo);
			found = 1;
		}
	}

	// Add the new item to the end of the array, if needed.
	if(!found)
		arAnimatedObjects.push(animInfo);

	// If the timer isn't yet running, set the timer to actually move the objects
	if(intervalID == null)
		intervalID = window.setInterval(timerCallback, 5);
}

function FinishAnimating()
{
	if(intervalID != null)
	{
		window.clearInterval(intervalID);
		intervalID = null;
	}
}

function timerCallback()
{
	if(arAnimatedObjects == null)
		return;

	// This is our stop case
	if(arAnimatedObjects.length == 0)
		FinishAnimating();
	else
	{
		// Iterate over the animated objects array, moving each object by one increment.
		var i = 0;
		while(i < arAnimatedObjects.length)
		{
			var animInfo = arAnimatedObjects[i];
			if(animInfo.isComplete())
			{
				// Finish the animation on this one
				animInfo.setFinalPosition();

				// Remove from the array, and DON'T increment our counter
				arAnimatedObjects.splice(i, 1);
			}
			else
			{
				// Move the object
				animInfo.moveIncrement();
				++i;
			}
		}
	}
}

