//<![CDATA[

var countryCode = "br";

var reasons = [ ] ;
reasons[G_GEO_SUCCESS]            = "Success" ;
reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value." ;
reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address." ;
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons." ;
reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given" ;
reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded." ;
reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed." ;

var accuracy = [ ] ;
accuracy[0] = 'Unknown' ;
accuracy[1] = 'Country' ;
accuracy[2] = 'Region' ;
accuracy[3] = 'Sub-region' ;
accuracy[4] = 'Town' ;
accuracy[5] = 'Post code' ;
accuracy[6] = 'Street' ;
accuracy[7] = 'Intersection' ;
accuracy[8] = 'Address' ;  

var map = null;
var geocoder = null;
var overview = null;

function loadMap(id) {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById(id));
	geocoder = new GClientGeocoder();
	geocoder.setBaseCountryCode(countryCode);
	overview = new GOverviewMapControl();
	map.addControl(overview);
	map.addControl(new GSmallMapControl());
	return true;
  }
  return false;
}

function showAddress(address) {
	map.clearOverlays();
	overview.hide();
	geocoder.getLocations(address, 
		function (result) { 
			if (result.Status.code == G_GEO_SUCCESS) {
				var p = result.Placemark[0].Point.coordinates;
				//  show first location
				map.setCenter(new GLatLng(p[1],p[0]),15);
				for (var i = 0; i < result.Placemark.length; i++) {
					p = result.Placemark[i].Point.coordinates;
					var markerAddress = result.Placemark[i].address;
					var marker = new GMarker(new GLatLng(p[1],p[0]),{title:markerAddress});
					var html = htmlThis(markerAddress, marker.getPoint());
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(html);
					});
					map.addOverlay(marker);
					marker.openInfoWindowHtml(html);
				}
			} else {
				var reason="Código "+result.Status.code;
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				}
				alert('Não foi possível encontrar "' + address + '" ' + reason);
			}
		}
	);
}

function mapCenter(lat, lng) {
	map.closeInfoWindow();
	map.setCenter(new GLatLng(lat,lng));
}

function htmlThis(address, geoPoint) {
	var html = "<span class=MainText>";
	html += address;
	html += "<br><a href='javascript:selectLocation" + geoPoint + "'>Selecionar</a>";
	html += "</span>";
	return html;
}

function selectLocation(lat, lng) {
	document.getElementById("latlng").value = lat + ", " + lng;
}

function showLocation(name, address, lat, lng) {
	var info = {title:name};
	var html = "<span class=MainText>";
	html += "<strong>" + name + "</strong>";
	html += "<br>" + address;
	html += "</span>";
	var marker = new GMarker(new GLatLng(lat,lng), info);
	map.setCenter(new GLatLng(lat,lng), 15);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	map.clearOverlays();
	map.addOverlay(marker);
}

function addOverlay(info1, info2, lat, lng) {
	var info = {title:info1};
	var html = "<span class=MainText>";
	html += "<strong>" + info1 + "</strong>";
	html += "<br>" + info2;
	html += "</span>";
	var marker = new GMarker(new GLatLng(lat,lng), info);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	map.addOverlay(marker);
}

//]]>