var locationSpecified;
var search_latlng;
var latLngBox;

function locationSearch(locationString,initmap) {
	locationSpecified = 0;
	var geocoder = new GClientGeocoder();
	
	geocoder.getLocations(
	locationString,
	function(location){
		 //if this address is found
		 if(location.Status.code == 200) {
		    var placeMark = location.Placemark[0];
		    latLngBox = placeMark.ExtendedData.LatLonBox;
		    
		    search_latlng = new GLatLng(placeMark.Point.coordinates[1], placeMark.Point.coordinates[0]);
		    locationSpecified = 1;
		    if (initmap == 1) {
			    onLoad();
		    }
		    else {
			    centerOnSearch(search_latlng,latLngBox);
			    return false;
		    }
		 }
		 else {
			 alert('Location not found : try a more specific location using a URL like :\n\nhttp://forage.rs/location/country/region/town');
			 onLoad();
		 }
		}
	);
}

function centerOnSearch(latlng,latLngBox) {
	if(latLngBox) {
		//determine zoom level if possible
		latLngBound = new GLatLngBounds(new GLatLng(latLngBox.south, latLngBox.west), new GLatLng(latLngBox.north, latLngBox.east));
		zoom = map.getBoundsZoomLevel(latLngBound);
	}
	else {
		zoom = 5;
	}
	
	if (map.getCurrentMapType().getName()=="Terrain" && zoom<G_PHYSICAL_MAP.getMaximumResolution()) {
		
		map.setCenter(latlng,zoom, G_PHYSICAL_MAP);
	}
	else if (map.getCurrentMapType().getName()=="Terrain" && zoom>=G_PHYSICAL_MAP.getMaximumResolution()) {
		map.setCenter(latlng,zoom, G_HYBRID_MAP);
	}
	else {
		map.setCenter(latlng,zoom);
	}
}
