/*
 * convert lat/long in degrees to radians, for handling input values
 *
 *   this is very flexible on formats, allowing signed decimal degrees (numeric or text), or
 *   deg-min-sec suffixed by compass direction (NSEW). A variety of separators are accepted 
 *   (eg 3º 37' 09"W) or fixed-width format without separators (eg 0033709W). Seconds and minutes
 *   may be omitted. Minimal validation is done.
 */
LatLong.llToRad = function(brng) {
  if (!isNaN(brng)) return brng * Math.PI / 180;  // signed decimal degrees without NSEW

  brng = brng.replace(/[\s]*$/,'');               // strip trailing whitespace
  var dir = brng.slice(-1).toUpperCase();         // compass dir'n
  if (!/[NSEW]/.test(dir)) return NaN;            // check for correct compass direction
  brng = brng.slice(0,-1);                        // and lose it off the end
  var dms = brng.split(/[\s:,°º′\'″\"]/)          // check for separators indicating d/m/s
  switch (dms.length) {                           // convert to decimal degrees...
    case 3:                                       // interpret 3-part result as d/m/s
      var deg = dms[0]/1 + dms[1]/60 + dms[2]/3600; break;
    case 2:                                       // interpret 2-part result as d/m
      var deg = dms[0]/1 + dms[1]/60; break;
    case 1:                                       // non-separated format dddmmss
      if (/[NS]/.test(dir)) brng = '0' + brng;    // - normalise N/S to 3-digit degrees
      var deg = brng.slice(0,3)/1 + brng.slice(3,5)/60 + brng.slice(5)/3600; break;
    default: return NaN;
  }
  if (/[WS]/.test(dir)) deg = -deg;               // take west and south as -ve
  return deg * Math.PI / 180;                     // then convert to radians
}



/*
 * LatLong constructor:
 */
function LatLong(degLat, degLong) {
  this.lat = LatLong.llToRad(degLat);
  this.lon = LatLong.llToRad(degLong);
}


/*
 * Calculate distance (in km) between two points specified by latitude/longitude with Haversine formula
 *
 */
function distancebetween(p1, p2) {
  var R = 6371; // earth's mean radius in km
  var dLat  = p2.lat - p1.lat;
  var dLong = p2.lon - p1.lon;

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(p1.lat) * Math.cos(p2.lat) * Math.sin(dLong/2) * Math.sin(dLong/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;

  return d;
}


var map;        // added 

function createMapNoMarkers(dblLatitude, dblLongitude, lngZoom, dlat, dlong) { 
    // creates a map with no markers 
    if (GBrowserIsCompatible()) {            
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.setCenter(new GLatLng(dblLatitude, dblLongitude), lngZoom);
    var hpos=new LatLong(dlat,dlong)
    } 
    else{
    alert("Sorry, your browser is not compatible")
    }
} 

// Creates a marker at the given point with the given number label
function createMarker(point, text, lat, lng, dlat, dlong) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
  var thispos=new LatLong(lat,lng) 
  var hpos=new LatLong(dlat,dlong)
  var db=distancebetween(thispos,hpos)
  var dbmiles=db * 0.621371192  
  var km=Math.round(db*10)/10
  var miles=Math.round(dbmiles*10)/10
  document.getElementById("HMousePos").innerHTML=text
  if(db>0){
  //document.getElementById("MousePos").innerHTML+= " (" + miles + " mi/" + km + " km from property)"
  }  
  });
  GEvent.addListener(marker, "mouseover", function() {
  var thispos=new LatLong(lat,lng) 
  var hpos=new LatLong(dlat,dlong)
  var db=distancebetween(thispos,hpos)
  var dbmiles=db * 0.621371192 
  var km=Math.round(db*10)/10
  var miles=Math.round(dbmiles*10)/10
  document.getElementById("HMousePos").innerHTML=text
  if(db>0){
  //document.getElementById("MousePos").innerHTML+= " (" + miles + " mi/" + km + " km from property)"
  } 
  });  
  GEvent.addListener(marker, "mouseout", function() {
  document.getElementById("HMousePos").innerHTML=dest
  });
  return marker;
}


function createMainMarker(dblLatitude, dblLongitude) { 
// creates a marker on a previously created map 
var point = new GLatLng(dblLatitude, dblLongitude);
map.addOverlay(createMarker(point,"",dblLatitude,dblLongitude)); 
}

function landmark(dlat,dlong){
var sel=document.getElementById("landmarks")
var lmark=sel.options[sel.selectedIndex].value

if(lmark!=""){
var lsplit=lmark.split("|")
var co=lsplit[1]
var csplit=co.split(",")
createNewMarker(csplit[0],csplit[1],lsplit[0],dlat,dlong)
//document.getElementById("HMousePos").innerHTML="Landmark located - hover or click to see distance from property"
}
}

function createNewMarker(dblLatitude, dblLongitude,title,dlat,dlong) { 
// creates a marker on a previously created map 
var point = new GLatLng(dblLatitude, dblLongitude);
map.addOverlay(createMarker(point,title,dblLatitude,dblLongitude,dlat,dlong));
map.setCenter(point, 14); 
}