var mapToolsMode = "none";
var myPositionMarker;
var arrowMarker;
var myPositionX;
var myPositionY;
var measureMarker1;
var measurePoint1;
var measureMarker2;
var measurePoint2;
var measureLine;
var iamhere;           // Map icon for my location

function degreesFromCoord(coord) {
	if (coord >= 0)
   		return Math.floor(coord);
   	else
   		return Math.floor(Math.abs(coord)) * -1;
}

function minutesFromCoord(coord) {
	var fractionPart = Math.abs(coord) - Math.floor(Math.abs(coord));
	return fractionPart * 60;
}

function secondsFromCoord(coord) {
	coord = Math.abs(coord);
	var fractionPart = coord - Math.floor(coord);
	return Math.round(((fractionPart*60) - Math.floor(fractionPart*60)) * 60);
}

function distanceBetween(p1, p2) {
  lat1 = p1.y * Math.PI/180.0;
  lat2 = p2.y * Math.PI/180.0;
  lon1 = p1.x * Math.PI/180.0;
  lon2 = p2.x * Math.PI/180.0;
  return Math.acos(Math.cos(lat1)*Math.cos(lon1)*Math.cos(lat2)*Math.cos(lon2) + Math.cos(lat1)*Math.sin(lon1)*Math.cos(lat2)*Math.sin(lon2) + Math.sin(lat1)*Math.sin(lat2)) * 3443.9;
}

function FormatFloat(pFloat, pDp){
    var m = Math.pow(10, pDp);
    return parseInt(pFloat * m, 10) / m;
}


