    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize() {
		if(document.getElementById('local_gmap')){
		  if (GBrowserIsCompatible()) {      
			map = new GMap2(document.getElementById("map_canvas"));
			gdir = new GDirections(map, document.getElementById("directions"));
			geocoder = new GClientGeocoder();

			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			GEvent.addListener(gdir, "error", handleErrors);
			inicia_mapa();
			
			//setDirections("-19.930236,-43.921645", "-19.930236,-43.921645", "pt_BR");
		  }
	  	}
	}
    
	// Ícone
	 var marca = new GIcon();
      marca.image = "imgs/gmaps.png";
      marca.shadow = "imgs/gmaps_shadow.png";
      marca.iconSize = new GSize(16, 16);
      marca.shadowSize = new GSize(32, 16);
      marca.iconAnchor = new GPoint(9, 34);
      marca.infoWindowAnchor = new GPoint(9, 2);
      marca.infoShadowAnchor = new GPoint(18, 25);
      marca.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
      marca.printImage = "imgs/imgs/gmaps.png";
      marca.mozPrintImage = "imgs/gmaps_shadow.png";

	
	function inicia_mapa(){
		// Pega valores do Oito
		var local = document.getElementById('local_gmap').value;
		var html = document.getElementById('html_gmap').value;
		var point = new GLatLng(-19.96992,-43.95866);
			map.setCenter(point, 16);
		var marker = new GMarker(point,marca);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(html);
		map.addControl(new GSmallMapControl());
	}
	
    function setDirections(fromAddress, toAddress, locale) {
		//document.getElementById("trajeto").style.display = 'block';
		//document.getElementById("validador").style.display = 'none';

		gdir.load("from: " + fromAddress + " to: " + toAddress,
        	        { "locale": locale });
	}

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     mensagem("O endereço informado nao foi encontrado, talvez ele ainda nao tenha sido mapeado pelo GoogleMaps");
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     mensagem("As coordenadas informadas nao foram encontradas, certifique-se de ter escrito no padrao");
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     mensagem("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)
	     mensagem("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)
	     mensagem("O caminho solicitado n&atilde;o pode ser construído, verifique se informou corretamente seu endereço");
	    
	   else mensagem("Erro desconhecido");
	   
	}

	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...
	}
	
	function mensagem(msg){
		document.getElementById("validador_msg").innerHTML = msg;
		document.getElementById("validador").style.display = 'block';
	}
