/*
	Copyright 2008 Google Inc.
	Licensed under the Apache License, Version 2.0:
	http://www.apache.org/licenses/LICENSE-2.0
*/

	var map;
	var directionsService;
	var directionsDisplay;
	var geocoder = null;
	var addressMarker;

	function initialize(){

		directionsService = new google.maps.DirectionsService();
		directionsDisplay = new google.maps.DirectionsRenderer();
		directionsDisplay.setPanel(document.getElementById("directions"));

	}

	function initializeMap() {

		var myOptions = {
			// zoom: 10,
			mapTypeControl: false,
			mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
			navigationControl: true,
			navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scaleControl: true
		}

		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		directionsDisplay.setMap(map);

	}

	function setDirections(fromAddress, toAddress, mode) {

		travelMode = (mode == 'walking') ? google.maps.DirectionsTravelMode.WALKING : google.maps.DirectionsTravelMode.DRIVING;

		request = {
			origin: fromAddress,
			// destination:  toAddress,
			destination:  new google.maps.LatLng(coords['lat'], coords['lng']),
			travelMode: travelMode,
			// unitSystem: DirectionsUnitSystem,
			// waypoints[]: DirectionsWaypoint,
			// provideTripAlternatives: true,
			region: "Chile"
		}

		directionsService.route(request, function(response, status) {

			if (status == google.maps.DirectionsStatus.OK) {
				$('#where').hide().next().fadeIn('fast', function(){
							initializeMap();
							directionsDisplay.setDirections(response);
							$('#getdirections').removeClass('loading').find('p.tooltip').hide();
							$('a.icon.info').fadeIn('slow');
				});
		  } else {
				// $('#where').show().next().hide();
				alert("No pudimos encontrar la ruta entre los dos puntos. Intenta escribir tu dirección exacta, incluyendo el nombre de la comuna y la ciudad, por ejemplo: Av Los Leones 521, Providencia, Santiago.");
		  }

		});

	}

/*

	function handleErrors(){
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert('No pudimos encontrar la ruta entre los dos puntos. Intenta escribir tu dirección exacta, incluyendo el nombre de la comuna y la ciudad, por ejemplo: Av Los Leones 521, Providencia, Santiago.');
			// 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.");
	}

*/

