/*
 * hyCMS
 * Copyright(C)2008 by Friedrich Gräter
 * Published under the terms of the Lesser GNU General Public License v2
 *
 * AnimationKit backend: Move behaviour
 *
 * Please Note: This module has to be clean from all dependencies to predicateJS!
 *				controller.js has to be included first.
 *
 */

/*
 * Animation: "move"
 *
 * This animation will change the 2d position of the domElement using CSS.
 * The specification of the animation may contain:
 *
 *		destY			The destination Y coordinate
 *		destX			The destination X coordinate
 *
 *		srcY			The source Y coordinate
 *		srcX			The source X coordinate
 *
 *		anchorY			The anchor used for the Y coordinate ("top", "middle", "bottom")
 *		anchorX			The anchor used for the X coordinate ("left", "middle", "right")
 *
 * If one element is not given, the current position of the element will be used.
 *
 */

// Animation initialization function
function ak_moveStartAnimation(specification, animationState)
{
	function __init_property(name, coordinate, anchor)
	{
		if (specification[name] != null) {
			animationState[name] = specification[name];
		}
		 else {
		 	animationState[name] = ak_getAnchoredCoordinate(animationState.domElement, coordinate, anchor);
		}
		
		if (animationState[name] == null)
			animationState[name] = 0;
	}

	animationState.anchorX = specification.anchorX;
	animationState.anchorY = specification.anchorY;

	if (animationState.anchorX == null)
		animationState.anchorX = "left";
	if (animationState.anchorY == null)
		animationState.anchorY = "top";

	__init_property("srcX", "x", animationState.anchorX);
	__init_property("srcY", "y", animationState.anchorY);

	__init_property("destX", "x", animationState.anchorX);
	__init_property("destY", "y", animationState.anchorY);

	animationState.currentX = animationState.srcX;
	animationState.currentY = animationState.srcY;
	
	ak_setAnchoredCoordinate(animationState.domElement, "x", animationState.anchorX, animationState.currentX);
	ak_setAnchoredCoordinate(animationState.domElement, "y", animationState.anchorY, animationState.currentY);
}

// Animation stop handler
function ak_moveStopAnimation(animationState, animationAborted, isReplaced)
{
	if (isReplaced == false) {
		ak_setAnchoredCoordinate(animationState.domElement, "x", animationState.anchorX, animationState.destX);
		ak_setAnchoredCoordinate(animationState.domElement, "y", animationState.anchorY, animationState.destY);
	}		
}

// Animation step
function ak_moveUpdateAnimation(animationState)
{
	var posX = animationState.progress[0];
	var posY = animationState.progress[1];
	
	var destX = animationState.destX;
	var srcX = animationState.srcX;
	var scaleX = destX - srcX;

	var destY = animationState.destY;
	var srcY = animationState.srcY;
	var scaleY = destY - srcY;

	animationState.currentX = srcX + (scaleX * posX);
	animationState.currentY = srcY + (scaleY * posY);

	ak_setAnchoredCoordinate(animationState.domElement, "x", animationState.anchorX, animationState.currentX);
	ak_setAnchoredCoordinate(animationState.domElement, "y", animationState.anchorY, animationState.currentY);
}

// Register animation
ak_registerBehaviour("move", Config.AnimationKit.FPS, 2, ak_moveStartAnimation, ak_moveStopAnimation, ak_moveUpdateAnimation);


