// JavaScript Document
var map = null;
var geocoder = null;

// INITALISE GOOGLE MAP
//--------------------------------------------------------------------------------->
function initialize() 
{
  if (GBrowserIsCompatible()) 
  {
	map = new GMap2(document.getElementById("map_canvas"));
	//map.setCenter(new GLatLng(-25.335448, 135.745076), 13);
	map.setCenter(new GLatLng(-27.467878615986918, 153.0298089981079), 15);
	geocoder = new GClientGeocoder();
	map.addControl(new GLargeMapControl());
	
	plotPoints();
  }
}

// SHOW INITAL ADDRESS OF REGION - GEOCODED LONG LAT
//--------------------------------------------------------------------------------->
function showAddress(address) 
{
  if (geocoder) 
  {
	geocoder.getLatLng(
	  address,
	  function(point) 
	  {
		if (!point) 
		{
		  alert(address + " not found");
		} 
		else 
		{
		  map.setCenter(point, 10);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml('Hastings Valley');
		  plotPoints();
		}
	  }
	);
  }
}

var gmarkers = [];
var htmls = [];

// handle clicks from the listing:
//--------------------------------------------------------------------------------->
function click(i){
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// set up a new marker
//--------------------------------------------------------------------------------->
function addMarker(lat, lon, html){
	var marker = new GMarker(new GLatLng(lat, lon));
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});
	gmarkers.push(marker);
	htmls.push(html);
	return marker;
}

// set up a new marker with auto geocode
//--------------------------------------------------------------------------------->
function addMarker2(latandlon, html)
{
	var marker = new GMarker(new GLatLng(latandlon));
	GEvent.addListener(marker, "click", function() 
	{
	  marker.openInfoWindowHtml(html);
	});
	gmarkers.push(marker);
	htmls.push(html);
	return marker;
}


// ALL THE POINTS TO MAP
//--------------------------------------------------------------------------------->
function plotPoints(){
		var marker;
		// add the points
		var html = formatHtml("Cha Cha Char Restaurant", "Shop 3 Plaza Level Eagle St Pier Brisbane, QLD 4000");
		marker = addMarker(-27.46787861598691, 153.0298089981079, html);
		map.addOverlay(marker);
}

// FORMATS THE RETURNED ADDRESS IN POPUP SPEECH BUBBLE
//--------------------------------------------------------------------------------->
function formatHtml(blurb, address) 
{
	return '<div class="blurb">' + blurb + '</div>\n<div class="address">' + address + '</div>';
}


