// JavaScript Document
var conteudoGMapas = "";
conteudoGMapas += '<div id="modulo_gmaps">';
conteudoGMapas += '<a class="close" href="javascript: fechaBox();"><img src="modulos/google_maps/img/btn_janela_close.png" style="width:auto; height:auto" /></a>';

conteudoGMapas += '<iframe name="detGoogleMaps" id="detGoogleMaps" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="modulos/google_maps/detGoogleMaps.php" width="480" height="410" style="overflow-x: hidden; margin-bottom:10px;"></iframe>';
conteudoGMapas += '</iframe>';

//conteudoGMapas += '<strong class="name">Nome Empresa</strong> - ';
//conteudoGMapas += '<a style="color:#196F30;" href="http://maps.google.com.br/maps?f=q&source=s_q&hl=pt-BR&geocode=&q=&sll=<?=$latitude?>,<?=$longitude?>&sspn=&ie=UTF8&ei=&sig2=&cd=&ll=-29.675618,-51.092927&spn=0.009471,0.019205&z=16&iwloc=r1" target="_blank">Como chegar</a><br />';
conteudoGMapas += '<span class="street">%GTR_DESLOGRADOURO%</span><br />';
conteudoGMapas += '<span class="city_state">%GTR_DESBAIRRO%, %GTR_DESCIDADE% - %GTR_DESESTADO%</span>, Cep: <span class="zipcode">%GTR_DESCEP%</span><br />';
conteudoGMapas += '<span class="phone">%GTR_DESCEP%</span>';

conteudoGMapas += '</div>';

var map;
var directionDisplay;
var directionsService;
var geocoder;

function initialize(latitude, longitude) {
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsService = new google.maps.DirectionsService();
	geocoder = new google.maps.Geocoder();
	
	var latlng = new google.maps.LatLng(latitude, longitude);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(document.getElementById("map_directions"));
	
	var marker1 = new google.maps.Marker({
		position: latlng, 
		map: map
	});
}

function setDirections(latitude,longitude) {	
	var from = document.getElementById('from').value;
	var to = new google.maps.LatLng(latitude, longitude);
	
	if (geocoder) {
		geocoder.geocode({'address': from}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var request = {
					origin: results[0].geometry.location, 
					destination:to,
					travelMode: google.maps.DirectionsTravelMode.DRIVING
				};
				directionsService.route(request, function(result, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(result);
					}
				});
			} else {
				alert(from + " não encontrado");
			}
		});
	}
}

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(){ 
}
