// Add to <head>
/*******************************
	<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA_BN8ilmM_Mr0sCjnQtYkGRSbLdpN2bEMDIeFV7SDFRYqQPqRNBRQ4x91cGL7NQnGSKru3OyF95HRxw"></script>
	<script type="text/javascript" src="../common/googlemaps.js"></script>
/*******************************/

// Add to <body>
/*******************************
	<body onload="load(); showAddress();" onunload="GUnload()">
/*******************************/

// The Address Form
/*******************************
	<form action="#" onsubmit="adjustView(); setDirections(this.from.value); return false" class="noPrint">
		<input type="text" size="60" id="fromAddress" name="from" value="Enter your address to calculate your distance" />
		<input name="submit" type="submit" value="Get Directions!" />
	</form>
/*******************************/

// The Map and Directions divs
/*******************************
	<div id="map" style="width: 600px; height: 300px; margin-left:auto; margin-right:auto;"></div>
	<div id="directions" style="width: 500px;display:none; visibility:hidden;"> </div>
/*******************************/

var address = 'cnr old cleveland and tilley roads chandler qld';
var map;
var geocoder;
var gdir;
var addressMarker;

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"), {draggableCursor: 'default', draggingCursor: 'move'});
		//map.setCenter(new GLatLng(-28.054893,153.354637), 16);
		map.addControl(new GLargeMapControl() /*, new GControlPosition(G_ANCHOR_LEFT_RIGHT)*/);
		map.addControl(new GMapTypeControl() /*, new GControlPosition(G_ANCHOR_TOP_RIGHT)*/);
		map.setMapType(G_HYBRID_MAP);
		map.addControl(new GOverviewMapControl(new GSize(100,100)) /*, new GControlPosition(G_ANCHOR_BOTTOM_RIGHT)*/);
		//map.enableDoubleClickZoom();
		//keyboardhandler = new GKeyboardHandler(map);
		geocoder = new GClientGeocoder();
		gdir = new GDirections(map, document.getElementById("directions"));
		//GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		
		function createMarker(icon, point, html) {
			// Create a lettered icon for this point using our icon class
			var marker = new GMarker(point, icon);
		
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
				map.panTo(point);
			});
			
			GEvent.addListener(marker, "infowindowclose", function() {
				map.panTo(point);
			});
			
			return marker;
		}

		if (geocoder) {
			geocoder.getLatLng(
				address,
				function(center) {
					center = new GLatLng(-27.51280087881724,153.14798712730408);
					if (!center) {
						alert(address + " not found");
					} else {
	
						var aIcon = new GIcon();
						var aPoint = center;
						var aHtml = ("<table width=\"240\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><strong>Swimming Queensland</strong><br />VIP Entry Sports Road<br />Sleeman Sports Centre<br />Corner Old Cleveland &amp; Tilley Roads<br />Chandler Qld 4155<br />Australia<br />Ph: +617 3390 2011</td><td><img src=\"/media/images/logo-swim-qld-vert-small.png\"></td></tr></table>");
						//aIcon.iconSize = new GSize(88, 62);
						aIcon.iconAnchor = new GPoint(16,32);
						aIcon.infoWindowAnchor = new GPoint(20,0);
						aIcon.image = "http://maps.google.com/mapfiles/ms/micons/blue-dot.png";
	
						map.setCenter(center, 15);
						map.addOverlay(createMarker(aIcon, aPoint, aHtml));
	
					}
				}
			);
		}
		
	}
}


function adjustView() {
	document.getElementById('directions').style.display = 'block';
	document.getElementById('directions').style.visibility = 'visible';
}
function setDirections(fromAddress) {
	var locale = "en";
	//GEvent.addListener(gdir, "load", onGDirectionsLoad);
	gdir.load("from: " + fromAddress + " to: " + address, { "locale": locale });
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	} else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	} else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code); 
	} else if (gdir.getStatus().code == G_GEO_BAD_KEY) {
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	} else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	} else {
		alert("An unknown error occurred.");
	}
}