var dxy = 5;
var ti = 100;

function Ship_rmove($r, $a)
{
	this.r = this.r + $r;
	this.alpha = this.alpha + $a;
	this.r2d();
	this.redraw();
}

function Ship_r2d()
{
	this.x = 250 + this.r * Math.cos(this.alpha);
	this.y = 250 + this.r * Math.sin(this.alpha);
}

function Ship_d2r()
{
	$dx = this.x-250;
	$dy = this.y-250;
	this.r = Math.sqrt($dx*$dx+$dy*$dy);
	this.alpha = Math.atan2($dy,$dx);
}

function Ship_move($dx, $dy)
{
	this.x = this.x + $dx*dxy;
	this.y = this.y + $dy*dxy;
	this.d2r();
	this.redraw();
}

function Ship_redraw()
{
	document.getElementById(this.name).style.left=this.x;
	document.getElementById(this.name).style.top=this.y;
//	eval('document.all.'+this.name+'.style.top='+this.y);
	document.all.w_x.value=this.x;
	document.all.w_y.value=this.y;
	document.all.w_r.value=this.r;
	document.all.w_a.value=this.alpha;
	document.all.w_vx.value=this.vx;
	document.all.w_vy.value=this.vy;
}

function Ship_draw()
{
	$str = '<div id="'+this.name+'" style="position:absolute;top:'+this.y+';left:'+this.x+';visibility:visible;text-align:center;font-size:10px;font-family:Verdana,Arial,sans-serif;"><img src="ship1.gif" border="0"><br>'+this.description+'</div>';
	document.writeln($str);
}

function Ship_target($tx, $ty)
{
	this.tx = $tx;
	this.ty = $ty;
	$x = $tx - this.x;	
	$y = $ty - this.y;
	$r = Math.sqrt($x*$x+$y*$y);
	
	//alert($x+':'+$y+':'+$r);
	
	this.vx = $x/$r;
	this.vy = $y/$r;
}

function Ship_operate()
{
	if (this.moving)
	{
		if (Math.abs(this.tx-this.x)<dxy && Math.abs(this.ty-this.y)<dxy)
		{
			this.x=this.tx;
			this.y=this.ty;
			this.moving=false;
		}
		else
				this.move(this.vx, this.vy);
		
		this.d2r();
		this.redraw();
	}
}

function Ship($name, $description, $x, $y)
{
	$dx = $x-250;
	$dy = $y-250;
	
	// parameters:
	this.name = $name;
	this.description = $description;
	this.moving = false;
	this.x = $x;
	this.y = $y;
	
	// methods:
	this.move = Ship_move;
	this.redraw = Ship_redraw;
	this.draw = Ship_draw;
	this.d2r = Ship_d2r;
	this.r2d = Ship_r2d;
	this.rmove = Ship_rmove;
	this.target = Ship_target;
	this.operate = Ship_operate;
	
	// init:
	this.d2r();
}
