<!--
// Chad Miles May 2004

// map points
var origin_x;
var origin_y;
var yards_per_pixel;
var coord_new_origin_x;
var coord_new_origin_y;

// mouse coords
var mouse_x;
var mouse_y;

// Variable for div_drag_ball (the ball graphic) and the yardage box
var ball = null;
var yardagebox = null;

// For the mouse coords
var x = y = null;

// Boundary's of the map
var rBound = 250;
var lBound = 10;
var tBound = 78;
var bBound = 368;

// Create the graphics object
var g = new jsGraphics("div_drag_canvas");

// Capture the three mouse events
document.onmousedown = down;
document.onmouseup = up;
document.onmousemove = move;

// talks to the mappoints frame and grabs the right numbers
function getMapPoints() {
  try {
	// Hide all the stuff
	window.div_drag_ball.style.visibility = "hidden";
	window.div_drag_yardage_box.style.visibility = "hidden";
	window.div_drag_red_dot.style.visibility = "hidden";
  try {	
	// Obtain tee coordinates (relative to map position)
	origin_x = parseInt(window.parent.frames("bottom_mappoints").hid_points_teex.value);
	origin_y = parseInt(window.parent.frames("bottom_mappoints").hid_points_teey.value);
	
	// Obtain yards per pixel
	yards_per_pixel = window.parent.frames("bottom_mappoints").hid_points_yardsperpixel.value;
	
	// If we don't have the coords, then try it again.
	if (origin_x == 0 || origin_y == 0 || yards_per_pixel == 0)
		updateMapPoints();
  }
  catch(er) {} 
  }
  catch(er){}
}
// refreshes the page that gets the map points
function updateMapPoints() {
	window.parent.frames("bottom_mappoints").location.href = "frame_bottom_mappoints.asp?sku=" + window.parent.frames("header").sku + "&hole=" + window.parent.frames("header").hole;
}
function down() {
	
	// store the mouse coords
	mouse_x = window.event.clientX;
	mouse_y = window.event.clientY;

	// Check that the mousedown is within the bounds of the map
	if (mouse_x >= lBound && mouse_x <= rBound && mouse_y >= tBound && mouse_y <= bBound) {
		
		//stuff for click and set
		// dy means divyardage
		dyX1 = window.div_drag_yardage_box.style.left;
		dyX1 = parseInt(dyX1.substring(0,dyX1.length-2));
		dyX2 = window.div_drag_yardage_box.style.width;
		dyX2 = dyX2.substring(0,dyX2.length-2);
		dyX2 = parseInt(dyX2) + parseInt(dyX1) - 2;
		dyY1 = window.div_drag_yardage_box.style.top;
		dyY1 = parseInt(dyY1.substring(0,dyY1.length-2));
		dyY2 = window.div_drag_yardage_box.style.height;
		dyY2 = dyY2.substring(0,dyY2.length-2);
		dyY2 = parseInt(dyY2) + parseInt(dyY1);
		// Catch a click In the Yardage Box
		if (mouse_x >= dyX1 && mouse_x <= dyX2 && mouse_y >= dyY1 && mouse_y <= dyY2)
			return false;
		
		// Set the ball and yardagebox objects
		ball = window.div_drag_ball;
		yardagebox = window.div_drag_yardage_box;
		
		// Make the ball, yardage box, and line visible
		ball.style.visibility = 'visible';
		yardagebox.style.visibility = 'visible';
		
		// Set the yardage box number
		window.span_yardage_box.innerText = getYardageDistance(mouse_x - origin_x, mouse_y - origin_y);
		
		// Set the position of the yardage box and ball
		moveMouseFollowers();
		
		// Clear the canvas
		g.clear();
	}
	else
		return false;
		
		
	// turn off the flashing ball
	window.parent.frames("header").setFlashingBall(0);
}

function move() {
	// store the mouse coords
	mouse_x = window.event.clientX;
	mouse_y = window.event.clientY;
	if (ball && mouse_x >= lBound && mouse_x <= rBound && mouse_y >= tBound && mouse_y <= bBound) {
	
		// Set the yardage box number
		window.span_yardage_box.innerText = getYardageDistance(mouse_x - origin_x, mouse_y - origin_y);
		
		// Set the position of the yardage box and ball
		moveMouseFollowers();

	}
	return false;
}

function moveMouseFollowers() {

	// Get the mouse's x and y
	var x = window.event.clientX;
	var y = window.event.clientY;
	
	// Set the position of the ball and yardagebox
	if (x <= rBound && x >= lBound && y >= tBound && y <= bBound) {
		ball.style.posLeft = (x - 5);
		ball.style.posTop = (y - 9);
		
		// Corrections for too close to the boundary's
		if (x < (lBound + 89)) xdiff = 10; else xdiff = -90;
		if (y > (bBound - 49)) ydiff = -55; else ydiff = 10;
		
		yardagebox.style.posLeft = (x + xdiff);
		yardagebox.style.posTop = (y + ydiff);
	}
}

function up() {
	// Checks that the mousedown was within the bounds, so that there is actual numbers to work with
	if (ball) {
		// Draw the line from tee
		g.clear();
		g.setStroke(1);
		g.setColor("#FFFFFF");
		g.drawLine(origin_x, origin_y, ball.style.posLeft - lBound + 4, ball.style.posTop - tBound + 5);
		g.paint();
		
		coord_new_origin_x = ball.style.posLeft+4;
		coord_new_origin_y = ball.style.posTop+5;
	}
	// Leave the ball where it's at
	ball = null;
}

// sets a new origin point when clicked on the yardage box
function setNewOrigin() {
	// change to the coordinates of the ball
	origin_x = parseInt(coord_new_origin_x - lBound);
	origin_y = parseInt(coord_new_origin_y - tBound);
	// clear the line
	g.clear();
	// hides the ball
	//ball.style.visibility = "hidden";
	// hide the yardage box and move it out of the way
	yardagebox.style.visibility = "hidden";
	yardagebox.style.posLeft = -100;
	yardagebox.style.posTop = -100;
	// set the red marker
	window.div_drag_red_dot.style.visibility = "visible";
	window.div_drag_red_dot.style.posLeft = coord_new_origin_x - 4;
	window.div_drag_red_dot.style.posTop = coord_new_origin_y - 9;
	window.div_drag_ball.visibility = "hidden";
}

// clears all this shiz...
function clearCanvas() {
	g.clear();
	window.div_drag_ball.style.visibility = "hidden";
	window.div_drag_red_dot.style.visibility = "hidden";
	window.div_drag_yardage_box.style.visibility = "hidden";
}

function getYardageDistance(xleg, yleg) {
	// subtract the placement of the map! - super important!
	xleg -= lBound;
	yleg -= tBound;
	// Calulates line length using pythagorean theroem
	a = eval(xleg * xleg); // Square the x leg
	b = eval(yleg * yleg); // Square the y leg
	c = Math.sqrt(a + b);  // Get square root of a plus b
	return Math.round(c * yards_per_pixel);	
}

-->