$(document).ready(function() {

//------------ Si pulsamos cualquier boton del menu
    $("#menu a, #capaReservas a, #info_legal").each(function(){
        var href = $(this).attr("href");
        href = href.replace(".html", "");
        href = href.replace(".php", "");
		    
        $(this).attr({ href: "#" + href});
        $(this).click(function(){
            if ($("#contenedor").position().left == -399){
                if (href == "tarifas"){
                  cargaApart (idioma + '/contenidos/' + href + ".php");
                }else{
                  cargaApart (idioma + '/contenidos/' + href + ".html");
                }
				marcBts(href);
            } else {
                $('#contenedor').stop().animate({
                    left: -399
                }, 1500,'easeOutCubic',  function(){
                
                if (href == "tarifas"){
                  cargaApart (idioma + '/contenidos/' + href + ".php");
                }else{
                  cargaApart (idioma + '/contenidos/' + href + ".html");
                }
					marcBts(href);
                }).delay(400);
            }
        });
    });

//------------ Si pulsamos en Inicio
    $("#inicio").click(function(){
    $("#backstretch img").attr("src", "images/fondo.jpg");
		$("#index").show();
		$(this).attr({ href: "#inicio"});
        $('#contenedor').stop().animate({
            left: -399
        }, 1500,'easeOutCubic', function (){
				cleanAnc(); //Vaciar anclas
				
			});
    });
	
	

//------------ Carga de contenido y apertura
    function cargaApart(h){
		cleanAnc(); //Vaciar anclas
		$("#carga").load(h);
        $('#contenedor').animate({
            left: 281
        }, 1500,'easeOutCubic', function (){
			$("#index").hide();
		});
    }
	
//------------ Vaciar cargadores
	function cleanAnc(){
		$("#carga *").remove();
		$("#cargaSubmenu *").remove();
	
		var apartados = new Array("bt_apartamentos","bt_adosados","bt_entorno","bt_tarifas","bt_localizacion","bt_contacto");
		for (a = 0; a < apartados.length; a++){
			$("." + apartados[a] + " .botones").attr("id","");
		}
		$(".bt_prin_reservas .bt_reservas").attr("id","");
	}
	
//------------ Altura Menu
	$('#contenedor').resize(function() {
  		//alert('contenedor: ' + $('#contenedor').height());
		  //alert('window: ' + $(window).height());
		minH = 790;
		w = $(window).height();
		c = $('#contenedor').height();
		
		if (w < minH){
			$("#contenedor").css("min-height", 790);
		} else {
			$("#contenedor").css("min-height", "100%");
		}
		
		
		if (c > w){
			$('#barra_menu').height(c);
		} else {
			if (w < minH){
				$('#barra_menu').height(minH);
			} else {
				$('#barra_menu').height('100%');
			}
		}
	});
  
  
  	has = window.location.hash;
	has = has.replace("#", "");
	var nombres = has.split("/");
	
  	switch(has){
		case "inicio":
		case "":
			marcBts("");
		break;
		default:
			if (nombres[0] == "tarifas"){
        cargaApart (idioma + '/contenidos/' + nombres[0] + ".php");
      }else{
        cargaApart (idioma + '/contenidos/' + nombres[0] + ".html");
      }

			if (nombres.length > 1){
				$("#cargaSubmenu").load(idioma + '/contenidos/' + nombres[0] + "/" + nombres[1] + ".html");
			}
			marcBts(nombres[0]);
		break;
	}
	
//----------------- Marcamos los botones seleccionados del menu principal	
	function marcBts (has){
		switch(has){
			case "2_habitaciones":
			case "3_habitaciones":
			case "apartamentos":
			
  			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/apartamentos.jpg");
        });
  			
				$(".bt_apartamentos .botones").attr("id","selectedMenu");
			break;
			case "adosados":
				$(".bt_adosados .botones").attr("id","selectedMenu");
				
			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/adosados.jpg");
        });
			break;
			case "entorno":
				$(".bt_entorno .botones").attr("id","selectedMenu");
				
			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/entorno.jpg");
        });
			break;
			case "tarifas":
				$(".bt_tarifas .botones").attr("id","selectedMenu");
				
			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/tarifas.jpg");
        });
			break;
			case "localizacion":
				$(".bt_localizacion .botones").attr("id","selectedMenu");
			
			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/localizacion.jpg");
        });
			break;
			case "contacto":
				$(".bt_contacto .botones").attr("id","selectedMenu");
			
			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/contacto.jpg");
        });
			break;
			case "reservas":
				$(".bt_prin_reservas .bt_reservas").attr("id","selectedMenu");
				
			$("#backstretch").fadeOut(function() {
              $(this).remove();
              $.backstretch("images/fondos/reservas.jpg");
        });
			break;
			default:
			break;	
		}
	}
});
