var queue = new Array();

/*
 * Unscheduled Simple Animation
 * 
 * elementId - the id of the element being animated
 * start - the starting location
 * end - the ending location
 * change - the amount to increment each translation
 * direction - 'L' for left, 'R' for Right, U for up, and D for down
 * time - the time between one translation
 */
function simpleAnimation(elementId, start, end, change, direction, time ) {
	if( direction == 'L' || direction == 'R' ) {
		document.getElementById(elementId).style.left = start + 'px';
	} else {
		document.getElementById(elementId).style.top = start + 'px';
	}

	if( ((direction == 'R' || direction == 'D') && parseInt(start) < parseInt(end)) ||
			((direction == 'L' || direction == 'U') && parseInt(start) > parseInt(end)) ) {
		start = parseInt(start) + parseInt(change);
		setTimeout( "animate('" + elementId + "','"
								+ start + "','"
								+ end + "','"
								+ change + "','"
								+ direction + "','"
								+ time + "')", time );
	} else {
		if( direction == 'L' || direction == 'R' ) {
			document.getElementById(elementId).style.left = end + 'px';
		} else {
			document.getElementById(elementId).style.top = end + 'px';
		}
	}
}

/*
 * Scheduled Animation
 */
function runSchedule() {
	if( queue.length != 0 ) {
		var func = queue.pop();
		scheduledAnimation( func[0], func[1], func[2], func[3], func[4], func[5], func[6], func[7] );
	}
}
/*
 * elementId - the id of the element being animated
 * start - the starting location
 * end - the ending location
 * change - the amount to increment each translation
 * direction - 'L' for left, 'R' for Right, U for up, and D for down
 * effects - effects after translation is complete
 * slowDown - 'true' if object should slow down when coming to a stop, else 'false' or ''
 * time - the time between one translation
 */
function scheduledAnimation(elementId, start, end, change, direction, effects, slowDown, time ) {
	if( direction == 'L' || direction == 'R' ) {
		document.getElementById(elementId).style.left = start + 'px';
	} else {
		document.getElementById(elementId).style.top = start + 'px';
	}

	if( ((direction == 'R' || direction == 'D') && parseInt(start) < parseInt(end)) ||
			((direction == 'L' || direction == 'U') && parseInt(start) > parseInt(end)) ) {
		start = parseInt(start) + parseInt(change);
		var run = parseInt(time);
		
		if( slowDown != "false" && slowDown != "" ) {
			if( Math.abs(parseInt(start) - parseInt(end)) <= 20 ) {
				var dChange = Math.abs( Math.abs(parseInt(start) - parseInt(end)) / parseInt(change) );
				var dTime = dChange / parseInt(time);
				
				if( dTime >= 1 ) run += 15;
				else if( dTime >= 0.5 ) run += 20;
				else run += 30;
			}
		}
		
		setTimeout( "scheduledAnimation('" 
								+ elementId + "','"
								+ start + "','"
								+ end + "','"
								+ change + "','"
								+ direction + "','"
								+ effects + "','"
								+ slowDown + "','"
								+ time + "')", run );
	} else {
		if( direction == 'L' || direction == 'R' ) {
			document.getElementById(elementId).style.left = end + 'px';
		} else {
			document.getElementById(elementId).style.top = end + 'px';
		}
		
		if( effects != "none" && effects != "" ) {
			eval(effects);
		}
		
		runSchedule();
	}
}

/*
 * Effects
 * 
 * elementId - the id of the element being bounced
 * position - the starting position
 * direction - 'H' for horizontal and 'V' for vertical
 * bounces - the number of bounces
 */
function bounce(elementId, position, direction, bounces) {
	var b = parseInt(bounces);
	var p = parseInt(position);
	if( b > 0 ) {
		--b;
		
		if( direction == 'H' ) {
			var h = document.getElementById(elementId).style.left;
			h = h.substr( 0, h.indexOf("px") == -1 ? h.length : h.indexOf("px") );
			if( h == p ) h = p + 10;
			else if( h > p ) h = p - 20;
			else h = p + 10;
			document.getElementById(elementId).style.left = h;
		} else {
			var v = document.getElementById(elementId).style.top;
			v = v.substr( 0, v.indexOf("px") == -1 ? v.length : v.indexOf("px") );
			if( v == p ) v = p + 10;
			else if( v > p ) v = p - 20;
			else v = p + 10;
			document.getElementById(elementId).style.top = v;
		}
		
		setTimeout( "bounce('" + elementId + "','"
							+ position + "','"
							+ direction + "','"
							+ b + "')", 250 );
	} else {
		if( direction == 'H' ) {
			document.getElementById(elementId).style.left = position + 'px';
		} else {
			document.getElementById(elementId).style.top = position + 'px';
		}
	}
}
