var centro=new GLatLng(40.420278,-3.705833);
var alo;
function dias(fecha)
{
    fecha=fecha.getFullYear()+'-'+(fecha.getMonth()+1)+'-'+fecha.getDate();

	for(i in calendario[alo])
        if(fecha==calendario[alo][i][0])
   			return [true,calendario[alo][i][1],calendario[alo][i][2]];

    return [false,''];
}
$(document).ready(function(){
//	$("#desde").datepicker({minDate:'+1D'});
 	//$("#hasta").datepicker({minDate:'+2D'});
 	$("#disponibilidad").datepicker({beforeShowDay:dias,hideIfNoPrevNext:true});
/*	$("tr").mouseover(function(){$(this).css("background","lime")});
	//$("tr").click(function(){$(this,'#alojamiento a').click()});
	$("tr").mouseleave(function(){$(this).css("background","red")});
*/
	$("form").submit(function(){$(":checkbox:checked").removeAttr("checked")});
	$(":checkbox").change(function(){var str="";$(":checkbox:checked").each(function(){str+=$(this).attr("id").substr(9)+"-"});$("#servicios").val(str.substring(0,str.length-1))});

	altura=Math.round($(":checkbox").size()/5)*25;
	if($(":checkbox:checked").size()>0)
		$("#servicio").css("height",altura);
	
	$("#servicio legend").click(function(){
	    if($("#servicio").css("height")=="26px")
	    {
	    	if($.browser.msie && $.browser.version==7)
      			$("#formulario,#linea").css("height","auto");
      			
			$("#servicio").animate({height:altura+20,backgroundPosition:"0 "+(altura+9)+"px"});
			$(this).css("background-position","250px -10px");
			$("#buscador").animate({top:altura+25});
		}
		else
		{
			$("#servicio").animate({height:"26px",backgroundPosition:"0 15px"});
   			$(this).css("background-position","250px 0");
			$("#buscador").animate({top:0});
			
			if($.browser.msie && $.browser.version==7)
			{
				$("#linea").animate({height:"82px"});
				$("#formulario").animate({height:"92px"});
			}
		}
	});

	$(".foto img").click(function(){
  		var actual=$(this);
  		var id=actual.attr("id");
  	    switch(id.substr(0,id.lastIndexOf("_")))
  	    {
  	        case "disponibilidad":
  	        	if($("#"+id+"+#disponibilidad:visible").size()==1)
  	        		$("#disponibilidad").fadeOut();
		  	    else
		  	    {
		  	    	$("#mapa").fadeOut();
		  	        alo=id.substr(id.lastIndexOf("_")+1);
		  	        $("#disponibilidad").fadeOut("fast",function(){$(this).datepicker('refresh').insertAfter(actual).css("position","absolute").fadeIn("slow")});
			  	}
  	            break;
  	        case "mapa":
	  	        if($("#"+id+"+#mapa:visible").size()==1)
		  	       $("#mapa").fadeOut();
		  	    else
		  	    {
		  	        var direccion;
             		$("#disponibilidad").fadeOut();
             		for(i in direcciones)
						if(id=="mapa_"+direcciones[i][0])
			  				geocoder.getLatLng(direcciones[i][1],function(point){map.setCenter(point, 9);map.addOverlay(new GMarker(point));});
			  				
			  	    $("#mapa").fadeOut("fast",function(){$(this).css("position","absolute").addClass("mapa_mini").insertAfter(actual).fadeIn()});
			  	    map.checkResize();
			  	}
	  	        break;
		}
	});
	$("#ver_mapa").click(function(){
		var ocultar=".foto,.atajos,.provincia,.servicios,.descripcion,#resultados th:last,#resultados th:eq(0),#resultados th:eq(1),#resultados th:eq(3)";
        if($("#resultados").css("width")=="50%")
        {
            $(this).text("Ver mapa de resultados");
        	$("#mapa").hide(100);
        	$("#resultados").animate({width:"100%"},150);
        	$(ocultar).show();
		}
		else
		{
			$(this).text("Ver tabla de resultados");
			$(ocultar).css("display","none");
			$("#resultados").animate({width:"50%"},50);
			$("#mapa").removeClass("mapa_mini").before($("#resultados")).show(100,function(){
				map.checkResize();
				map.setCenter(centro,5);
			});
		}
	});
	if (GBrowserIsCompatible()) {
  		var map = new GMap2(document.getElementById("mapa"));
  		map.setCenter(centro,6);
  	    map.addControl(new GLargeMapControl());
  		map.addControl(new GMapTypeControl());
  		var geocoder = new GClientGeocoder();
  
		for(i in direcciones)
		{
		    var direccion=direcciones[i];
		    var imagen=direccion[2];
		    geocoder.getLatLng(direccion[1],function(point)
			{
/*				if(!point)
					alert(direccion[1] + " not found");
				else
				{*/
				    var marker = new GMarker(point);
				    GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowTabsHtml('<img src="/img/alojamientos/'+imagen+'_p.jpg" />');
					});
				    map.addOverlay(marker);
//    			}
			});
		}
  	}

});

