function showAddress(address) {
	// Get map point from street address and call map init
	if (GBrowserIsCompatible()) {
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address.replace(/<br>/gi, ' '), function(point) {
			if (point) {
				return initMap(point);
			} else {
				// Cycle back through address to find nearest working location.
				arr = address.split(/<br>/gi);
				while (arr.shift() == "");
				
				if (arr.length > 0) 
					showAddress(arr.join("<br>"));
			}
		});
	}else{
		return false;
	}
	
}
function showLatLong(latitude, longitude) {
	if (latitude.length > 1 && longitude.length > 1 && GBrowserIsCompatible()) {
		// Create point from lat/long
		var point = new GLatLng(latitude, longitude);
		
		if(point)
			return initMap(point);
		else
			return false;
	}else{
		return false;
	}
}
function initMap(point) {
	// Create map object
	var map = new GMap2(document.getElementById("map_canvas"));
	
	zoomend = GEvent.bind(map, "zoomend", this, function() {
		setTimeout(function() { afterZoom(map, zoomend) }, 1000);
	});
	
	// Init map
	map.setCenter(point, 15);
	var marker = new GMarker(point);
	map.addOverlay(marker);
	map.addControl(new GSmallZoomControl());
	
	return true;
}

// If the map doesn't have images for the current zoom level,
// zoom back out a couple of notches.
function afterZoom(map, listener) {
  var zoom_needed = false
  $("#map_canvas p").each(function(i, p) {
    if ($(p).text() == "Try zooming out for a broader look.") zoom_needed = true;
  });
  
  if (zoom_needed) {
    map.setZoom(map.getZoom() - 2);
  } else {
    GEvent.removeListener(listener);
  }
}

$(function() {
	// Show lat/long from markup
	if(!showLatLong($("#lat").text(), $("#long").text())){
		// If no long/lat, show address from markup
		showAddress($("address").html() + " Australia");
	}
	
});