//<![CDATA[

var map;
var gdir;
var geocoder = null;
var addressMarker;
var marker;

function goToMap(lat, lng, zoom, text){
    var map = itemMap;
    map.panTo(new GLatLng(lat, lng));
    map.openInfoWindow(new GLatLng(lat, lng), text);
}

// Creates a marker at the given point with the given label
function createMarker(point, label){
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function(){
        marker.openInfoWindowHtml(label);
    });
    return marker;
}

function setDirections(ruaOrigem, cidadeOrigem, codigoPostalOrigem, destino, locale){
	var map = itemMap;
	gdir = new GDirections(map, document.getElementById("directions"));

	gdir.load("from: " + ruaOrigem + ", " + cidadeOrigem + ", " + codigoPostalOrigem + " to: " + destino, {
        "locale": locale
    });

	GEvent.addListener(gdir, "error", handleErrors);
    var moradaDestino = destino;
    var moradaOrigem = ruaOrigem + ", " + cidadeOrigem + ", " + codigoPostalOrigem;

}


function loadMap(){

	if (GBrowserIsCompatible()) {
    	itemMap = new GMap2(document.getElementById("map"));

	    itemMap.addControl(new GSmallMapControl());
	    itemMap.addControl(new GMapTypeControl());

        gdir = new GDirections(map, document.getElementById("directions"));
    }
}
function showAddress(addressDestino, addressOrigem){
    alert("1");
    if (geocoder) {
        alert("2");
        geocoder.getLatLng(addressDestino, function(point){
            if (!point) {
                alert(addressDestino + " not found");
            }
            else {
                map.setCenter(point, 15);
                var marker = new GMarker(point);

                if (addressDestino == addressOrigem) {
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(" Campus de Campolide<BR>1099-032 LISBOA<BR><bR>Entrada 1 - Mesquita de Lisboa<BR>Entrada 2 - Rua de Campolide ");

                }
                else {
                    GEvent.addListener(marker, "click", function(){
                        marker.openInfoWindowHtml(" Campus de Campolide<BR>1099-032 LISBOA<BR><bR>Entrada 1 - Mesquita de Lisboa<BR>Entrada 2 - Rua de Campolide ");
                    });
                    map.addOverlay(marker);

                }

            }
        });
    }
}


function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("Não existe informação para o endereço pretendido.\nVerifique os dados inseridos e tente novamente.");
    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_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
            //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\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("Não existe informação para o endereço pretendido.\nVerifique os dados inseridos e tente novamente.");

    map.addOverlay(marker);

}
