
// Helper function that translates a number into a pixel coordinate that styles like
function NumberToPxStyle(number)
{
	return number.toString() + "px";
}

// Sets the final position of an object.
function AnimSetFinalPosition()
{
	// Animation is done, set the final position of both objects
	if(this.dx != 0)
	{
		this.obj.style.left = NumberToPxStyle(this.targetLeft);
	}
	if(this.dy != 0)
	{
		this.obj.style.top = NumberToPxStyle(this.targetTop);
	}
}

// Moves the object in a single increment.
function AnimMoveIncrement()
{
	this.currentLeft += this.stepX;
	this.obj.style.left = NumberToPxStyle(this.currentLeft);

	this.currentTop += this.stepY;
	this.obj.style.top = NumberToPxStyle(this.currentTop);
	
	this.increments--;
}

// Tells us whether we've run out of increments.
function AnimIsComplete()
{
	return (this.increments <= 0);
}

// Updates an AnimInfo with new values
function AnimUpdate(animInfo)
{
	this.targetLeft = animInfo.targetLeft;
	this.targetTop = animInfo.targetTop;
	this.currentLeft = animInfo.currentLeft;
	this.currentTop = animInfo.currentTop;
	this.dx = animInfo.dx;
	this.dy = animInfo.dy;
	this.increments = animInfo.increments;
	this.stepX = animInfo.stepX;
	this.stepY = animInfo.stepY;
}

// This defines the AnimInfo object
function AnimInfo(object, posX, posY)
{
	this.obj = object;
	this.targetLeft = posX;
	this.targetTop = posY;

	// These record the initial positions of the object
	this.currentLeft = object.offsetLeft;
	this.currentTop = object.offsetTop;

	// These will hold the number of pixels that we need to move the
	// object in the x and y directions.
	var dx = Math.abs(this.targetLeft - this.currentLeft);
	var dy = Math.abs(this.targetTop - this.currentTop);
	this.dx = dx;
	this.dy = dy;

	// For smooth animation, we don't want to move any more than 7 pixels at a time.
	// Less is OK, just not more.
	var increments = Math.max(dx, dy) / 8;
	increments = Math.round(increments);
	this.increments = increments;

	// Modifiers, to handle direction.
	var xMod = (this.currentLeft <= this.targetLeft) ? 1 : -1;

	var yMod = (this.currentTop <= this.targetTop) ? 1 : -1;

	// Amounts to increment/decrement, correcting for rounding down
	if(increments > 0)
	{
		this.stepX = Math.floor(dx / increments) * xMod;  
		this.stepY = Math.floor(dy / increments) * yMod;
	}

	// Member functions
	this.isComplete = AnimIsComplete;
	this.moveIncrement = AnimMoveIncrement;
	this.setFinalPosition = AnimSetFinalPosition;
	this.update = AnimUpdate;
}