//<![CDATA[

    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function load() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));

	map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		geocoder = new GClientGeocoder();
        setDirections("Praceta da Beira, 4", "Oeiras", "2780-004", "Praceta da Beira, 4", "Oeiras", "2780-004", "pt_PT");
      }
    }
    
    function setDirections(ruaOrigem, cidadeOrigem, codigoPostalOrigem, ruaDestino, cidadeDestino, codigoPostalDestino, locale) {
      
      gdir.load("from: " + ruaOrigem + ", "+ cidadeOrigem + ", " + codigoPostalOrigem + " to: " + ruaDestino + ", "+ cidadeDestino + ", " + codigoPostalDestino,
                { "locale": locale });
      
       var moradaDestino = ruaDestino + ", "+ cidadeDestino + ", " + codigoPostalDestino;
       var moradaOrigem = ruaOrigem + ", "+ cidadeOrigem + ", " + codigoPostalOrigem;       
       showAddress(moradaDestino, moradaOrigem);
    }

    function showAddress(addressDestino, addressOrigem) {
      if (geocoder) {
        geocoder.getLatLng(
          addressDestino,
          function(point) {
            if (!point) {
              alert(addressDestino + " not found" + point);
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point);
              
	      if (addressDestino == addressOrigem){
	      
		map.addOverlay(marker);
              marker.openInfoWindowHtml("<b>Vetoeiras</b><br>Praceta da Beira, 4-A Loja<br>2780-004 Oeiras - Portugal");
	      
	      }else{
	      
	      
	      GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<b>Vetoeiras</b><br>Praceta da Beira, 4-A Loja<br>2780-004 Oeiras - Portugal");
		});
		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.");
	   
	}

	function onGDirectionsLoad(){ 
        
	  	
	  //alert ("aqui");
	  // Use this function to access information about the latest load()
          // results.

          // e.g.
	  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}


    //]]>
