/**************************************************************************************/
/** El array destinos está inicializado en latitudLongitudAeropuertoEn.js (o Es o It)**/
/**************************************************************************************/

  function load(latitud,longitud) {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
		   map.addControl(new GOverviewMapControl());
        map.setCenter(new GLatLng(latitud,longitud), 5);

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.shadow = "/img/elementos/markers/shadow.png";
        baseIcon.iconSize = new GSize(32, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(16, 34);
        baseIcon.infoWindowAnchor = new GPoint(16, 3);
        baseIcon.infoShadowAnchor = new GPoint(0, 0);

        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
        function createMarker(point, index) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
			 var destinosHTML = destinos[i][3];
          var icon = new GIcon(baseIcon);
          icon.image = "/img/elementos/markers/" + destinos[i][0] + ".png";
          var marker = new GMarker(point, icon);

          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(destinosHTML);
          });
          return marker;
        }

        // Add 10 markers to the map at random locations
          for (var i = 0; i < destinos.length; i++) {
	          var point = new GLatLng(destinos[i][1],destinos[i][2]);
          map.addOverlay(createMarker(point, i));
        }
      }
    }


	 function pintarMapaAeropuertosCercanos(aeropuertosCercanos,maximaDistancia){

	// metemos en array latitudes las latitudes del array aeropuertosCercanos.
	// con sort ordenamos de menor a mayor
	// restamos la latitud mas alta, con la mas baja. lo dividimos entre dos y lo sumamos a la latitud mas corta
	// asi conseguimos el centro.
	var latitud =[];
	for (i=0;i<aeropuertosCercanos.length; i++) {latitud.push(aeropuertosCercanos[i][1]);}

	latitud.sort();
	var distanciaLatitud = (eval("latitud[" + (latitud.length-1) +"];") - (latitud[0]));
	var centroLatitud = (distanciaLatitud/2)+latitud[0];

	// lo mismo con longitud
	var longitud =[];
	for (i=0;i<aeropuertosCercanos.length; i++) {longitud.push(aeropuertosCercanos[i][2]);}
	longitud.sort();
	var distanciaLongitud = (eval("longitud[" + (longitud.length-1) +"];") - (longitud[0]));
	var centroLongitud = (distanciaLongitud/2)+longitud[0];

	if(maximaDistancia<10){
		zoom = 12;
	}else if(maximaDistancia<40){
		zoom = 10;
	}else if(maximaDistancia<80){
		zoom = 9;
	}else if(maximaDistancia<120){
		zoom = 8;
	}else if(maximaDistancia<140){
		zoom = 8;
	}else if(maximaDistancia<200){
		zoom = 7;
	}else{
		zoom = 6;
	}
	if (GBrowserIsCompatible()) {
	   var map = new GMap2(document.getElementById('map'));
      map.addControl(new GSmallMapControl());
		 map.addControl(new GOverviewMapControl());
		map.setCenter(new GLatLng(centroLatitud,centroLongitud), zoom);

		// Creamos los iconos nuevos, sombra, etc.
		var baseIcon = new GIcon();
		baseIcon.shadow = "/img/elementos/markers/shadow.png";
		baseIcon.iconSize = new GSize(32, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(16, 34);
		baseIcon.infoWindowAnchor = new GPoint(16, 3);
		baseIcon.infoShadowAnchor = new GPoint(0, 0);

		// array para el cambio de colores
		var colores = new Array ("#000000","#000066","#666600","#006600","#0000ff","#006666","#660000"," #ff0000");

		 function createMarker(point, index) {
          // Create a lettered icon for this point using our icon class
			 var destinosHTML = aeropuertosCercanos[i][3];
          var icon = new GIcon(baseIcon);
          icon.image = "/img/elementos/markers/" + aeropuertosCercanos[i][0] + ".png";
          var marker = new GMarker(point, icon);
          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(destinosHTML);
          });
          return marker;
        }




		// Añadimos los markers con las coordenadas que vienen del array aeropuertosCercanos.
			for (var i = 1; i < aeropuertosCercanos.length; i++) {
				var point = new GLatLng(aeropuertosCercanos[i][1],aeropuertosCercanos[i][2]);
				map.addOverlay(createMarker(point, i));


			// aprovechamos el bucle para hacer las lineas
			eval("linea" + i + " = [new GLatLng(aeropuertosCercanos[0][1],aeropuertosCercanos[0][2]),new GLatLng(aeropuertosCercanos[i][1],aeropuertosCercanos[i][2])];");
			map.addOverlay(new GPolyline(eval("linea" + i),colores[i],4,0.8));
		}

		// añadimos el icono del destino
		var baseIcon2 = new GIcon();
		baseIcon2.iconSize = new GSize(23, 23);
		baseIcon2.iconAnchor = new GPoint(12,12);
		baseIcon2.infoWindowAnchor = new GPoint(16, 3);
		baseIcon2.infoShadowAnchor = new GPoint(0, 0);
		var destinoFinal = aeropuertosCercanos[0][3];
		var point2 = new GLatLng(aeropuertosCercanos[0][1],aeropuertosCercanos[0][2]);
		var icon2 = new GIcon(baseIcon2);
		icon2.image = "/img/elementos/markers/point.png";
		var marker2 = new GMarker(point2, icon2);
		map.addOverlay(marker2);
		GEvent.addListener(marker2, "click", function() {
            marker2.openInfoWindowHtml(destinoFinal);
          });



	    }

    // display a warning if the browser was not compatible
    else {
      alert(tradErrorIncompatibilidadMapasGoogle);
    }
}