/*
 * 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: "scale"
 *
 * This animation will scale the domElement using CSS.
 * The specification of the animation may contain:
 *
 *		destHeight		The destination height
 *		destWidth		The destination width
 *
 *		srcHeight		The source height
 *		srcWidth		The source width
 *
 *		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_scaleStartAnimation(specification, animationState)
{
	function __init_property(name, cssName, anchor)
	{
		if (specification[name] != null) {
			animationState[name] = specification[name];
		}
		 else {
		 	animationState[name] = ak_sizeToNumber(animationState.domElement.style[cssName]);
		}
		
		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("srcWidth", "width", animationState.anchorX);
	__init_property("srcHeight", "height", animationState.anchorY);

	__init_property("destWidth", "width", animationState.anchorX);
	__init_property("destHeight", "height", animationState.anchorY);

	animationState.currentX = ak_sizeToNumber(animationState.domElement.style.left);
	animationState.currentY = ak_sizeToNumber(animationState.domElement.style.top);
	
	animationState.currentHeight = animationState.srcHeight;
	animationState.currentWidth = animationState.srcWidth;

	if (animationState.currentX != null)	
		ak_setAnchoredCoordinate(animationState.domElement, "x", animationState.anchorX, animationState.currentX);
		
	if (animationState.currentY != null)
		ak_setAnchoredCoordinate(animationState.domElement, "y", animationState.anchorY, animationState.currentY);
		
	animationState.domElement.style.height = animationState.currentHeight + "px";
	animationState.domElement.style.width = animationState.currentWidth + "px";	
}

// Animation stop handler
function ak_scaleStopAnimation(animationState, animationAborted, isReplaced)
{
	if (isReplaced == false) {
		if (animationState.currentX != null)
			ak_setAnchoredCoordinate(animationState.domElement, "x", animationState.anchorX, animationState.currentX);
		
		if (animationState.currentY != null)
			ak_setAnchoredCoordinate(animationState.domElement, "y", animationState.anchorY, animationState.currentY);
			
		animationState.domElement.style.height = animationState.currentHeight + "px";
		animationState.domElement.style.width = animationState.currentWidth + "px";	
	}		
}

// Animation step
function ak_scaleUpdateAnimation(animationState)
{
	var posWidth = animationState.progress[0];
	var posHeight = animationState.progress[1];
	
	var destWidth = animationState.destWidth;
	var srcWidth = animationState.srcWidth;
	var scaleWidth = destWidth - srcWidth;

	var destHeight = animationState.destHeight;
	var srcHeight = animationState.srcHeight;
	var scaleHeight = destHeight - srcHeight;

	animationState.currentHeight = srcHeight + (scaleHeight * posHeight);
	animationState.currentWidth = srcWidth + (scaleWidth * posWidth);

	animationState.domElement.style.height = animationState.currentHeight + "px";
	animationState.domElement.style.width = animationState.currentWidth + "px";	

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

// Register animation
ak_registerBehaviour("scale", Config.AnimationKit.FPS, 2, ak_scaleStartAnimation, ak_scaleStopAnimation, ak_scaleUpdateAnimation);


