/********************************************************************************

	slide

********************************************************************************/
Thing.prototype.slideTo = function( x, y, speed1, speed2, speed3, dt, curve ){
	if( x == null || y == null ) return;
	
	this.pEnd = new Point( x,y );
	this.dt = (dt) ? dt : 17;
	this.curve = (curve) ? curve : 0;
	
	var direction = this.pEnd.sub(this.position);
	this.totDist = direction.size();

	this.speed1 = ( speed1 != null ) ? speed1 : 10;
	
	if( speed2 != null ){
		if( speed3 != null ){ 
			//gave all three
			this.speed2 = speed2;
			this.speed3 = speed3;
		}
		else{
			//gave 1 & 2
			this.speed3 = speed2;
			this.speed2 = (this.speed1 + this.speed3)/2;
		}
	}
	else{
		// gave only 1
		if( this.speed1 == 0 ){
			this.moveTo( pEnd );
			this.onSlideEnd();
		}
		else{
			this.speed3 = this.speed2 = this.speed1;
		}
	}
	this.slideStep();
}
Thing.prototype.slideBy = function( dx, dy, speed1, speed2, speed3, dt, curve ){
	this.slideTo( this.position.x + dx, this.position.y + dy, speed1, speed2, speed3, dt, curve  )
}
Thing.prototype.slideStep = function(){

	var direction = this.pEnd.sub(this.position);
	var dist = direction.size();
	direction = direction.mult( 1/dist );
	
	// figure out speed
	var progress = 2 * ( this.totDist - dist ) / this.totDist;
	var perp = direction.perp();
	
	if( progress < 1 ){
		var speed = this.speed1 * (1 - progress) + this.speed2 * (progress);
		perp = perp.mult( (progress) * this.curve )
	}
	else if( progress < 2 ){
		progress = progress - 1
		var speed = this.speed2 * (1 - progress) + this.speed3 * (progress);
		perp = perp.mult( (1 - progress) * this.curve )		
	}
	if( progress > 2 || speed >= dist || dist < 3 ){
		this.moveTo( this.pEnd );
		return this.onSlideEnd();	
	}
	direction = direction.mult( speed );
	this.moveBy( direction.add(perp) );
	
	this.slideTimeout = setTimeout( "Thing.all." + this.name + ".slideStep()", this.dt )
}
Thing.prototype.killSlide = function( finish ){
	clearTimeout( this.slideTimeout );
	if( finish ) this.moveTo( this.pEnd )
}
Thing.prototype.onSlideEnd = function(){}
