google.setOnLoadCallback(showMap);
var directions;
var map;
var gLatLng;
function showMap()
{
	map = new google.maps.Map2(document.getElementById("gmap"));
	
	createDirections();
	
	gLatLng = new GLatLng(gLat, gLng);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(gLatLng, 15);
	var marker = new GMarker(gLatLng);
	map.addOverlay(marker);
	//marker.openInfoWindowHtml($("gaddress").innerHTML);		
	GEvent.addListener(marker, "click", function()
	{
		marker.openInfoWindowHtml($("gaddress").innerHTML);
	});
	
	directions = new GDirections(map, $("gdirections"));
    GEvent.addListener(directions, "error", handleErrors);
	document.body.onunload = GUnload;
	
	function createDirections()
	{
		
	}
}

window.onload = showMap;

function setDirections(fromAddress)
{
	map.clearOverlays();
	directions.load("from: " + fromAddress + " to: " + gLatLng.toUrlValue(),{ "locale": "pl" });
	return false;
}
				    
function handleErrors()
{
	if (directions.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: " + directions.getStatus().code);
	else if (directions.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: " + directions.getStatus().code);
	else if (directions.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: " + directions.getStatus().code);
	else if (directions.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: " + directions.getStatus().code);
	else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
		alert("Adres nie możę być poprawnie przetworzony.\n Kod błęu: " + directions.getStatus().code);
	else alert("Nieznany bład");
}
