/*
 *               ____
 *              /\   \
 *             ___\   \___
 *            /\          \
 *            \ \___    ___\
 *        ____ \/__/\   \__/
 *       /\   \    \ \___\    klof  |  innovative web technology
 *      ___\   \___ \/___/
 *     /\          \          klib3/animation.js
 *     \ \___    ___\         HTML Element animation
 *      \/__/\   \__/
 *          \ \___\           Copyright 2003-2006, klof
 *           \/___/           http://www.klof.net/k.lib3/
 *
 *                            requirements: klib3/dynamic.js
 *                                          klib3/animation.js
 */


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



klib3.animation = function( mElement )
{
	this._version = "1.0.0";
	this._posstep = 20;
	this._dimstep = 20;
	this._callAfterSlide = false;
	this._callAfterGrow  = false;
	this.init( mElement );
};
klib3.animation.prototype = new klib3.dynamic;
klib3.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 );
	this.__position = this.timedcall( "slidestep();", 1000 / this._fps );
};
klib3.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();
	}
};
klib3.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 );
	this.__dimension = this.timedcall( "growstep();", 1000 / this._fps );
};
klib3.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();
	}
};

klib3.animation.prototype.linear = function( nBegin, nEnd, nStep, nNumStep )
{
	return ( nEnd - nBegin ) * nStep / nNumStep + nBegin;
};

