//<![CDATA[

var map;
var geocoder;
var gdir;
var addressMarker;

function initialize() {
	map = new GMap2(document.getElementById("map_canvas"));
	var mapTypeControl = new GMapTypeControl();
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
	var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
	map.addControl(mapTypeControl, topRight);

	GEvent.addListener(map, "dblclick", function() {
	  map.removeControl(mapTypeControl);
	  map.addControl(new GMapTypeControl(), bottomRight);
	});

	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(-23.9342491,-46.3256907), 17); //-23.560322, -46.193246
	geocoder = new GClientGeocoder();
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);		
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert("Sorry, we were unable to geocode that address");
		
	} else {	
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		marker = new GMarker(point);
		map.addOverlay(marker);
	
		/*GEvent.addListener(marker, 'click',function() {
			marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country:</b> ' + place.AddressDetails.Country.CountryName);
		});*/
		
		marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country:</b> ' + place.AddressDetails.Country.CountryName);
	}
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(address) {
	geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
	document.forms[0].toAddress.value = address;
	showLocation(address);
}

//switchFromTo() is used to switch addresses From -> To or To -> From
function switchFromTo(){
	var t = document.getElementById("fromAddress").value;
	document.getElementById("fromAddress").value = document.getElementById("toAddress").value;
	document.getElementById("toAddress").value = t;
}

function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "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.");
}

function onGDirectionsLoad(){ 
// Use this function to access information about the latest load()
// results.

// e.g.
// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
// and yada yada yada...
}

window.onload = initialize;
window.onunload = GUnload;
//]]>
