function find_place() {
	var n = $('find_place_q');
	var place = n.value;

	if(place.length == 0)
		return;

	display('find_place_res', '');
	display('find_place_none', 'none');
	display('find_place_results', 'none');
	display('find_place_close', 'none');
	display('find_place_loading', '');

	var a = new AJAX('geo.findplace', function(response, data) {
		display('find_place_loading', 'none');

		display_places(response.places);

		return;
	}, null);
	a.setParam('p', place);
	a.send();
}

function goto_place(p) {
	close_places();
	var place = _places[p];

	map.topinMap.setCenter(new GLatLng(place.lat, place.lng), 12);
}

function display_places(places) {
	var n = $('find_place_results');

	if(places == null) {
		display('find_place_close', '');
		display('find_place_none', '');
		return;
	}

	if(places.totalResultsCount == 0) {
		display('find_place_close', '');
		display('find_place_none', '');
		return;
	}

	clear(n);
	display('find_place_close', '');
	display(n, '');

	_places = [];

	var text = '';
	var link = null;
	var place = null;
	for(var i = 0; i < places.geonames.length; i++) {
		place = places.geonames[i];

		text = place.name + ' (';
		if(place.adminName1)
			text = text + place.adminName1 + ', ';
		text = text + place.countryName + ')';

		link = $Link('javascript: goto_place(' + _places.length + ')', text);

		n.appendChild(link);
		n.appendChild($Element('br'));

		_places.push(place);
	}

	if(_places.length == 1)
		goto_place(0);
}

function close_places() {
	display('find_place_res', 'none');
}