    
var marker_icon_saba = 'img/marker_icon_saba.png';  
var infowindow = new google.maps.InfoWindow();
var distances = new Object();
var marker, addressCor;
var markerArray = new Array();
var map;   
//GEOCODER
geocoder = new google.maps.Geocoder();
//Mostramos el mapa completo con los markers
map = new google.maps.Map(document.getElementById('map_canvas'), {
  zoom: 5,
  center: new google.maps.LatLng(43.389082,3.251953),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  zoomControl: true,
  zoomControlOptions: {
    style: google.maps.ZoomControlStyle.SMALL,
    position: google.maps.ControlPosition.RIGHT_TOP
  }
});

//Colocamos todos los markers en el mapa
for (i = 0; i < locations.length; i++) {
	if (locations[i] && locations[i][3] && locations[i][4]) {
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(locations[i][3], locations[i][4]),
			map: map,
			icon: marker_icon_saba
		});
		markerArray[i] = marker;
		
		google.maps.event.addListener(marker, 'click', (function(marker, i){
			return function(){
				//Generamos la informacion que queremos aparesca en la ventana infowindow de cada mapa.
				infoWindowContent = "<img src='img/logoInfoWindow.png'><br />";
				infoWindowContent += "<p style='font-size:10px;'>" + locations[i][1];
				infoWindowContent += "<br /><img src='img/mini-phone-icon.gif'> " + locations[i][2];
				infoWindowContent += "<br /><br /><a href='ficha.php?id=" + locations[i][0] + "'><b>+ info</b></a></p>";
				
				infowindow.setContent(infoWindowContent);
				infowindow.open(map, marker);
			}
		})(marker, i));
	}
}

//Llamamos a esta funcion desde el formulario y mostramos en el mapa el parking mas cercano con su infoWindow abierta.
closestParking = function (){ 
  
  var formAddress = $("input[name=street]").val() + ", "+ $("input[name=city]").val() +", "+ $("select[name=country] option:selected").val();
  
  var distances = new Array();
  //Buscamos las coordenadas de la direccion que han ingresado
  geocoder.geocode( {'address': formAddress }, function(results, status) {
     if(results[0]){
     
         addressCor = { 'lat1' : results[0].geometry.location.lat(),
                        'lon1' : results[0].geometry.location.lng()};
          //Buscamos la coordinada mas cercana de la direccion que nos han dado con el array que tenemos de coordinadas.
          for (var i = 0; i < locations.length; i++) {
		   if (locations[i][3] && locations[i][4]) {
			   addressCor.lat2 = locations[i][3];   
	           addressCor.lon2 = locations[i][4];   
	           
	           distances[i] = findDistance(addressCor);	
		   } 
          } 
          
          distanceMin = distances.min();
          
          //Colocamos el marker en la direccion que han buscado          
          searchedAddress = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng())
            marker = new google.maps.Marker({
              position: searchedAddress,
              map: map
            });
          
          //Abrimos el infowindow para el marker que se encuentra mas cercano.
          //infowindow.setContent(locations[distanceMin.id][1]);
          infowindow.open(map,markerArray[distanceMin.id]);
            
            //Centramos el mapa y ajustamos el zoom para que se vean los 2 markers
            var latlong1 = new google.maps.LatLng(locations[distanceMin.id][3],locations[distanceMin.id][4]);
            var latlong2 = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());

            var LatLngList = Array(latlong1,latlong2);
            //  Create a new viewpoint bound
            var bounds = new google.maps.LatLngBounds ();
            //  Go through each...
            for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
            //  And increase the bounds to take this point
            bounds.extend (LatLngList[i]);
            }
            //  Fit these bounds to the map
            map.fitBounds (bounds);
          
  }    
 }); 
 return false;   
}



