//initialize clock for load timer
var earthnc_time = new Date();
var earthnc_then = earthnc_time.getTime();
var earthncmap = null;
 
//function to create the 'View in Google Earth' button
function EarthNCGEControl() {}
EarthNCGEControl.prototype = new GControl();
EarthNCGEControl.prototype.initialize = function(map) {
  var container=document.createElement("div");
  var zoomInDiv=document.createElement("div");this.setButtonStyle_(zoomInDiv);
  container.appendChild(zoomInDiv);
   
  var img=document.createElement('img');
  img.setAttribute('src','http://earthnc.com/images/EarthNC_link.png');
   
   zoomInDiv.appendChild(img);
   GEvent.addDomListener(zoomInDiv,"click",function(){
   var center = map.getCenter();
   window.open('http://earthnc.info/services/earthnconline.php?ll='+center.lat()+','+center.lng());
   });
   
  map.getContainer().appendChild(container);return container;}

EarthNCGEControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(55, 35));
  }

EarthNCGEControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "underline";
  button.style.color = "#0000cc";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}

//function to set proper URL to EarthNC Navaid file
function earthnc_getKMLUrl(nlat,nlon){
//base path
var URL = 'http://earthnc.com/kml/gmapsd/';

var loni = Math.floor(Math.abs(nlon));
var lond = Math.abs(nlon)-loni;
var lati = Math.floor(Math.abs(nlat));
var latd = Math.abs(nlat)-lati;

if (latd>=.75){latd=.75;} 
else if (latd<.75 && latd>=.5){latd=.5;} 
else if (latd<.5 && latd>=.25){latd=.25;} 
else if (latd<.25){latd=0;} 
if (lond>=.75){lond=0; loni+=1;} 
else if (lond<.75 && lond>=.5){lond=.75;}
else if (lond<.5 && lond>=.25){lond=.5;}
else if (lond<.25){lond=.25;}

 nlon = loni+lond;
 nlat = lati+latd;

URL = URL + nlat + '_' + nlon +'nearthncmarks.kmz';
lat = nlat;
lon = -nlon;
return URL;
}

function earthnc_chartlookup(){
  if (document.getElementById("showearthnc").checked){
    var nlat= map.getCenter().lat();
    var nlon= map.getCenter().lng();
    var zoom = map.getZoom();
    var time = new Date();
    var now = time.getTime();
    //test for movement magnitude and timeout
    if ((Math.abs(nlat-lat)>.5 || Math.abs(nlon-lon)>.5) && nlon<-45 && nlat>16 && zoom>=9 && (now-then)>12){
      var URL = earthnc_getKMLUrl(nlat,nlon);
      if (earthncmap != null){
      map.removeOverlay(earthncmap);
      }
      earthncmap = new GGeoXml(earthnc_getKMLUrl(nlat,nlon));
      map.addOverlay(earthncmap); 
      
      //reset timer
      then = now;
  }
  }
  else
  {
  map.removeOverlay(earthncmap);
  }
}

function earthnc_chartload(){
  if (document.getElementById("showearthnc").checked){
    var nlat= map.getCenter().lat();
    var nlon= map.getCenter().lng();
    var zoom = map.getZoom();
    var time = new Date();
    var now = time.getTime();

      var URL = earthnc_getKMLUrl(nlat,nlon);
      if (earthncmap != null){
      map.removeOverlay(earthncmap);
      }
      earthncmap = new GGeoXml(earthnc_getKMLUrl(nlat,nlon));
      map.addOverlay(earthncmap); 
      //reset timer
      then = now; 
 }
  else
  {
  map.removeOverlay(earthncmap);
      
  }
}
        
function earthnc_getUrlVariable(variable) {
  var url = document.location.href.split('?');
  if (url[1]){
  var vars = url[1].split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
    return pair[1];
    }
  }
  }
}

 function earthnc_kmlload(){
   //check for additional KML file
       if (earthnc_getUrlVariable("kmlurl")){
       var kmlurl = earthnc_getUrlVariable("kmlurl");
       kmlurl = kmlurl.replace(/%26/g,"&").replace(/%3F/g,"?").replace(/%3D/g,"=");
       }
       var kmlXml = new GGeoXml(kmlurl, function() {
        earthnc_zoomToGeoXML(kmlXml);
      }); 
       map.addOverlay(kmlXml);
 }

  function earthnc_zoomToGeoXML(geoXml) {
  if (earthnc_getUrlVariable("zkml")){
      var center = geoXml.getDefaultCenter();
      var span = geoXml.getDefaultSpan();
      var sw = new GLatLng(center.lat() - span.lat() / 2,
                       center.lng
      () - span.lng() / 2);
      var ne = new GLatLng(center.lat() + span.lat() / 2,
                       center.lng() + span.lng() / 2);
      var bounds = new GLatLngBounds(sw, ne);
      map.setCenter(center);
      map.setZoom
      (map.getBoundsZoomLevel(bounds));
      }
    }
    
function earthnc_setCenter(){
var lat = 37.846262;
var lon = -122.403238;
var zoom = 10;
if (earthnc_getUrlVariable("ll")){
var ll=earthnc_getUrlVariable("ll"); var z=earthnc_getUrlVariable("z");
map.setCenter(new GLatLng(parseFloat(ll.split(',')[0]),parseFloat(ll.split(',')[1])), parseFloat(z));     
}else{
map.setCenter(new GLatLng(lat, lon), zoom);}
}

