var map,marker,geocoder,elevator,terminatorLayer,LL_STYLE={D:0,DM:1,DMS:2},llstyle=LL_STYLE.D,lat_start=51,lon_start=10,zoom_start=6;function terminatorOverlay(){var a={getTileUrl:function(b,c){return"termovl.php?z="+c+"&x="+b.x+"&y="+b.y+"&t="+Math.round((new Date).getTime()/6E4)},tileSize:new google.maps.Size(256,256),isPng:true,opacity:0.2,maxZoom:18,name:"Terminator"};return new google.maps.ImageMapType(a)}
function displayTerminator(a){a===true?map.overlayMapTypes.insertAt(0,terminatorOverlay()):map.overlayMapTypes.removeAt(0)}function dec2dms(a){var b=a>=0?1:-1;a=Math.abs(a);var c=Math.floor(a+1.0E-5);a=(a-c)*60+1.0E-5;var e=Math.floor(a);return[b,c,e,(a-e)*60]}function dec2dm(a){var b=a>=0?1:-1;a=Math.abs(a);var c=Math.floor(a+1.0E-5);return[b,c,(a-c)*60]}function dms2dec(a,b,c){var e=a>=0?1:-1;a=Math.abs(a);return e*(a+b/60+c/3600)}
function parseCoord(a){var b,c,e,d=/^ *([+-]?) *(\d+\.?\d*)\S? *([NSEOW]?) *$/;if(d=d.exec(a)){b=parseFloat(d[2]);c=d[1];e=d[3];llstyle=LL_STYLE.D}else{d=/^ *([+-]?) *(\d+)\S? +(\d+\.?\d*)\S? *([NSEOW]?) *$/;if(d=d.exec(a)){b=dms2dec(parseFloat(d[2]),parseFloat(d[3]),0);c=d[1];e=d[4];llstyle=LL_STYLE.DM}else{d=/^ *([+-]?) *(\d+)\S? +(\d+)\S? +(\d+\.?\d*)\S? *([NSEOW]?) *$/;if(d=d.exec(a)){b=dms2dec(parseFloat(d[2]),parseFloat(d[3]),parseFloat(d[4]));c=d[1];e=d[5];llstyle=LL_STYLE.DMS}else alert("Invalid format")}}if(c==
"-"||e=="S"||e=="W")b=-b;return b}function displayLocationOnMap(a,b,c){if(map){a=new google.maps.LatLng(a,b);if(marker)marker.setPosition(a);else marker=new google.maps.Marker({position:a,map:map});c&&map.setCenter(a)}}function formatD(a){return a.toPrecision(7)}function hemi(a,b){return a>0?b?"E":"N":b?"W":"S"}function formatDM(a,b){a=dec2dm(a);return a[1]+"\u00b0 "+a[2].toFixed(4)+"\u2032 "+hemi(a[0],b)}
function formatDMS(a,b){a=dec2dms(a);return a[1]+"\u00b0 "+a[2]+"\u2032 "+a[3].toFixed(2)+"\u2033 "+hemi(a[0],b)}
function displayLocationOnForm(a,b){var c=utmLonZone(b),e=[],d=utmLatZone(a);switch(llstyle){case LL_STYLE.D:document.forms.decform.londec.value=formatD(b);document.forms.decform.latdec.value=formatD(a);break;case LL_STYLE.DM:document.forms.decform.londec.value=formatDM(b,true);document.forms.decform.latdec.value=formatDM(a,false);break;case LL_STYLE.DMS:document.forms.decform.londec.value=formatDMS(b,true);document.forms.decform.latdec.value=formatDMS(a,false);break}document.getElementById("latd").innerHTML=
formatD(a);document.getElementById("lond").innerHTML=formatD(b);document.getElementById("latdm").innerHTML=formatDM(a,false);document.getElementById("londm").innerHTML=formatDM(b,true);document.getElementById("latdms").innerHTML=formatDMS(a,false);document.getElementById("londms").innerHTML=formatDMS(b,true);c=latLonToUTMXY(degToRad(a),degToRad(b),c,e);document.forms.utmform.x.value=Math.round(e[0]);document.forms.utmform.y.value=Math.round(e[1]);document.forms.utmform.z.value=c+d;document.getElementById("geodata-sundiag").href=
"sun/sundiag.php?size=400x800&year="+encodeURIComponent((new Date).getFullYear())+"&lat="+encodeURIComponent(a)+"&lon="+encodeURIComponent(b)}
function retrieveGeodata(a,b){document.getElementById("geodata-ele").innerHTML="&nbsp;";document.getElementById("geodata-tz").innerHTML="&nbsp;";document.getElementById("geodata-time").innerHTML="&nbsp;";document.getElementById("geodata-sr").innerHTML="&nbsp;";document.getElementById("geodata-ss").innerHTML="&nbsp;";downloadUrl("geoservice.php?lat="+encodeURIComponent(a)+"&lon="+encodeURIComponent(b)+"&key="+encodeURIComponent(key),displayGeodata);if(elevator){a={locations:[new google.maps.LatLng(a,
b)]};elevator.getElevationForLocations(a,displayElevation)}}function clearAddress(){document.forms.searchform.address.value=""}
function displayGeodata(a,b){var c;if(b===200)try{if(c=JSON.parse(a)){if(c.ele&&c.ele>-32000)document.getElementById("geodata-ele").innerHTML=c.ele+" m";if(c.timezone)document.getElementById("geodata-tz").innerHTML=c.timezone;if(c.time)document.getElementById("geodata-time").innerHTML=c.time;if(c.sunrise)document.getElementById("geodata-sr").innerHTML=c.sunrise;if(c.sunset)document.getElementById("geodata-ss").innerHTML=c.sunset}}catch(e){}}
function displayElevation(a,b){if(b==google.maps.ElevationStatus.OK)if(a[0]){a=parseFloat(a[0].elevation);document.getElementById("geodata-ele").innerHTML=a.toFixed(0)+" m"}}function showDec(){var a=parseCoord(document.forms.decform.londec.value),b=parseCoord(document.forms.decform.latdec.value);clearAddress();displayLocationOnForm(b,a);displayLocationOnMap(b,a,true);retrieveGeodata(b,a)}
function showUtm(){var a=[],b=parseInt(document.forms.utmform.x.value,10),c=parseInt(document.forms.utmform.y.value,10),e=false,d,f=/(\d\d?)([C-X])/.exec(document.forms.utmform.z.value);if(f===null)alert("Invalid format of UTM zone. Enter text like 32U or 16T.");else{d=parseInt(f[1],10);if(f[2]<"N")e=true;utmXYToLatLon(b,c,d,e,a);clearAddress();displayLocationOnForm(radToDeg(a[0]),radToDeg(a[1]));displayLocationOnMap(radToDeg(a[0]),radToDeg(a[1]),true);retrieveGeodata(radToDeg(a[0]),radToDeg(a[1]))}}
function searchAddress(){var a=document.forms.searchform.address.value;geocoder&&geocoder.geocode({address:a,bounds:map.getBounds()},function(b,c){if(c==google.maps.GeocoderStatus.OK){b=b[0].geometry.location;displayLocationOnForm(b.lat(),b.lng());displayLocationOnMap(b.lat(),b.lng(),true);retrieveGeodata(b.lat(),b.lng())}else alert(a+" not found. Status = "+c)})}
function onLoad(){var a=lat_start,b=lon_start,c=zoom_start,e=new google.maps.LatLng(a,b);c={zoom:c,center:e,overviewMapControl:true,mapTypeId:google.maps.MapTypeId.ROADMAP,mapTypeControlOptions:{mapTypeIds:[google.maps.MapTypeId.HYBRID,google.maps.MapTypeId.SATELLITE,google.maps.MapTypeId.ROADMAP,google.maps.MapTypeId.TERRAIN,"OSM"]}};if(map=new google.maps.Map(document.getElementById("map"),c)){autoResizeMap(map,350,100);osmMap(map);google.maps.event.addListener(map,"click",function(d){d=d.latLng;
clearAddress();displayLocationOnForm(d.lat(),d.lng());displayLocationOnMap(d.lat(),d.lng());retrieveGeodata(d.lat(),d.lng())})}clearAddress();displayLocationOnMap(a,b);geocoder=new google.maps.Geocoder;elevator=new google.maps.ElevationService;displayLocationOnForm(a,b);retrieveGeodata(a,b)};

