//Copyright 2008 EarthNC, Inc www.earthnc.com
//initialize clock for load timer
var earthnc_time = new Date();
var earthnc_then = earthnc_time.getTime();
var earthncmap = null;
var rlevel = null;
var rchart = null;
var mapcontrol = null;
var tilelayers = null;   var map = null;
var rastermap; var opacity=.5;
var lat;
var lon;


function show(did){
document.getElementById(did).style.display='block';
}

function hide(did){
document.getElementById(did).style.display='none';
}
    
//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;
      lat = nlat; lon = nlon;
  }
  }
  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);
  }
}

