var ge=null,geocoder=null,sat_ray=null,nray=null,sray=null,flat=51.494,flon=-0.125,sat_lon=13,dish=null,azimuth=null,azimuth_text=null,live=null;google.load("earth","1",{other_params:"sensor=true"});google.load("maps","2.99");function init(){geocoder=new GClientGeocoder();google.earth.setLanguage("en");google.earth.createInstance("map3d",initB,onError)}function initB(a){ge=a;el("geplugin_version").innerHTML=ge.getPluginVersion();ge.getWindow().setVisibility(true);ge.getOptions().setOverviewMapVisibility(true);geShowBorders(false);geShowNavCtrl();geDrawGeoOrbit();if(google&&google.loader&&google.loader.ClientLocation){flat=google.loader.ClientLocation.latitude;flon=google.loader.ClientLocation.longitude;initC()}else{initC()}}function initC(){geDrawSatRay();geDrawNorthRay();geDrawSouthRay();dish=geCreatePointPlacemark(flat,flon,getLatStr(flat)+"    "+getLngStr(flon));azimuth=geDrawAzimuth(flat,flon);el("addr").value=Math.round(flat*1000)/1000+"     "+Math.round(flon*1000)/1000;setTimeout(function(){geMoveCamera(flat,flon,400000,undefined)},1000);google.earth.addEventListener(ge.getGlobe(),"mousedown",onGlobeClick)}function onError(a){}function onGlobeClick(a){setTimeout(function(){if(a.getButton()==2){flat=a.getLatitude();flon=a.getLongitude();setTimeout(function(){geMoveCamera(flat,flon,undefined,undefined)},500);geDrawSatRay();geDrawNorthRay();geDrawSouthRay();geDeletePlacemark(dish);dish=geCreatePointPlacemark(flat,flon,getLatStr(flat)+"    "+getLngStr(flon));el("addr").value=Math.round(flat*1000)/1000+"     "+Math.round(flon*1000)/1000;geDeletePlacemark(azimuth);azimuth=geDrawAzimuth(flat,flon)}},0)}function geDrawSatRay(){if(sat_ray){ge.getFeatures().removeChild(sat_ray)}sat_lon=el("satlist").value;sat_ray=geDrawLine(flat,flon,0,parseFloat(sat_lon),1,"ffFFE5B2")}function geDrawNorthRay(){if(nray){ge.getFeatures().removeChild(nray)}if(jsIsChecked("nr")){nray=geDrawLine(90,flon,flat,flon,1,"ffff0000")}}function geDrawSouthRay(){if(sray){ge.getFeatures().removeChild(sray)}if(jsIsChecked("sr")){sray=geDrawLine(-90,flon,flat,flon,1,"ff0000ff")}}function geDrawLine(e,b,h,c,a,d){var g=ge.createPlacemark(""),i=ge.createLineString("");g.setGeometry(i);i.setTessellate(true);i.getCoordinates().pushLatLngAlt(e,b,0);i.getCoordinates().pushLatLngAlt(h,c,0);g.setStyleSelector(ge.createStyle(""));var f=g.getStyleSelector().getLineStyle();f.setWidth(f.getWidth()+a);f.getColor().set(d);ge.getFeatures().appendChild(g);return g}function geCreatePointPlacemark(e,b,d){var a=ge.createPoint(""),c=ge.createPlacemark("");a.setLatitude(e);a.setLongitude(b);c.setName(d);c.setGeometry(a);ge.getFeatures().appendChild(c);return c}function geDeletePlacemark(a){if(a){ge.getFeatures().removeChild(a)}}function getLatStr(b){var g="N";if(b<0){b*=-1;g="S"}var e=parseInt(b);var f=(b-e)*60;var a=parseInt(f);var c=parseInt((f-a)*60);return"Lat: "+e.toString()+"°"+a.toString()+"'"+c.toString()+'"'+g}function getLngStr(b){var g="E";if(b<0){b*=-1;g="W"}var e=parseInt(b);var f=(b-e)*60;var a=parseInt(f);var c=parseInt((f-a)*60);return"Lon: "+e.toString()+"°"+a.toString()+"'"+c.toString()+'"'+g}function geDrawGeoOrbit(){geDrawLine(0,0,0,180,1,"33ffeedd");geDrawLine(0,0,0,-180,1,"33ffeedd")}function geMoveCamera(d,c,b,a){var e=ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);if(d!=undefined){e.setLatitude(d)}if(c!=undefined){e.setLongitude(c)}if(b!=undefined){e.setRange(b)}if(a!=undefined){e.setTilt(a)}ge.getView().setAbstractView(e)}function geShowNavCtrl(){var b=ge.getNavigationControl();var a=b.getScreenXY();a.setXUnits(ge.UNITS_INSET_PIXELS);a.setYUnits(ge.UNITS_INSET_PIXELS);b.setVisibility(ge.VISIBILITY_SHOW)}function geHideCtrl(){ge.getNavigationControl().setVisibility(ge.VISIBILITY_HIDE)}function geShowSun(a){ge.getSun().setVisibility(a)}function geShowGrid(a){ge.getOptions().setGridVisibility(a)}function geShowBorders(a){ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS,a)}function jsShowLive(){geDeletePlacemark(live);live=geCreatePointPlacemark(flat*-1,flon<0?flon+180:flon-180,"Here is other side :)");geMoveCamera(flat*-1,flon<0?flon+180:flon-180,undefined,undefined)}function geSubmitLocation(){geocoder.getLatLng(el("addr").value,function(a){if(a&&ge!=null){flat=a.y;flon=a.x;setTimeout(function(){geMoveCamera(flat,flon,4000,undefined)},500);geDrawSatRay();geDrawNorthRay();geDrawSouthRay();geDeletePlacemark(dish);dish=geCreatePointPlacemark(flat,flon,getLatStr(flat)+"    "+getLngStr(flon));el("addr").value=Math.round(flat*1000)/1000+"     "+Math.round(flon*1000)/1000;geDeletePlacemark(azimuth);azimuth=geDrawAzimuth(flat,flon)}})}function jsAddrOnKeydown(a){if(a.keyCode==13){geSubmitLocation()}}function jsOnSatSel(){geDrawSatRay();geDeletePlacemark(azimuth);azimuth=geDrawAzimuth(flat,flon)}function jsOnBorders(){geShowBorders(el("borders").checked)}function jsOnSun(){geShowSun(el("sun").checked)}function jsOnGrid(){geShowGrid(el("grid").checked)}function jsIsChecked(a){if(el(a).checked){return true}return false}function jsGetAz(){var a=parseFloat(flon);var d=parseFloat(sat_lon);var b=a>d?a-d:d-a;var c=((Math.atan(Math.tan((Math.PI*(a-d))/180)/Math.sin((Math.PI*flat)/180))*180)/Math.PI);if(b<90){if(flat<0){if(c<0){c+=360}}else{c+=180}}else{if(flat>0){if(c<0){c+=360}}else{c+=180}}return c}function jsGetEarthRadiusAtLat(d){var b=Math.pow(Math.pow(6378137,2)*Math.cos(d),2);var f=Math.pow(Math.pow(6356752.3142,2)*Math.sin(d),2);var e=Math.pow(6378137*Math.cos(d),2);var c=Math.pow(6356752.3142*Math.sin(d),2);var a=Math.sqrt((b+f)/(e+c));return a}function jsGetEarthDistance(d,c,b,a){return 6378137*Math.acos(Math.sin((Math.PI*d)/180)*Math.sin((Math.PI*b)/180)+Math.cos((Math.PI*d)/180)*Math.cos((Math.PI*b)/180)*Math.cos((Math.PI*(c>a?c-a:a-c))/180))}function geDrawAzimuth(f,h){var j,g;var k=jsGetAz();var c=ge.createLineString("");c.setTessellate(true);var a=(Math.PI*k)/180;g=Math.cos(a);var e=111319.49079326246/jsGetEarthDistance(f+g,10,f+g,11);for(var b=0;b<k;b+=1){a=(Math.PI*b)/180;j=Math.sin(a)*e;g=Math.cos(a);c.getCoordinates().pushLatLngAlt(f+g,h+j,0)}a=(Math.PI*k)/180;j=Math.sin(a)*e;g=Math.cos(a);c.getCoordinates().pushLatLngAlt(f+g,h+j,0);var d=ge.createPlacemark("");d.setGeometry(c);ge.getFeatures().appendChild(d);geDeletePlacemark(azimuth_text);azimuth_text=geCreatePointPlacemark(f+g,h+j,getAzStr(k));return d}function getAzStr(b){var d=parseInt(b);var e=(b-d)*60;var a=parseInt(e);var c=parseInt((e-a)*60);return"Azimuth: "+d.toString()+"°"+a.toString()+"'"+c.toString()}function el(a){return document.getElementById(a)};