function initialize_map_tools() {
  // Show my location marker with a custom icon	
  iamhere = new GIcon();
  iamhere.image = "/images/Sailboat.png";
  iamhere.shadow = "/images/SailboatShadow.png";
  iamhere.iconSize = new GSize(35, 36);
  iamhere.shadowSize = new GSize(60, 36);
  iamhere.iconAnchor = new GPoint(18, 36);
  iamhere.infoWindowAnchor = new GPoint(18, 0);

  arrow = new GIcon();
  arrow.image = "/images/Arrow.png";
  arrow.shadow = "/images/ArrowShadow.png";
  arrow.iconSize = new GSize(35, 35);
  arrow.shadowSize = new GSize(70, 35);
  arrow.iconAnchor = new GPoint(0, 35);
  arrow.infoWindowAnchor = new GPoint(35, 35);

  iconA = new GIcon();
  iconA.image = "/images/iconA.png";
  iconA.shadow = "/images/measure_shadow.png";
  iconA.iconSize = new GSize(25, 28);
  iconA.shadowSize = new GSize(45, 28);
  iconA.iconAnchor = new GPoint(12, 28);
  iconA.infoWindowAnchor = new GPoint(12, 0);

  iconB = new GIcon();
  iconB.image = "/images/iconB.png";
  iconB.shadow = "/images/measure_shadow.png";
  iconB.iconSize = new GSize(25, 28);
  iconB.shadowSize = new GSize(45, 28);
  iconB.iconAnchor = new GPoint(12, 28);
  iconB.infoWindowAnchor = new GPoint(12, 0);

  // Add NOAA map type
  noaaLayer = new GTileLayer(new GCopyrightCollection(""), 8, 14);
  //noaaLayer.myBaseURL = "/noaamap/images?";
  noaaLayer.myBaseURL = "/noaamap/";
  noaaLayer.myFormat = 'image/png';
  noaaLayer.myLayers = 'noaa';
  noaaLayer.getTileUrl = function(p,z) {
	//return this.myBaseURL + "x=" + p.x + "&y=" + p.y + "&zoom=" + z;
	return this.myBaseURL + p.x + "_" + p.y + "_" + z + ".png";
  }
  noaaLayer.isPng = function() { return true; }
  noaaLayer.getOpacity = function() { return 0.65; }
  noaaMapLayers = [G_HYBRID_MAP.getTileLayers()[0], noaaLayer];
  
  var noaaMap = new GMapType(noaaMapLayers, G_SATELLITE_MAP.getProjection(), "NOAA", G_SATELLITE_MAP);
  mymap.addMapType(noaaMap);
  mymap.addControl(new GMapTypeControl());

  // Configure click listener
  GEvent.addListener(mymap, "click", function(overlay, point) {
	if (mapToolsMode == "none") {
		if (arrowMarker) {
			mymap.removeOverlay(arrowMarker);
		}
		arrowMarker = new GMarker(point, arrow);
		mymap.addOverlay(arrowMarker);
		$('mapheader').innerHTML = "<p>Lat: " + degreesFromCoord(point.y) + "&deg; " + FormatFloat(minutesFromCoord(point.y), 2) + "\'  Lon: " + degreesFromCoord(point.x) + "&deg; " + FormatFloat(minutesFromCoord(point.x), 2) + "\' <img src='/images/Arrow.png' width='15' height='15'/> " + "<a href=\"/directory/find_near?lat=" + point.y + "&lon=" + point.x + "\">What's near?</a>";
	}
	if (mapToolsMode == "setpos") {
	  if (myPositionMarker) {
	    mymap.removeOverlay(myPositionMarker);	
	  }
	  myPositionMarker = new GMarker(point, iamhere);
	  mymap.addOverlay(myPositionMarker);
	  myPositionX = point.x;
	  myPositionY = point.y;
	  $("myposition").innerHTML = "Lat: " + degreesFromCoord(point.y) + "\' " + FormatFloat(minutesFromCoord(point.y), 2) + "\"  Lon: " + degreesFromCoord(point.x) + "\' " + FormatFloat(minutesFromCoord(point.x), 2) + "\"";
	  document.cookie = 'lat=' + point.y + '; expires=Tue, 1 Jan 2008 20:47:11 UTC; path=/';
	  document.cookie = 'lon=' + point.x + '; expires=Tue, 1 Jan 2008 20:47:11 UTC; path=/';
	  $('find_ship').style.display = "block";
	}
	if (mapToolsMode == "measure2") {
      measureMarker2 = new GMarker(point, iconB);
      measurePoint2 = point;
      mymap.addOverlay(measureMarker2);
	  measureLine = new GPolyline([
	      measurePoint1,
	      measurePoint2
	      ], "#ff0000", 10);
	  mymap.addOverlay(measureLine);
	  mapToolsMode = "none";
 	  $("start_measure").innerHTML = "(" + distanceBetween(measurePoint1, measurePoint2).toFixed(1) + " nm) <br/><a href=\"javascript:start_measure();\">Measure</a>";
      end_measure();
	}
	if (mapToolsMode == "measure1") {
	  if (measureMarker1) {
		mymap.removeOverlay(measureMarker1);
 	  }
	  if (measureMarker2) {
		mymap.removeOverlay(measureMarker2);
 	  }
	  if (measureLine) {
		mymap.removeOverlay(measureLine);
 	  }
      measureMarker1 = new GMarker(point, iconA);
      measurePoint1 = point;
      mymap.addOverlay(measureMarker1);
      mapToolsMode = "measure2";
	}
  });	
}

function start_set_position() {
	mapToolsMode = "setpos";
	$('start_set_position').style.display = "none";
	$('end_set_position').style.display = "block";
	$('maptools_help_contents').innerHTML = "Set your position by clicking on the map, then <a href=\"javascript:end_set_position();\">click here</a> when finished.";
	$('maptools_help').style.display = "block";
}

function end_set_position() {
	mapToolsMode = "none";
	$('start_set_position').style.display = "block";
	$('end_set_position').style.display = "none";
	$('maptools_help').style.display = "none";
}

function start_measure() {
	mapToolsMode = "measure1";
	$("start_measure").style.display = "none";
	$("maptools_help_contents").innerHTML = "Click a start and end point on the map to measure the distance between them.";
	$("maptools_help").style.display = "block";
	$("start_set_position").style.display = "block";
	$("end_set_position").style.display = "none";
}

function end_measure() {
	mapToolsMode = "none";
	$("start_measure").style.display = "block";
	$("maptools_help").style.display = "none";
	$("end_set_position").style.display = "none";
}

function find_ship() {
	if (myPositionMarker) {
		mymap.setCenter(new GLatLng(myPositionY, myPositionX));
		//mymap.centerAndZoom(new GPoint(myPositionX, myPositionY), mymap.getZoomLevel());
	}
}
