var directions;
function carregarMapa() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('mapaGoogleCompartir'));

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());		
		
		directions = new GDirections(map);
		GEvent.addListener(directions, 'load', onDirectionsLoad); 
		directions.load('from: ' + from + ' to: ' + to , { 'locale': locale , 'getSteps': true });
	}
}
function carregarMapaMobi() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('mapaGoogleCompartir'));

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());		

		directions = new GDirections(map);
		directions.load('from: ' + from + ' to: ' + to , { 'locale': locale , 'getSteps': true });
	}
}
function carregarMapaPortada() {
	if (GBrowserIsCompatible()) {
		 map = new GMap2(document.getElementById("mapaGoogleCompartir"));
		 map.setCenter(new GLatLng(40.526327,-3.702393), 5);
		 map.addControl(new GSmallMapControl());
		 afegirCobrandeds();
	   }
}
function createMarker(point, icon, text) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml("<div style='font-family:Lucida Grande, Trebuchet MS, Verdana'>" + text + "</div>");
	});
	return marker;
}
function createMarkerMap(point, icon) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, 'click', function() {
		marker.showMapBlowup( {zoomLevel:16, mapType: G_NORMAL_MAP} );
	});
	return marker;
}
function carregarMapaBici(encodedPoints, encodedLevels) {
	

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		
		var encodedPolyline = new GPolyline.fromEncoded({
			color: "#3333cc",
			weight: 10,
			points: encodedPoints,
			levels: encodedLevels,
			zoomFactor: 32,
			numLevels: 4
		});
		var iconSortida = new GIcon();
		iconSortida.image = "http://www.compartir.com/imatges/dd-start.png";
		iconSortida.iconSize = new GSize(20, 34);
		iconSortida.iconAnchor = new GPoint(9, 34);
		iconSortida.infoWindowAnchor = new GPoint(5, 1);

		var iconArribada = new GIcon();
		iconArribada.image = "http://www.compartir.com/imatges/dd-end.png";
		iconArribada.iconSize = new GSize(20, 34);
		iconArribada.iconAnchor = new GPoint(9, 34);
		iconArribada.infoWindowAnchor = new GPoint(5, 1);
		
		map.setCenter(encodedPolyline.getBounds().getCenter(), map.getBoundsZoomLevel(encodedPolyline.getBounds()));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());       
		map.setMapType(G_HYBRID_MAP);
		map.addOverlay(encodedPolyline);
		map.addOverlay(createMarkerMap(new GLatLng (o_lat,o_lon), iconSortida));
		map.addOverlay(createMarkerMap(new GLatLng (d_lat,d_lon), iconArribada));
	  }
}

function arrodonir(Num) {
	var Places = 2; // The number of decimal places to round to
	if (Places > 0) {
	  if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
		 var Rounder = Math.pow(10, Places);
		 return Math.round(Num * Rounder) / Rounder;
	  }
	  else return Num;
   }
   else return Math.round(Num);
}

function onDirectionsLoad(directions)
{					
	document.getElementById('dadesViatge').style.display = 'block';
	
	var distance = directions.getDistance();
	var temps = directions.getDuration();

	
	//cost_economic = distancia * preu km
	var cost_economic = arrodonir( (distance.meters/1000) * 0.42 );

	
	//cost_ambiental = distancia * 7 litres/100km * 2.5 KG de CO2 per litre de gasolina
	
	if(combustible > 0 && combustible < 4 && cilindrada > 0 && cilindrada < 5 ){
		var emisiones = new Array(0.1513,0.1881,0.258,0.1979,0.1809,0.2139,0.2958,0.207,0.1262,0.1262,0.224,0.2042);
		var co2 = emisiones[4*parseInt(combustible)-5+parseInt(cilindrada)];
		var cost_ambiental = arrodonir( (distance.meters/1000) * co2 );
	}
	else{
		var cost_ambiental = arrodonir( (distance.meters/1000) * 0.07 * 2.5 );
	}
	
	document.getElementById('span-ViatgeDistancia').innerHTML = distance.html;						
	document.getElementById('span-ViatgeTemps').innerHTML = temps.html;																				
	document.getElementById('span-CostEconomic').innerHTML = cost_economic;												
	document.getElementById('span-CostAmbiental').innerHTML = cost_ambiental;
	
	// Estalvi econòmic per persona
	// document.getElementById('es2').innerHTML = arrodonir( ( (Cost total viatge * Correció per passatger extra) / Nombre passatgers) * (Nombre passatgers - 1) );
	document.getElementById('es2').innerHTML = arrodonir( ( ( cost_economic * 1.05 ) / 2 ) );
	document.getElementById('es3').innerHTML = arrodonir( ( ( cost_economic * 1.10 ) / 3 ) * 2 );
	document.getElementById('es4').innerHTML = arrodonir( ( ( cost_economic * 1.15 ) / 4 ) * 3 );
	document.getElementById('es5').innerHTML = arrodonir( ( ( cost_economic * 1.20 ) / 5 ) * 4 ); 
	// Estalvi ambiental per persona
	document.getElementById('esa2').innerHTML = arrodonir( ( ( cost_ambiental * 1.05 ) / 2 ) );
	document.getElementById('esa3').innerHTML = arrodonir( ( ( cost_ambiental * 1.10 ) / 3 ) * 2 );
	document.getElementById('esa4').innerHTML = arrodonir( ( ( cost_ambiental * 1.15 ) / 4 ) * 3 );
	document.getElementById('esa5').innerHTML = arrodonir( ( ( cost_ambiental * 1.20 ) / 5 ) * 4 );
	
	if(document.getElementById('ViatgeDistancia')) document.getElementById('ViatgeDistancia').value = distance.meters;
	if(document.getElementById('ViatgeTemps')) document.getElementById('ViatgeTemps').value = temps.seconds;
}

function addEvent(obj,eventType,fn,useCapture){
	if(obj.addEventListener){obj.addEventListener(eventType,fn,useCapture);return true;}else{if(obj.attachEvent){var r=obj.attachEvent("on"+eventType,fn);return r;}}
}
