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.gears){function c(b){flat=b.latitude;flon=b.longitude;initC()}function d(b){flat=51.494;flon=-0.125;initC()}google.gears.factory.create("beta.geolocation").getCurrentPosition(c,d)}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*1E3)/1E3+"     "+Math.round(flon*1E3)/1E3;setTimeout(function(){geMoveCamera(flat,flon,4E5,undefined)},1E3);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*1E3)/1E3+"     "+Math.round(flon*1E3)/1E3;geDeletePlacemark(azimuth);azimuth=geDrawAzimuth(flat,flon)}},0)}
function geDrawSatRay(){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(){nray&&ge.getFeatures().removeChild(nray);if(jsIsChecked("nr"))nray=geDrawLine(90,flon,flat,flon,1,"ffff0000")}function geDrawSouthRay(){sray&&ge.getFeatures().removeChild(sray);if(jsIsChecked("sr"))sray=geDrawLine(-90,flon,flat,flon,1,"ff0000ff")}
function geDrawLine(a,c,d,b,e,h){var f=ge.createPlacemark(""),g=ge.createLineString("");f.setGeometry(g);g.setTessellate(true);g.getCoordinates().pushLatLngAlt(a,c,0);g.getCoordinates().pushLatLngAlt(d,b,0);f.setStyleSelector(ge.createStyle(""));a=f.getStyleSelector().getLineStyle();a.setWidth(a.getWidth()+e);a.getColor().set(h);ge.getFeatures().appendChild(f);return f}
function geCreatePointPlacemark(a,c,d){var b=ge.createPoint("");b.setLatitude(a);b.setLongitude(c);a=ge.createPlacemark("");a.setName(d);a.setGeometry(b);ge.getFeatures().appendChild(a);return a}function geDeletePlacemark(a){a&&ge.getFeatures().removeChild(a)}function getLatStr(a){var c="N";if(a<0){a*=-1;c="S"}var d=parseInt(a),b=(a-d)*60;a=parseInt(b);b=parseInt((b-a)*60);return"Lat: "+d.toString()+"\ufffd\ufffd"+a.toString()+"'"+b.toString()+'"'+c}
function getLngStr(a){var c="E";if(a<0){a*=-1;c="W"}var d=parseInt(a),b=(a-d)*60;a=parseInt(b);b=parseInt((b-a)*60);return"Lon: "+d.toString()+"\ufffd\ufffd"+a.toString()+"'"+b.toString()+'"'+c}function geDrawGeoOrbit(){geDrawLine(0,0,0,180,1,"33ffeedd");geDrawLine(0,0,0,-180,1,"33ffeedd")}
function geMoveCamera(a,c,d,b){var e=ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);a!=undefined&&e.setLatitude(a);c!=undefined&&e.setLongitude(c);d!=undefined&&e.setRange(d);b!=undefined&&e.setTilt(b);ge.getView().setAbstractView(e)}function geShowNavCtrl(){var a=ge.getNavigationControl(),c=a.getScreenXY();c.setXUnits(ge.UNITS_INSET_PIXELS);c.setYUnits(ge.UNITS_INSET_PIXELS);a.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,4E3,undefined)},500);geDrawSatRay();geDrawNorthRay();geDrawSouthRay();geDeletePlacemark(dish);dish=geCreatePointPlacemark(flat,flon,getLatStr(flat)+"    "+getLngStr(flon));el("addr").value=Math.round(flat*1E3)/1E3+"     "+Math.round(flon*1E3)/1E3;geDeletePlacemark(azimuth);azimuth=geDrawAzimuth(flat,flon)}})}
function jsAddrOnKeydown(a){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),c=parseFloat(sat_lon),d=a>c?a-c:c-a;a=Math.atan(Math.tan(Math.PI*(a-c)/180)/Math.sin(Math.PI*flat/180))*180/Math.PI;if(d<90)if(flat<0){if(a<0)a+=360}else a+=180;else if(flat>0){if(a<0)a+=360}else a+=180;return a}
function jsGetEarthRadiusAtLat(a){var c=Math.pow(Math.pow(6378137,2)*Math.cos(a),2),d=Math.pow(Math.pow(6356752.3142,2)*Math.sin(a),2),b=Math.pow(6378137*Math.cos(a),2);a=Math.pow(6356752.3142*Math.sin(a),2);c=Math.sqrt((c+d)/(b+a));alert(c);return c}function jsGetEarthDistance(a,c,d,b){return 6378137*Math.acos(Math.sin(Math.PI*a/180)*Math.sin(Math.PI*d/180)+Math.cos(Math.PI*a/180)*Math.cos(Math.PI*d/180)*Math.cos(Math.PI*(c>b?c-b:b-c)/180))}
function geDrawAzimuth(a,c){var d,b,e=jsGetAz(),h=ge.createLineString("");h.setTessellate(true);b=Math.PI*e/180;b=Math.cos(b);for(var f=111319.49079326246/jsGetEarthDistance(a+b,10,a+b,11),g=0;g<e;g+=1){b=Math.PI*g/180;d=Math.sin(b)*f;b=Math.cos(b);h.getCoordinates().pushLatLngAlt(a+b,c+d,0)}b=Math.PI*e/180;d=Math.sin(b)*f;b=Math.cos(b);h.getCoordinates().pushLatLngAlt(a+b,c+d,0);f=ge.createPlacemark("");f.setGeometry(h);ge.getFeatures().appendChild(f);geDeletePlacemark(azimuth_text);azimuth_text=
geCreatePointPlacemark(a+b,c+d,getAzStr(e));return f}function getAzStr(a){var c=parseInt(a),d=(a-c)*60;a=parseInt(d);d=parseInt((d-a)*60);return"Azimuth: "+c.toString()+"\ufffd\ufffd"+a.toString()+"'"+d.toString()}function el(a){return document.getElementById(a)};