function GReverseGeocoder(a){this.map=a;this.gdirections=new GDirections();this.geocoder=new GClientGeocoder();this.bounds=0.1;this.lastpoint=null;this.closestonroad=null;this.experimental=false;this.ad="";this.step=10;this.start=1;this.gdirectionsrefine=new GDirections();GEvent.bind(this.gdirections,"error",this,this.handleError);GEvent.bind(this.gdirections,"load",this,this.processDirection);GEvent.bind(this.gdirectionsrefine,"error",this,this.handleError);GEvent.bind(this.gdirectionsrefine,"load",this,this.processDirectionRefine)}GReverseGeocoder.prototype.reverseGeocode=function(a){this.lastpoint=a;this.closestonroad=null;this.gdirections.clear();this.gdirections.loadFromWaypoints([a.toUrlValue(6),a.toUrlValue(6)],{getSteps:true,locale:"GB",getPolyline:true})};GReverseGeocoder.prototype.getStatus=function(){return this.gdirections.getStatus()};GReverseGeocoder.prototype.handleError=function(){GEvent.trigger(this,"error",this.lastpoint)};GReverseGeocoder.prototype.processDirection=function(){var c=this;if(this.gdirections.getPolyline()!=null){this.closestonroad=this.gdirections.getPolyline().getVertex(0)}if(this.gdirections.getNumRoutes()!=0){var e=this.getStreet(this.gdirections.getGeocode(0).address);var a=new GLatLng(Number(this.lastpoint.lat())-this.bounds,Number(this.lastpoint.lng())-this.bounds);var d=new GLatLng(Number(this.lastpoint.lat())+this.bounds,Number(this.lastpoint.lng())+this.bounds);var b=new GLatLngBounds(a,d);this.geocoder.setViewport(b);this.geocoder.getLocations(e,function(f){var g=c.getBestMatchingPlacemark(f);if(g!=null){if(c.experimental){c.ad=g.address;c.step=10;c.start=1;c.houseNumberSearch()}else{GEvent.trigger(c,"load",g)}}else{c.handleError()}})}};GReverseGeocoder.prototype.getBestMatchingPlacemark=function(d){if(!d||d.Status.code!=200){return null}var e=-1;var g=1000000;for(var f=0;f<d.Placemark.length;f++){var b=d.Placemark[f];var a=new GLatLng(b.Point.coordinates[1],b.Point.coordinates[0]);var c=this.lastpoint.distanceFrom(a);if(c<g){e=f;g=c}}if(e<0){return null}d.Placemark[e].RequestPoint={coordinates:[this.lastpoint.lng(),this.lastpoint.lat()]};d.Placemark[e].PointOnRoad={coordinates:[this.closestonroad.lng(),this.closestonroad.lat()]};d.Placemark[e].Distance=g;d.Placemark[e].DistanceOnRoad=this.closestonroad.distanceFrom(new GLatLng(d.Placemark[e].Point.coordinates[1],d.Placemark[e].Point.coordinates[0]));return d.Placemark[e]};GReverseGeocoder.prototype.processDirectionRefine=function(){var g=this.gdirectionsrefine.getNumGeocodes();var b=-1;var d=100;for(var c=1;c<g;c++){var a=this.gdirectionsrefine.getGeocode(c);var h=new GLatLng(a.Point.coordinates[1],a.Point.coordinates[0]);if(a.AddressDetails.Accuracy==8){var k=this.lastpoint.distanceFrom(h);if(k<d){b=c;d=k}}}if(b<0){if(this.start+(24*this.step)<2000){this.start=this.start+(25*this.step);this.houseNumberSearch()}else{this.handleError()}}else{if(this.step==1){var e=this.gdirectionsrefine.getGeocode(b);e.RequestPoint={coordinates:[this.lastpoint.lng(),this.lastpoint.lat()]};e.PointOnRoad={coordinates:[this.closestonroad.lng(),this.closestonroad.lat()]};e.Distance=d;e.DistanceOnRoad=this.closestonroad.distanceFrom(new GLatLng(e.Point.coordinates[1],e.Point.coordinates[0]));GEvent.trigger(this,"load",e)}else{var a=this.gdirectionsrefine.getGeocode(b);var f=this.start+(b*this.step);this.start=f-10;this.step=1;this.houseNumberSearch()}}};GReverseGeocoder.prototype.houseNumberSearch=function(){this.gdirectionsrefine.clear();this.gdirectionsrefine.loadFromWaypoints([(""+(this.start+(0*this.step))+" ")+this.ad,(""+(this.start+(1*this.step))+" ")+this.ad,(""+(this.start+(2*this.step))+" ")+this.ad,(""+(this.start+(3*this.step))+" ")+this.ad,(""+(this.start+(4*this.step))+" ")+this.ad,(""+(this.start+(5*this.step))+" ")+this.ad,(""+(this.start+(6*this.step))+" ")+this.ad,(""+(this.start+(7*this.step))+" ")+this.ad,(""+(this.start+(8*this.step))+" ")+this.ad,(""+(this.start+(9*this.step))+" ")+this.ad,(""+(this.start+(10*this.step))+" ")+this.ad,(""+(this.start+(11*this.step))+" ")+this.ad,(""+(this.start+(12*this.step))+" ")+this.ad,(""+(this.start+(13*this.step))+" ")+this.ad,(""+(this.start+(14*this.step))+" ")+this.ad,(""+(this.start+(15*this.step))+" ")+this.ad,(""+(this.start+(16*this.step))+" ")+this.ad,(""+(this.start+(17*this.step))+" ")+this.ad,(""+(this.start+(18*this.step))+" ")+this.ad,(""+(this.start+(19*this.step))+" ")+this.ad,(""+(this.start+(20*this.step))+" ")+this.ad,(""+(this.start+(21*this.step))+" ")+this.ad,(""+(this.start+(22*this.step))+" ")+this.ad,(""+(this.start+(23*this.step))+" ")+this.ad,(""+(this.start+(24*this.step))+" ")+this.ad],{getSteps:true,locale:"GB"})};GReverseGeocoder.prototype.getStreet=function(a){if(a==null){return null}if((a!=null)&&(a.indexOf("/")>0)){a=a.split("/");if(isNaN(a[0].charAt(1))){a=a[0]}else{a=a[1]}}return a};GReverseGeocoder.prototype.setExperimentalHouseNumber=function(a){this.experimental=a};GReverseGeocoder.prototype.getPlacemarkProperty=function(a,b){for(var d in a){if((d==b)){return String(a[d])}else{if(typeof(a[d])=="object"){var c=this.getPlacemarkProperty(a[d],b);if(c!=null){return c}}}}return null};
