// JavaScript Document
var transX = 5, isMoving = false, maxPoints = 1000;
function XTransformation( obj , to , velocity , accell , deccell , maxV , frameT , dir ){
	this.obj = obj;
	this.to = to;
	this.v = velocity;
	this.accell = accell;
	this.deccell = deccell;
	this.maxV = maxV;
	this.move = plot;
	this.play = playTrans;
	this.frameT = frameT;
	this.dir = dir;
	this.points = ( dir != "y" )? [obj.offsetLeft] : [obj.offsetTop];
	this.current = 0;
	isMoving = true;
	this.move();
}
function transMove(){
	var dX = this.to - this.obj.offsetLeft , speed = Math.abs( this.v ) , bearing = ( this.v / speed ) , aim = ( dX / Math.abs( dX ) ), D = Math.abs( dX ), limit;
	if( this.v * this.v >= 2 * this.deccell * D ){
		this.v -= this.deccell * bearing;
		if( this.v > 0 && this.v < 1 ){
			this.v = 1;
		}
		else if( this.v < 0 && this.v > -1 ){
			this.v = -1;
		}
	} else {
		//don't let it pick up too much speed
		this.v = Math.min( this.v + aim * this.accell , this.maxV );	
		//alert( this.v * this.v + " >= " + 2 * this.deccell * Math.abs( dX ) )
		limit = 2 * this.deccell * ( D - speed );
		if( this.v * this.v >= limit ){ 
			this.v = aim * Math.sqrt( limit );
			//alert(this.deccell * 2 * dX);
		}
	}
	if( ( speed > D &&  speed < Math.abs( this.accell * 2 ) ) || 
			( speed < D && Math.abs( this.accell ) > D ) ){
		this.obj.style.left = this.to + "px";
		//alert( this.obj.offsetLeft + " = " + this.to );
		this.onEnd( this.obj );
	} else {		
		this.obj.style.left = -1 * ( dX - this.to ) + this.v + "px";
		window.setTimeout( "transX.move()", this.frameT );
	}
}
function transMoveY(){
	var dY = this.to - this.obj.offsetTop , speed = Math.abs( this.v ) , bearing = ( this.v / speed ) , aim = ( dY / Math.abs( dY ) ), D = Math.abs( dY ), limit;
	if( this.v * this.v >= 2 * this.deccell * D ){
		this.v -= this.deccell * bearing;
		if( this.v > 0 && this.v < 1 ){
			this.v = 1;
		}
		else if( this.v < 0 && this.v > -1 ){
			this.v = -1;
		}
	} else {
		//don't let it pick up too much speed
		this.v = Math.min( this.v + aim * this.accell , this.maxV );	
		//alert( this.v * this.v + " >= " + 2 * this.deccell * Math.abs( dX ) )
		limit = 2 * this.deccell * ( D - speed );
		if( this.v * this.v >= limit ){ 
			this.v = aim * Math.sqrt( limit );
			//alert(this.deccell * 2 * dY);
		}
	}
	if( ( speed > D &&  speed < Math.abs( this.accell * 2 ) ) || 
			( speed < D && Math.abs( this.accell ) > D ) ){
		this.obj.style.top = this.to + "px";
		//alert( this.obj.offsetTop + " = " + this.to );
		this.onEnd( this.obj );
	} else {		
		this.obj.style.top = -1 * ( dY - this.to ) + this.v + "px";
		window.setTimeout( "transX.move()", this.frameT );
	}
}
function plot(){
	this.current = this.points.length;
	while( 1==1 ){
		var X = this.points[ this.points.length - 1 ], dX = this.to - X , speed = Math.abs( this.v ) , bearing = ( this.v / speed ) , D = Math.abs( dX ), aim = ( dX / D ), limit;
		if( this.v * this.v >= 2 * this.deccell * D ){
			this.v -= this.deccell * bearing;
			if( D > .5 * this.v * this.v / this.deccell ){
				limit = 2 * this.deccell * ( D - speed );
				this.v = aim * Math.sqrt( Math.abs( limit ) );
			}
		} else {
			//don't let it pick up too much speed
			this.v = Math.min( this.v + aim * this.accell , this.maxV );	
			//alert( this.v * this.v + " >= " + 2 * this.deccell * Math.abs( dX ) )
			limit = 2 * this.deccell * ( D - speed );
			if( this.v * this.v >= limit ){
				this.v = aim * Math.sqrt( Math.abs( limit ) );
				//alert(this.deccell * 2 * dX);
			}
		}
		//if( ( speed > D &&  speed < Math.abs( this.accell * 2 ) ) || 
			//	( speed < D && Math.abs( this.accell ) > D ) ){
		if( Math.abs( this.v ) < this.deccell || this.points.length >= maxPoints){
			//this.points.push( X + this.v );
			var last = this.points.pop();//this.points[ this.points.length - 1 ];
			/*while( Math.abs( last - this.to ) > 1 ){
				last = ( 4 * last + this.to ) / 5;
				this.points.push( last );
			}*/
			this.points.push( this.to );
			for( var i = 0; i < this.points.length; i++ ){
				this.points[ i ] = Math.ceil( this.points[ i ] ) + "px";
			}
			this.current = 0;
			this.play();
			break;
			//alert( this.obj.offsetLeft + " = " + this.to );
			this.onEnd( this.obj );
		} else {		
			//alert( -1 * ( dX - this.to ) + this.v );
			this.points.push( X + this.v );
			if( this.points.length > 2 * Math.abs( this.to - this.obj.offsetLeft ) && this.dir != "y" ){
				this.obj.style.left = this.to + "px";
				return;
			}
			//window.setTimeout( "transX.move()", this.frameT );
		}
	}
}
function playTrans(){
	if( this.current < this.points.length ){
		if( this.dir != "y" ){ this.obj.style.left = this.points[ this.current ];}
		else{ this.obj.style.top = this.points[ this.current ]; }
		var self = this;
		this.int = setTimeout( function(){ self.play(); } , 15 );
		this.current++;
	} else { 
		isMoving = false;
		if( this.onEnd != null ){ this.onEnd( this.obj ); }
	}
}
function last( L ){ return L[ L.length - 1 ]; }