/**
 * @author J. Benito (benitowm@hotmail.com) 
 * @copyright 2009
 */

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

function addMarcador(lat,longi,marcador) {
   // Limpando os overlays
	
	if(map == ""){
		if (GBrowserIsCompatible()){
		    map = new GMap2(document.getElementById("map"));
		    map.addControl(new GSmallZoomControl);
		    map.setCenter(new GLatLng(lat,longi), 16);
		}
	}
   
	var dadosMarcador = marcador.split("+");
	var marcador = dadosMarcador[0];
	var width = dadosMarcador[1];
	var height = dadosMarcador[2];
	
    var tinyIcon = new GIcon();
    tinyIcon.image = marcador;
    tinyIcon.iconSize = new GSize(width,height);
    tinyIcon.iconAnchor = new GPoint(width/2,height);
    tinyIcon.infoWindowAnchor = new GPoint(width,1);

    markerOptions = { icon:tinyIcon };
	var m = new GMarker(new GLatLng(lat,longi),markerOptions);
    map.addOverlay(m);
	
    GEvent.addListener(m, "click", function() {
         m.openInfoWindowHtml("Maps")
       });
}
function geraCords(endereco,marcador){
	
	var geocoder = new GClientGeocoder;
	//geocoder.getLatLng(('-0706.5754' ,'-03452.7717'), function(){});
	geocoder.getLocations(endereco, function(resposta){
		var novacod = resposta.Placemark[0].Point.coordinates;	
		addMarcador(novacod[1],novacod[0],marcador);
	});

}


   
function inicializar_gmaps() {
  if (GBrowserIsCompatible()) {
  	document.getElementById("direcoes").innerHTML = "";
    map = new GMap2(document.getElementById("map")); //Local onde o mapa gerado deve ficar
    map.addControl(new GSmallZoomControl);
    gdir = new GDirections(map, document.getElementById("direcoes")); //Local para ficar o "passo-a-passo" pra chegar ao destino
    GEvent.addListener(gdir, "error", gmaps_erros); //Define qual função vai manipular os erros retornados
  }
}
	
function gmaps_erros() {
   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("Um erro desconhecido aconteceu.");
  
}
    
function mapsPesquisa(minhaLocalizacao,irPara,marcador) {
	inicializar_gmaps();
	geraCords(irPara,marcador);
    //Responsavel por iniciar o carregamento dos mapas nos locais especificos
    gdir.load("from: " + minhaLocalizacao + " to: " + irPara);
}

function geraRota(destino,marcador){
	var seu_enedereco = document.getElementById("onde").value;
	mapsPesquisa(seu_enedereco,destino,marcador);
}
	
	
    
