var mostrar_ut_sistema = true; //Saber si hay que mostrar los UT de sistema o de resultado de disponibilidad
var array_marcadores_disponibilidad = new Array();
var progressBar = null;
var num = 0;
var n_hoteles = 0;

function lanzarActualizarMapa(){
	progressBar = new ProgressbarControl (map,{width:150});
		
	$.getJSON("/servicios/servicioBusqueda_mapa.php",function(datos){			
		if(datos.hoteles != null && datos.hoteles.length > 0){
			borrarMarcadoresDisponibilidad();
			array_marcadores_disponibilidad = new Array();
			num = 0;
			n_hoteles = datos.hoteles.length;
			progressBar.start(n_hoteles);
			
			var iteracion = 0;
			var first = true;
			setTimeout(function(){
				pushMarcadorJSON(datos.hoteles,iteracion,first);
			},10);
		}
		
	});
}

function borrarMarcadoresDisponibilidad(){
	if(array_marcadores_disponibilidad != null && array_marcadores_disponibilidad.length > 0){
		for(i=0;i<array_marcadores_disponibilidad.length;i++){
			var marcador = array_marcadores_disponibilidad[i];
			map.removeOverlay(marcador);
		}	
	}	
}

function pushMarcadorJSON(input,iteracion,first){
	datos = input[iteracion];
	
	num = num + 1; //N de elementos cargados
	progressBar.updateLoader(1); //Actualizar el progressbar
	//alert(datos);
	if(typeof(datos) != 'undefined'){
		var nombre = datos.nombre;
		var direccion = datos.direccion;
		var precio = datos.mejorPrecio;
		var url = datos.mejorProveedorURL;
		var latitud = datos.latitud;
		var longitud = datos.longitud;
		
		var html = '<div class="gmaps gmaps-simple"><strong>'+nombre+'</strong><br />'+direccion+'<br /><strong>Mejor precio:</strong> '+precio+'&euro;<br /><a href="'+url+'" target="_blank">Reservar</a></div>';
		var marcador = createMarkerJSON(new GLatLng(latitud,longitud),html,nombre,'hotel','hotel_disponibilidad');
		map.addOverlay(marcador);
		array_marcadores_disponibilidad.push(marcador); //Aņadir al control de marcadores
		if(first == true){
			//marcador.openInfoWindowHtml(html);
			//map.setCenter(new GLatLng(lat_n,log_n));
			first = false;	
		}
		filtros_marcadores['hoteles_disponibilidad'].push(marcador);
	}
	
	iteracion += 1;
	if (num < n_hoteles) {
		setTimeout(function(){pushMarcadorJSON(input,iteracion,first);},100);
	}
	else{
		progressBar.remove();
	    num = 0;
	}
}

function ocultarHotelesSistema() {
	if( typeof(filtros_marcadores) != 'undefined'){ 
		if( typeof(filtros_marcadores["hotel"])  != 'undefined'){ 
			if(filtros_marcadores != null && filtros_marcadores["hotel"].length > 0){
				for (var i = 0; i < filtros_marcadores["hotel"].length; i++) {
					var marker = filtros_marcadores["hotel"][i];
					marker.hide();
				}
			}
		}
	}
}