// js_AnimateMovement.js
var animateSpeed = 20; // the larger the number the slower (and chopier) the animation
var objectToAnimate = null; // object being moved
var fx = null; // final left
var fY = null; // final top
var cX = null; // current left
var cY = null; // current top
var dX = null; // delta left
var dY = null; // delta top
var stepX = null; // distance left per animation frame
var stepY = null; // distance top per aniamtion frame
var slope = null; // rise over run

function initAnimate (objectID, x, y) {

	objectToAnimate = document.getElementById(objectID);
	fX = x; // final left
	fY = y; // final top
	
	// Current position
	cX = objectToAnimate.offsetLeft;
	cY = objectToAnimate.offsetTop;

	// Calculate the total distance moved and the distance to move for each frame of the animation.
	dX = Math.abs(fX - cX);
	dY = Math.abs(fY - cY);
	if ((dX == 0) || (dY == 0)) slope = 0;
	else slope = dY/dX;
	
	if (dX>=dY) {
		if (cX<fX) stepX = animateSpeed;
		else if (cX>fX) stepX = - animateSpeed;
		if (cY<fY) stepY = animateSpeed * slope;
		else if (cY>fY) stepY = - animateSpeed * slope;
	}
	else  {//(dX<dY)
		if (cX<fX) stepX = animateSpeed / slope;
		else if (cX>fX) stepX = - animateSpeed / slope;
		if (cY<fY) stepY = animateSpeed;
		else if (cY>fY) stepY = - animateSpeed;
	}
	animateObject();
	
}
function animateObject () {
	if ((dX > 0) || (dY > 0)) {
		objectToAnimate.style.left = Math.round(cX) + 'px';
		objectToAnimate.style.top = Math.round(cY) + 'px';
		cX = cX + stepX;
		cY = cY + stepY;
		dX = dX - Math.abs(stepX);
		dY = dY - Math.abs(stepY);
		setTimeout('animateObject()',animateSpeed);
	}	
	else {
		objectToAnimate.style.left = fX + 'px';
		objectToAnimate.style.top = fY + 'px';
	}
	return;
}
