// (c)2007 Mapstraction Team - http://mapstraction.com

function MapstractionGeocoder(callback,api,error_callback){this.api=api;this.callback=callback;this.geocoders=new Object();if(error_callback==null){this.error_callback=this.geocode_error}else{this.error_callback=error_callback;}
this.svn_revision_string='$Revision: 107 $';this.addAPI(api);}
MapstractionGeocoder.prototype.addAPI=function(api){me=this;switch(api){case'google':this.geocoders[api]=new GClientGeocoder();break;case'mapquest':var proxyServerName="";var proxyServerPort="";var ProxyServerPath="mapquest_proxy/JSReqHandler.php";var serverName="geocode.access.mapquest.com";var serverPort="80";var serverPath="mq";this.geocoders[api]=new MQExec(serverName,serverPath,serverPort,proxyServerName,ProxyServerPath,proxyServerPort);break;default:alert(api+' not supported by mapstraction-geocoder');}}
MapstractionGeocoder.prototype.swap=function(api){if(this.api==api){return;}
this.api=api;if(this.geocoders[this.api]==undefined){this.addAPI($(element),api);}}
MapstractionGeocoder.prototype.geocode_error=function(response){alert("Sorry, we were unable to geocode that address");}
MapstractionGeocoder.prototype.geocode_callback=function(response,mapstraction_geocoder){var return_location=new Object();switch(mapstraction_geocoder.api){case'google':if(!response||response.Status.code!=200){mapstraction_geocoder.error_callback(response);}else{return_location.street="";return_location.locality="";return_location.region="";return_location.country="";place=response.Placemark[0];if(place.AddressDetails.Country.AdministrativeArea!=null){return_location.region=place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea!=null){if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality!=null){return_location.locality=place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare!=null)
return_location.street=place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;}}}
return_location.country=place.AddressDetails.Country.CountryNameCode;return_location.point=new LatLonPoint(place.Point.coordinates[1],place.Point.coordinates[0]);mapstraction_geocoder.callback(return_location);}
break;case'mapquest':break;}}
MapstractionGeocoder.prototype.geocode=function(address){var return_location=new Object();var mapstraction_geocoder=this;switch(this.api){case'google':if(address.address==null||address.address=="")
address.address=address.street+", "+address.locality+", "+address.region+", "+address.country
this.geocoders[this.api].getLocations(address.address,function(response){mapstraction_geocoder.geocode_callback(response,mapstraction_geocoder);});break;case'mapquest':var mqaddress=new MQAddress();var gaCollection=new MQLocationCollection("MQGeoAddress");mqaddress.setStreet(address.street);mqaddress.setCity(address.locality);mqaddress.setState(address.region);mqaddress.setPostalCode(address.postalcode);mqaddress.setCountry(address.country);this.geocoders[this.api].geocode(mqaddress,gaCollection);var geoAddr=gaCollection.get(0);var mqpoint=geoAddr.getMQLatLng();return_location.street=geoAddr.getStreet();return_location.locality=geoAddr.getCity();return_location.region=geoAddr.getState();return_location.country=geoAddr.getCountry();return_location.point=new LatLonPoint(mqpoint.getLatitude(),mqpoint.getLongitude());this.callback(return_location,this);break;default:alert(api+' not supported by mapstraction-geocoder');break;}}