// JavaScript Document

// Sets animation speed.
var animateSpeed = 50;

// Stores Object ID
var object = null;

// Final X and Y coordinates
var fX = null; 
var fY = null;

// Current X and Y coordinates
var cX = null;
var cY = null;

// Keep track of the amount the object has moved to the left and from the top while being animated
var dX = null;
var dY = null;

// Keep track of how far the object should move per each execution
var stepX = null;
var stepY = null;

// Records the ratio of X to Y for the slant of the objects path from start point to end point
var slope = null;

var isopen = false;
function initAnimate(objectID,x,y,originX,originY) {
    isopen = !isopen;
	object = document.getElementById(objectID);
	// Set initial position
	object.style.left = Math.round(originX) + 'px';
	object.style.top = Math.round(originY) + 'px';
    
    if (isopen) {
	    document.getElementById('flyContain').style.display = 'block';
	}
	
	// final position
	fX = x;
	fY = y;
	// current position
	cX = object.offsetLeft;
	cY = object.offsetTop;
	
	dX = Math.abs(fX-cX);
	dY = Math.abs(fY-cY);
	
	if ((dX == 0) || (dY == 0)) {
		slope = 0;
	}else{
		slope = dY/dX;
	}
	// Determines how far the object has to move
	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 if (dX<dY) {
		if (cY<fY) stepY = animateSpeed;
		else if (cY>fY) stepY = - animateSpeed;
		
		if (cX<fX) stepX = animateSpeed/slope;
		else if (cX>fX) stepX = - animateSpeed/slope;
	}
	animateObject();
}

function animateObject() {
	if ((dX > 0) || (dY > 0)) {
		object.style.left = Math.round(cX) + 'px';
		object.style.top = Math.round(cY) + 'px';
		
		cX = cX + stepX;
		cY = cY + stepY;
		
		dX = dX - Math.abs(stepX);
		dY = dY - Math.abs(stepY);
		
		setTimeout('animateObject()',0);
	}else{
		object.style.left = fX + 'px';
		object.style.top = fY + 'px';
		if (!isopen) {
	        document.getElementById("flyContain").style.display = 'none';
		}
	}
	return;
}
