var map = null;
var geocoder = null;
var markerOptions = null;
var directions = null;
var savedResult = null;

window.onload = function()
{
	if (GBrowserIsCompatible()) {
//		map = new GMap2(document.getElementById("dealerMap"));
		map = new GMap2($("#dealerMap").get(0));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
//		var marinIcon = new GIcon(G_DEFAULT_ICON);
//		marinIcon.image = "img/bearhead.png";
//		markerOptions = { icon:marinIcon };
//		directions = new GDirections(map, document.getElementById("dealerOutput"));
		directions = new GDirections(map, $("#dealerOutput").get(0));
//		showAddress("Online HQ", "Moosweg 8, 92318 Neumarkt, Germany");
	}
}

window.onunload = function()
{
	GUnload();
}

function showAddress(shop, address)
{
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert("Address not found!");
				} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point, markerOptions);
				map.addOverlay(marker);
				var htmlContent = "<p><b>" + shop + "</b></p><p>" + address + "</p>";
				htmlContent = htmlContent + '<hr /><p>Show directions:</p><form onsubmit="callDirections(\'' + address + '\'); return false;"><table><tr><td>From:</td><td><input type="text" size="23" name="origin" value="Place of departure" onfocus="this.value=\'\'" /></td><td><input type="submit" value="Go" /></tr></table></form>';
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(htmlContent);
				});
				marker.openInfoWindowHtml(htmlContent);
			}
		}
		);
	}
}

function callDirections(dest)
{
	var from = $('input[@name="origin"]').val();
	if(from) {
		showDirection("from: " + from + " to: " + dest);
	}
}

function restoreContent()
{
	if(savedResult)
		$("#dealerOutput").html(savedResult);
	savedResult = null;
}

function showDirection(path)
{
	if(savedResult == null)
		savedResult = $("#dealerOutput").html();;
	$("#dealerOutput").html('<p>&laquo; <a href="javascript:restoreContent();void(null);">Back</a></p>');
	directions.load(path, { "locale": "de" });
}
