/*
 *               ____
 *              /\   \
 *             ___\   \___
 *            /\          \
 *            \ \___    ___\
 *        ____ \/__/\   \__/
 *       /\   \    \ \___\    klof  |  innovative web technology
 *      ___\   \___ \/___/
 *     /\          \          keen/animation.js
 *     \ \___    ___\         HTML Element animation
 *      \/__/\   \__/
 *          \ \___\           Copyright 2003-2007, klof
 *           \/___/           http://keen.klof.net/
 *
 *                            requirements: keen/dynamic.js
 *                                          keen/animation.js
 */


/* Create the master Keen object if it doesn't exist */
if ( typeof Keen != "object" )
	Keen = new ( function(){ this._child = 0 } )();



Keen.animation = function( mElement )
{
	this._version  = "1.0.0";
	this._posstep  = 20;
	this._dimstep  = 20;
	this._fadestep = 10;
	this._callAfterSlide = false;
	this._callAfterGrow  = false;
	this.init( mElement );
};
Keen.animation.prototype = new Keen.dynamic;
Keen.animation.prototype.slide = function( nX, nY, mMode, nStep, oCallWhenDone )
{
	if ( typeof mMode != "function" && typeof this[ mMode ] != "function" )
		mMode = "linear";
	if ( typeof nStep == "number" )
		this._posstep = nStep;
		
	this._posbeginX = this._x;
	this._posbeginY = this._y;
	this._posendX   = nX;
	this._posendY   = nY;
	this._postime   = 0;
	this._posmode   = typeof mMode == "function" ? mMode : this[ mMode ];
	this._callAfterSlide = ( typeof oCallWhenDone == "function" ? oCallWhenDone : false );

	clearTimeout( this.__position );
	this.__position = this.timedcall( "slidestep();", 1000 / this._fps );
};
Keen.animation.prototype.slidestep = function()
{
	if ( ++this._postime < this._posstep )
	{
		this.move(
			this._posmode( this._posbeginX, this._posendX, this._postime, this._posstep ),
			this._posmode( this._posbeginY, this._posendY, this._postime, this._posstep )
		);
		this.__position = this.timedcall( "slidestep();", 1000 / this._fps );
	}
	else
	{
		this.move( this._posendX, this._posendY );
		clearTimeout( this.__position );
		if ( this._callAfterSlide )
			this._callAfterSlide();
	}
};
Keen.animation.prototype.grow = function( nW, nH, mMode, nStep, oCallWhenDone )
{
	if ( typeof mMode != "function" && typeof this[ mMode ] != "function" )
		mMode = "linear";
	if ( typeof nStep == "number" )
		this._dimstep = nStep;
	this._dimbeginW  = this._width;
	this._dimbeginH  = this._height;
	this._dimendW    = nW;
	this._dimendH    = nH;
	this._dimtime    = 0;
	this._dimmode    = typeof mMode == "function" ? mMode : this[ mMode ];
	this._callAfterGrow = ( typeof oCallWhenDone == "function" ? oCallWhenDone : false );


	clearTimeout( this.__dimension );
	this.__dimension = this.timedcall( "growstep();", 1000 / this._fps );
};
Keen.animation.prototype.growstep = function()
{
	if ( ++this._dimtime < this._dimstep )
	{
		this.resize(
			this._dimmode( this._dimbeginW, this._dimendW, this._dimtime, this._dimstep ),
			this._dimmode( this._dimbeginH, this._dimendH, this._dimtime, this._dimstep )
		);
		this.__dimension = this.timedcall( "growstep();", 1000 / this._fps );
	}
	else
	{
		this.resize( this._dimendW, this._dimendH );
		clearTimeout( this.__dimension );
		if ( this._callAfterGrow )
			this._callAfterGrow();
	}
};
Keen.animation.prototype.fade = function( nAlpha, mMode, nStep, oCallWhenDone )
{
	if ( typeof mMode != "function" && typeof this[ mMode ] != "function" )
		mMode = "linear";
	if ( typeof nStep == "number" )
		this._fadestep = nStep;
	this._fadebeginAlpha = this.getAlpha();
	this._fadeendAlpha   = nAlpha;
	this._fadetime       = 0;
	this._fademode       = typeof mMode == "function" ? mMode : this[ mMode ];
	this._callAfterFade  = ( typeof oCallWhenDone == "function" ? oCallWhenDone : false );
	
	clearTimeout( this.__fadetimer );
	this.__fadetimer = this.timedcall( "fadestep();", 1000 / this._fps );
};
Keen.animation.prototype.fadestep = function()
{
	if ( ++this._fadetime < this._fadestep )
	{
		this.setAlpha( this._fademode( this._fadebeginAlpha, this._fadeendAlpha, this._fadetime, this._fadestep ) );
		this.__fadetimer = this.timedcall( "fadestep();", 1000 / this._fps );
	}
	else
	{
		this.setAlpha( this._fadeendAlpha );
		clearTimeout( this.__fadetimer );
		if ( this._callAfterFade )
			this._callAfterFade();
	}
};
Keen.animation.prototype.linear = function( nBegin, nEnd, nStep, nNumStep )
{
	return ( nEnd - nBegin ) * nStep / nNumStep + nBegin;
};


