var map;
var directions;
var mode;

var trips = new Array();

function selectDestinationFavorite(what){
	if ($("#gtfavorite" + what).val() != 'title'){
		$("#gt" + what).val($("#gtfavorite" + what).val());
		$("#gtfavorite" + what).val('title');
	}
}


function showRoute() {

	$('#wtaddtolist').attr('disabled','disabled');

	map = new GMap2(document.getElementById("wtmap"));
	map.setMapType(G_SATELLITE_MAP);
	map.setCenter(new GLatLng(45.76985796144351, 4.824886322021484), 12);
	directions = new GDirections(map, document.getElementById('steps'));
	
	mode = $(".gtmode:checked").val();


	var travelmode = G_TRAVEL_MODE_DRIVING;
	if (mode == 'velo' || mode == 'marche'){
		travelmode = G_TRAVEL_MODE_WALKING;
	}	
	
	directions.load("from: " + $("#gtdeparture").val() + " to: " + $("#gtdestination").val(), {travelMode:travelmode } );
      
     GEvent.addListener(directions, "load", function() {
     	var distance = directions.getDistance().meters;
     	var duration = directions.getDuration().seconds;
    	if (mode == 'velo') duration = duration / 3;
     	if (mode == 'scooter') duration = distance * 0.13;

		var freq = parseInt($("#wtfreq").val()) * 2 * $("#wtweeks").val();
		var cost = 0;
		var co2 = 0;
		
		if (mode == 'voiture'){
			cost = distance * freq * 0.5 / 1000;
			co2 = distance * freq * 0.32 / 1000;
		}
		if (mode == 'moto'){
			cost = distance * freq * 0.3 / 1000;
			co2 = distance * freq * 0.17 / 1000;
		}
		if (mode == 'scooter'){
			cost = distance * freq * 0.2 / 1000;
			co2 = distance * freq * 0.10 / 1000;
		}
		$("#wtmode").html(mode);
		
		$("#wtdistance").html(Math.round(distance * freq / 100)/10 + '&nbsp;km&nbsp;/&nbsp;an');
		$("#wtduration").html(Math.round(duration/60) + '&nbsp;minutes');
		$("#wtcost").html(Math.round(cost) + '&nbsp;eur&nbsp;/&nbsp;an');
		$("#wtco2").html(Math.round(co2) + ' kg&nbsp;éq.&nbsp;CO2&nbsp;/&nbsp;an');	
		
		setTimeout("map.zoomOut();", 1);
		$('#wtaddtolist').attr('disabled','');
   	});
     GEvent.addListener(directions, "error", function() {
		$('#wtresults').hide(); $('#wtdialog').show();
     	alert("Impossible de calculer ce trajet, entrez des adresses plus précises");
     });
}

function addTrip(){
	trip = {mode: $("#wtmode").html(), destination: $("#gtdestination").val(), distance: $("#wtdistance").html(), duration: $("#wtduration").html(), cost: $("#wtcost").html(), co2: $("#wtco2").html()};
	trips[trips.length] = trip;
	renderList();
}

function deleteTrip(id){
	if (confirm("Voulez vous supprimer ce trajet ?")){
		newTrips = new Array();
		var j = 0;
		for (i = 0; i < trips.length; i++){
			if (i != id){
				newTrips[j] = trips[i];
				j++;
			}
		}
		trips = newTrips;
		renderList();
	}
}

function renderList(){
	var totalCost = 0;
	var totalCo2 = 0;
	var html = '<tr><th>Destination</th><th>Mode</th><th class="wtnumeric">Durée</th><th class="wtnumeric">Distance</th><th class="wtnumeric">Coût</th><th class="wtnumeric">Emissions CO2</th><th></th></tr>';
	for(i = 0; i < trips.length; i++){
		//alert(parseFloat(trips[i].cost));
		totalCost += parseFloat(trips[i].cost);
		totalCo2 += parseFloat(trips[i].co2);
		html += '<tr><td>' + trips[i].destination + '</td><td>' + trips[i].mode + '</td><td class="wtnumeric">' + trips[i].duration + '</td><td class="wtnumeric">' + trips[i].distance + '</td><td class="wtnumeric">' + trips[i].cost + '</td><td class="wtnumeric">' + trips[i].co2 + '</td><td><img onclick="deleteTrip(' + i + ')" src="weeztrips/css/delete.png"/></td></tr>';
	}
	
	html += '<tr><td><strong>Total</strong></td><td></td><td class="wtnumeric"></td><td class="wtnumeric"></td><td class="wtnumeric"><strong>' + totalCost + '</strong>&nbsp;eur&nbsp;/&nbsp;an</td><td class="wtnumeric"><strong>' + totalCo2 + '</strong>&nbsp;kg&nbsp;éq.&nbsp;CO2&nbsp;/&nbsp;an</td><td></td></tr>';
	$("#wttrips").html(html);
	
	//alert(parseInt($("#co2logement").html()));
	
	$("#co2total").html(parseInt($("#co2logement").html()) + totalCo2);
	$("#co2totalinput").val(parseInt($("#co2logement").html()) + totalCo2);
	$("#co2trajets").val(totalCo2);
	$("#couttotal").html(parseInt($("#coutlogement").html()) + totalCost);
	$("#couttotalinput").val(parseInt($("#coutlogement").html()) + totalCost);
	$("#couttrajets").val(totalCost);
}
