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

/*
 * ak_setOpacity(domElement, opacity)
 *
 * Sets the opacity of an object
 *
 */
function ak_setOpacity(domElement, opacity)
{
	if (isInternetExplorer == true) {
		domElement.__ieSafeOpacity = opacity;
		domElement.style.filter = "alpha(opacity="+Math.ceil(opacity * 100)+")";
	}
	 else {
		domElement.style.opacity = opacity;
	}
}

/*
 * ak_getOpacity(domElement, opacity)
 *
 * Returns the opacity of an object
 *
 */
function ak_getOpacity(domElement)
{
	var opacity;

	if (isInternetExplorer == true) {
		opacity = domElement.__ieSafeOpacity;
	}
	 else {
		if (domElement.style.opacity == "") return null;
		
		opacity = Number(domElement.style.opacity);
	}
	
	return opacity;
}

/*
 * Animation: "opacity"
 *
 * This animation will change the opacity of the domElement using CSS.
 * The specification of the animation has to contain:
 *
 *		destOpacity			The destination opacity
 *
 * It may additionally contain:
 *
 *		srcOpacity			The opacity to start from 
 *		destDisplay			The CSS display destination style (none, block, ...)
 *		srcDisplay			The CSS display source style (none, block, ...)
 *		destVisibility		The CSS visibility destination style (hidden, visible,...)
 *		srcVisibility		The CSS visibility source style (hidden, visible,...)
 *		destZIndex			The Z-Index of the destination style
 *		srcZIndex			The Z-Index of the source style 
 *
 */
 
// Animation initialization function
function ak_opacityStartAnimation(specification, animationState)
{

	if (specification.srcOpacity != null) {
		animationState.srcOpacity = specification.srcOpacity;
	}
	 else {
	 	animationState.srcOpacity = ak_getOpacity(animationState.domElement);

	 	if (animationState.srcOpacity == null)
	 		animationState.srcOpacity = 1;
	}

	animationState.destOpacity = specification.destOpacity;
	animationState.currentOpacity = animationState.srcOpacity;
	
	animationState.destDisplay = specification.destDisplay;
	animationState.srcDisplay = specification.srcDisplay;
	
	animationState.destVisibility = specification.destVisibility;
	animationState.srcVisibility = specification.srcVisibility;

	animationState.srcZIndex = specification.srcZIndex;	
	animationState.destZIndex = specification.destZIndex;
	
	ak_setOpacity(animationState.domElement, animationState.currentOpacity);
	
	if (animationState.srcDisplay != null)
		animationState.domElement.style["display"] = animationState.srcDisplay;

	if (animationState.srcVisibility != null)
		animationState.domElement.style["visibility"] = animationState.srcVisibility;

	if (animationState.srcZIndex != null)
		animationState.domElement.style["zIndex"] = animationState.srcZIndex;
}

// Animation stop handler
function ak_opacityStopAnimation(animationState, animationAborted, isReplaced)
{
	if (isReplaced == false) {
		ak_setOpacity(animationState.domElement, animationState.destOpacity);

		if (animationState.destDisplay != null)
			animationState.domElement.style.display = animationState.destDisplay;		

	if (animationState.destVisibility != null)
		animationState.domElement.style["visibility"] = animationState.destVisibility;
	}

	if (animationState.destZIndex != null)
		animationState.domElement.style["zIndex"] = animationState.destZIndex;
}

// Animation step
function ak_opacityUpdateAnimation(animationState)
{
	var pos = animationState.progress;
	
	var dest = animationState.destOpacity;
	var src = animationState.srcOpacity;
	var scale = dest - src;

	animationState.currentOpacity = src + (scale * pos);

	ak_setOpacity(animationState.domElement, animationState.currentOpacity);
}

// Register animation
ak_registerBehaviour("opacity", Config.AnimationKit.FPS, 1, ak_opacityStartAnimation, ak_opacityStopAnimation, ak_opacityUpdateAnimation);


