/**
 * Copyright (c) 2008 Andreas Timm | symmetrics gmbh (http://www.symmetrics.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $CreateDate: 2008-07-30 16:22:00 +0001 (Wen, 30 Jul 2008) $
 *
 * Version: 0.1
 * 
 * @author Andreas Timm <at@symmetrics.de>
 *
 * Requires: dAux
 **/
var mScrollInertia = function()
{
	
	this.vars_default =
	{
		init: null,
		speed1: 0,
		speed2: 0,
		shift_delta: 50
	} ;
	
	 
	this.vars = new Object ;

	this.temp_vars = "" ;
	
	
	/**
	 * Inertia stop
	 *
	 * @param	String		id:
	 **/
	this.stop = function ( id )
	{
		if ( this.vars[id] === undefined )
			return ;
		this.speed_calc1 ({ id: id,	action: "stop" }) ;
		if ( this.vars[id].init !== null )
		{
			clearTimeout ( this.vars[id].init ) ;
			this.vars[id].init = null ;
		}
		this.vars[id].sc_param1_temp = 0 ;
//		if ( this.vars[id].move_stop !== undefined )
//			this.vars[id].move_stop() ;
	} ;
	
	
	/**
	 * scaler inertia
	 * 
	 *
	 * @param	Object		v:
	 * 				String		id:
	 * 				Nuumber		param1:
	 * 				Nuumber		param2:
	 * 				Function	move:
	 * 				Function	move_stop:
	 **/
	this.init = function ( v )
	{
		var time_temp ;

		if ( this.vars[v.id] === undefined )
			this.vars[v.id] = dAux.clone_obj ( this.vars_default ) ;

		this.vars[v.id].move_stop = v.move_stop ;
		this.vars[v.id].init = setTimeout ( function () { mScrollInertia.move( v.id, v.move) }, 50 ) ;

		this.vars[v.id].param1 = v.param1 + this.vars[v.id].speed1 * 100 ;
		if ( v.param2 !== undefined )
			this.vars[v.id].param2 = v.param2 + this.vars[v.id].speed2 * 100 ;

		this.vars[v.id].time = (new Date()).getTime();

		v.move ( this.vars[v.id] ) ;
	} ;
	

	/**
	 * move
	 *
	 * @param	String		id:
	 * 			Function	move_stop:
	 **/
	this.move = function ( id, move )
	{
		var time_temp = (new Date()).getTime();

		mScrollInertia.vars[id].param1 +=
			mScrollInertia.vars[id].speed1
			* ( time_temp - mScrollInertia.vars[id].time ) ;
			
		if ( mScrollInertia.vars[id].param2 !== undefined )
			mScrollInertia.vars[id].param2 +=
				mScrollInertia.vars[id].speed2
				* ( time_temp - mScrollInertia.vars[id].time ) ;
			
		mScrollInertia.vars[id].time = time_temp ; 
	
		if
		(
			mScrollInertia.vars[id].speed1 > -0.01 &&
			mScrollInertia.vars[id].speed1 < 0.01 &&
			(
				mScrollInertia.vars[id].param2 === undefined ||
				(
					mScrollInertia.vars[id].speed2 > -0.01 &&
					mScrollInertia.vars[id].speed2 < 0.01
				)
			)
		)
		{
			mScrollInertia.stop(id) ;
			if ( mScrollInertia.vars[id].move_stop !== undefined )
				mScrollInertia.vars[id].move_stop() ;
		}
		else
		{
			move ( mScrollInertia.vars[id] ) ;
	
			mScrollInertia.vars[id].speed1 = mScrollInertia.vars[id].speed1 / 1.30 ;
			
			if (  mScrollInertia.vars[id].param2 !== undefined )
				mScrollInertia.vars[id].speed2 =
					mScrollInertia.vars[id].speed2 / 1.30 ;

			mScrollInertia.vars[id].init =
				setTimeout ( function(){ mScrollInertia.move(id,move)}, 50 ) ;
		}
	} ;
	
	
	
	/**
	 * speed calc 1
	 *
	 * @param	Object		v:
	 * 				String		action: start|stop (default=start)
	 * 				String		id:
	 * 				Function	get_values: (return Array of params)
	 **/
	this.speed_calc1 = function ( v )
	{
		if ( v.action == "stop" )
		{
			clearInterval ( this.vars[v.id].sc_init ) ;
			this.vars[v.id].sc_init = null ;			
		}
		else
		{
			if ( this.vars[v.id] === undefined )
				this.vars[v.id] = dAux.clone_obj ( this.vars_default ) ;
			
			clearTimeout ( this.vars[v.id].init ) ;
			clearInterval ( this.vars[v.id].sc_init ) ;
			
			this.vars[v.id].speed1 = 0 ;
			
			var values = v.get_values () ;
			
			this.vars[v.id].sc_param1 = values[0] ;
			if ( values[1] !== undefined  )
				this.vars[v.id].sc_param2 = values[1] ;
			
			this.vars[v.id].sc_time1 =
				this.vars[v.id].sc_time2 = (new Date()).getTime() ;
				
			var time_temp ;
			
			this.vars[v.id].sc_init = setInterval(function(){
				time_temp = (new Date()).getTime() ;
				mScrollInertia.vars[v.id].sc_time1 =
					time_temp - mScrollInertia.vars[v.id].sc_time1 ;
				
				if ( mScrollInertia.vars[v.id].sc_time1 == 0 )
					return ;
				 
				values = v.get_values () ;
				
				mScrollInertia.vars[v.id].speed1 =
					( values[0] - mScrollInertia.vars[v.id].sc_param1 ) 
					/ mScrollInertia.vars[v.id].sc_time1 ;
				mScrollInertia.vars[v.id].sc_param1 = values[0] ; 
					
				if ( values[1] !== undefined )
				{
					mScrollInertia.vars[v.id].speed2 =
						( values[1] - mScrollInertia.vars[v.id].sc_param2 ) 
						/ mScrollInertia.vars[v.id].sc_time1 ;
					mScrollInertia.vars[v.id].sc_param2 = values[1] ; 
				}
						
				mScrollInertia.vars[v.id].sc_time1 = time_temp ;
				
//				dHelp.out ( 1,
//					'1', mScrollInertia.vars[v.id].speed1,
//				1 );

				
			}, 100 ) ; 
		}
		
	} ;
	
	
	
	/**
	 * speed clac 2  
	 *
	 * @param	Object		v:
	 * 				String		id:
	 * 				Number		param1:
	 * 				Number		param2:
	 **/
	this.speed_calc2 = function ( v )
	{
		var param_temp = this.vars[v.id].sc_param1_temp ;
		var shift_delta = this.vars[v.id].shift_delta ;
		
		if 
		(
			v.param1 < param_temp - shift_delta ||
			v.param1 > param_temp + shift_delta
		)
		{
			var time_temp = (new Date()).getTime() ;
			this.vars[v.id].speed1 = 
				( v.param1 - param_temp ) /
				( time_temp - this.vars[v.id].sc_time2 );
			this.vars[v.id].sc_param1_temp = v.param1 ;
			this.vars[v.id].sc_time2 = time_temp ;
		}
		
		if ( v.param2 !== undefined )
		{
			param_temp = this.vars[v.id].sc_param2_temp ;
			if 
			(
				v.param2 < param_temp - shift_delta ||
				v.param2 > param_temp + shift_delta
			)
			{
				var time_temp = (new Date()).getTime() ;
				this.vars[v.id].speed2 = 
					( v.param2 - param_temp ) /
					( time_temp - this.vars[v.id].sc_time2 );
				this.vars[v.id].sc_param2_temp = v.param1 ;
				this.vars[v.id].sc_time2 = time_temp ;
			}
		}

//		dHelp.out ( 1,
//			'2:', mScrollInertia.vars[v.id].speed1,
//		1 );

	} ;
	
	
};

mScrollInertia = new mScrollInertia ;