/* <![CDATA[ */
  	var map;
	var directionsPanel;
	var directions;


    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas2"));
		directionsPanel = document.getElementById("textual_div");
		
		directions = new GDirections(map);
		directions.load("from: " + oriLatitude + "," + oriLongitude + " to: " + desLatitude + "," + desLongitude, {getSteps:!phpRouteLoad});

		// Gestion de eventos - Carga
		GEvent.addListener(directions, "load", function() {

			if (!phpRouteLoad){
				// Definimos valores de entrada
				origen = (document.getElementById('rutaOrigen').value!='') ? document.getElementById('rutaOrigen').value : oriName;

				destino = (document.getElementById('rutaDestino').value!='') ? document.getElementById('rutaDestino').value : desName;



				ruta = directions.getRoute(0);
				numPasos = ruta.getNumSteps();
				distancia = formateaDistancia(ruta.getDistance().meters);
				duracion = formateaDuracion(ruta.getDuration().seconds);

					


				// Comenzamos a construir salida html
				myHtml = '<table class="gmapTable1">';
				myHtml += '	<tr>';
				myHtml += '		<td class="inicioRuta">';
				myHtml += '			' + origen + ' -> ' + destino ;
				myHtml += '		</td>';
				myHtml += '	</tr>';
				myHtml += '	<tr>';
				myHtml += '		<td class="inicioRutaDistancia">';
				myHtml += '			' + label_distancia + ': ' + distancia + ' (' + label_duracion_aprox + ': ' + duracion + ')';
				myHtml += '		</td>';
				myHtml += '</tr>';
				myHtml += '	<tr>';
				myHtml += '		<td class="RutaStart">';
				myHtml += '			' + label_salida + ': ' + origen;
				myHtml += '		</td>';
				myHtml += '</tr>';
				myHtml += '</table>';


				myHtml += '<table class="gmapTable2">';
				for (i=0; i<numPasos; i++){
					paso = ruta.getStep(i);
					texto = paso.getDescriptionHtml();
					distancia = formateaDistancia(paso.getDistance().meters);
					duracion = formateaDuracion(paso.getDuration().seconds);
					
					
					myHtml += '	<tr>';
					myHtml += '		<td class="textoPaso">';
					myHtml += '			' + (i+1) + '- ' + texto;
					myHtml += '		</td>';
					myHtml += '		<td class="distanciaPaso">';
					myHtml += '			' +  distancia;
					myHtml += '		</td>';
					myHtml += '		<td class="duracionPaso">';
					myHtml += '			' +  duracion;
					myHtml += '		</td>';
					myHtml += '	</tr>';
					
				}
				myHtml += '</table>';

				// Final de ruta
				myHtml += '<table class="gmapTable1">';
				myHtml += '	<tr>';
				myHtml += '		<td class="RutaStop">';
				myHtml += '			' + label_llegada + ': ' + destino;
				myHtml += '		</td>';
				myHtml += '</tr>';
				myHtml += '</table>';
				myHtml += '</table>';


				document.getElementById('textual_div').innerHTML = myHtml;
			}
         });


		 // Gestion de errores
		 GEvent.addListener(directions, "error", function() {
			 alert ('No se ha podido calcular la ruta. Por favor, revise las direcciones');
		  });

      }
    }

	function formateaDistancia(dist){
		distancia = dist / 100;
		distancia = Math.round(distancia) / 10;
		if (distancia==0) return dist + ' m.';
		return distancia + ' km.';
	}

	function formateaDuracion(dur){
		duracion = dur / 60;
		duracion = Math.round(duracion);
		if (duracion==0) return dur + ' seg.';
		return duracion + ' min.';
	}

	function recalcula(){
		// Definimos valores de entrada
		origen = document.getElementById('rutaOrigen').value;
		destino = document.getElementById('rutaDestino').value;

		phpRouteLoad = false;

		directions.load("from: " + origen + " to: " +  destino, {getSteps:true});
	}

	
/* ]]> */
