var GeoCoder = Class.create({
	
	initialize: function(map){
		
		this.map = map;
		
		this.googleGC = new GClientGeocoder();
		GeoCoder.mapGeoName = (function(response){
		
			this.mapGeoName(response);	
			
		}).bind(this);
	},
	
	find: function(address){
	
		this.googleGC.getLocations(address, (function(response){
			
			if (response.Status.code == G_GEO_SUCCESS){
	
				var coords 	= response.Placemark[0].Point.coordinates;
				var zoom 	= response.Placemark[0].AddressDetails.Accuracy + 9;
				var point 	= new GLatLng(coords[1], coords[0]);
	
				this.map.setCenter(point, zoom);
	
			} else {
	
				switch (response.Status.code){
	
					case G_GEO_MISSING_ADDRESS:
						break;
	
					case G_GEO_SERVER_ERROR:
					case G_GEO_UNKNOWN_ADDRESS:
					case G_GEO_UNAVAILABLE_ADDRESS:
						
						var request = 'http://ws.geonames.org/searchJSON?q=' + encodeURIComponent(address)  + '&maxRows=1&callback=GeoCoder.mapGeoName';
			
						var jsr = new JSONscriptRequest(request);
						jsr.buildScriptTag();
						jsr.addScriptTag();
						
						break;
	
					case G_GEO_BAD_KEY:
						alert("Sorry, the Google API key won't work for this page!");
						break;
	
					default:
						alert("An unknown error (" + response.Status.code + ") occurred!");
				}	
			}
			
		}).bind(this));	
	},
	
	mapGeoName: function(response){
		
		try {

			var place = response.geonames[0];
			
			Logger.log(place);	
			var point = new GLatLng(place.lat, place.lng);

			this.map.setCenter(point, 12);	
			
		} catch(e) {
			
			var address = alert("Sorry, this address is unavailable! \nRefine the place name you are looking for!");
		}		
	}
});
