// DHTML MD500 Lib

// START FUNCTION SECTION

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;
    if (bCategory == 1){ // IE 4+
		this.obj = eval("MD500" + ".style");
		this.imgObj = eval("document.all.chopperImg");
	}
	if (bCategory == 2){ // NN 4
		this.imgObj = eval("document.MD500.document.images[0]");
		this.obj = eval("document.MD500");
	}
	if (bCategory == 3){ // NN 6
		this.imgObj = document.getElementById("chopperImg");
		this.obj = document.getElementById("MD500").style;
	}
}

function MoveHandler(e){
    set_target(e.pageX, e.pageY, window.innerWidth + window.pageXOffset, window.innerHeight + window.pageYOffset);
    return true;
}

function MoveHandlerIE(){
    set_target(window.event.x + document.body.scrollLeft, window.event.y + document.body.scrollTop, document.body.clientWidth + document.body.scrollLeft, document.body.clientHeight + document.body.scrollTop);
}

function takeOff(){
	//setting event handler stuff while loading script
	if (bCategory == 1){
	   document.onmousemove = MoveHandlerIE;
	}
	if (bCategory == 2){
	    document.captureEvents(Event.MOUSEMOVE);
	    document.onMouseMove = MoveHandler;
	}
	if (bCategory == 3){
		window.addEventListener("mousemove", MoveHandler, false);
	}
	
	//defining object
	chopper = new chopperObj();
	//setting its position
	chopper.obj.left = chopper.X;
	chopper.obj.top = chopper.Y;
	running = setInterval("animate()", 20);
	
}

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.imgObj.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;
	// move img to new position
	chopper.obj.left = Math.round(chopper.X);
	//Keeping Away from Flash Header
	if (chopper.Y < 1){chopper.Y = 1};

	chopper.obj.top =  chopper.Y;
	
}

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);
	}
}

function autoPilotLanding(){
	// need to check to see if the chopper is at landing pad.
	if (Math.abs(chopper.X - lPad.X) < 10){
		// then kill the "running" interval (animate() interval)	
		if (running){
			clearInterval(running);
			clearInterval(landing);
		}
	}
}

//- 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";
	img[i].src = "/md500_dhtml_images/" + i + ".gif";
}
var img_swap_accel = 30; // automatically changes image based on current acceleration
var img_offset_radius = 50;  // this sets the radial distance from the mouse cursor to the center of the chopper image
var img_width = 200; // width of the chopper images
var img_height = 150; //height of the chopper images

var Xpos = 20;
var Ypos = 550;
var fsLen = 10; // free spring length
var DAMPER = 20; //

if (bCategory > 0){
	takeOff();
}

// - END VAR DEF SECTION

// end hiding -->