function v(X,Y){this.X=X;this.Y=Y;}
// Chopper Object Definition
function chopperObj(){this.X=Xpos;this.Y=Ypos;this.dx=0;this.dy=0;this.obj=$('#chopperImg');this.set=function(){this.obj.css('top',Math.round(this.Y)).css('left',Math.round(this.X));}}
window.viewport={height:function(){return $(window).height();},width:function(){return $(window).width();}}
function takeOff(){
$('body').mousemove(function(e){set_target(e.clientX+$(window).scrollLeft(),e.clientY+$(window).scrollTop(),viewport.height(),viewport.width());})
chopper=new chopperObj();
chopper.obj.css('display','block');
chopper.set();
running=setInterval("animate()",20);
$('#tcLink').html(cursorToggleMsgs[0]);
}

function springForce(spring){
	var dx=(Xpos-chopper.X);
	var dy=(Ypos-chopper.Y);
	var len=Math.sqrt(dx*dx+dy*dy);
	if(len>fsLen){
		var springF=(len-fsLen);
		spring.X+=(dx/len)*springF;
		spring.Y+=(dy/len)*springF;
	}
}

function animate() {	
	var spring = new v(0, 0);
	springForce(spring);
	// air resisitance/friction
	var resist = new v(-chopper.dx * DAMPER, -chopper.dy * DAMPER);
	// compute new accel, including gravity
	var accel = new v((spring.X + resist.X),(spring.Y + resist.Y));
	// compute new velocity
	chopper.dx += (accel.X/100);
	chopper.dy += (accel.Y/100);

	// checking to see if image swap is needed
	var newImgNo = Math.round(accel.X/img_swap_accel);
	if (Math.abs(newImgNo) > 2){newImgNo = 2*(newImgNo/Math.abs(newImgNo))};
	newImgNo += 2;
	if (imgNo != newImgNo){
		imgNo = newImgNo;
		chopper.obj[0].src = img[imgNo].src;
	}
	// stop dead so it doesn't jitter when nearly still
	if (Math.abs(chopper.dx) < 0.1 && Math.abs(chopper.dy) < 0.1 && Math.abs(accel.X) < 0.1 && Math.abs(accel.Y) < 0.1) {
	    chopper.dx = 0;
	    chopper.dy = 0;
	}
	// set new position
	chopper.X += chopper.dx;
	chopper.Y += chopper.dy;
	chopper.set();
}

function set_target(inpX, inpY, inpSrnWidth, inpSrnHeight){
	// This will keep the chopper away from the edges to stop
	// the annoying scrollbars from appearing
	x1 = inpX - (inpSrnWidth / 2)
	y1 = -(inpY - (inpSrnHeight / 2))
	h1 = Math.pow(Math.pow(x1,2) + Math.pow(y1,2), 0.5)
	h2 = img_offset_radius;
	if ((x1*y1) != 0){
		x2 = Math.pow(((h2*h2)/(((y1*y1)/(x1*x1))+1)),0.5)
		y2 = Math.pow((h2*h2)-(x2*x2),0.5)
		if (x1>0){x2=-x2};
		if (y1>0){y2=-y2};
		Xpos = (inpX + x2)  - (img_width /2);
		Ypos = (inpY - y2) - (img_height/2);
	}
}

// if cookie is not set then start the chopper
function startChopper(){if(getCookie('MD500_cursor')!='hide'){takeOff();}}

function setChopperCookie(v){
	today = new Date();
	today.setTime(today.getTime());
	expires_date = new Date(today.getTime()+(365*1000*60*60*24));
	setCookie('MD500_cursor',v,expires_date.toUTCString(),'/');
}

function toggleChopper(){
	if(getCookie('MD500_cursor')=='hide'){
		setChopperCookie('show');
		startChopper();
	}else{
		setChopperCookie('hide');
		clearInterval(running);
		chopper.obj.css('display','none');
		$('#tcLink').html(cursorToggleMsgs[1]);
	}
}

//- END FUNCTION SECTION
// - START VAR DEF SECTION

followChopperLoaded = true; //used to turn chopper back on
var landing; //used to animate landing sequence.
var lPad = new v(-300, -300); //landing pad position

var chopper;
var running;
	
var imgNo = 2; //current chopper image (changes base on acceleration) [2 is the default]
var img = new Array();
for (var i=0;i<=4;i++){
	img[i] = new Image();
	img[i].src = "/lib/g/md500/" + i + ".gif";
}
var img_swap_accel = 30; // automatically changes image based on current acceleration
var img_offset_radius = 140;  // this sets the radial distance from the mouse cursor to the center of the chopper image
var img_width = 170; // width of the chopper images
var img_height = 115; //height of the chopper images

var Xpos = lPad.X;
var Ypos = lPad.Y;
var fsLen = 10; // free spring length
var DAMPER = 10; //
cursorToggleMsgs = new Array("<b>HIDE</b> Helicopter", "<b>SHOW</b> Helicopter")

// - END VAR DEF SECTION

// end hiding -->
