numFotosVerts = new Array(3,2,6,4,2,2,1,3,3,4);
numSecciones  = 10;
ancho         = 0;
alto          = 0;
anchoLogo     = 280;
currVert      = new Array(0,0,0,0,0,0,0,0,0,0);
apartOcult    = false;
inicio = false;


var actAb;
              
$(document).ready(function () {
  $("body").queryLoader2({
	percentage:true,
    onComplete	: function(){
    
      
    var $tf_bg			    = $('#tf_bg'),
    $tf_bg_images		    = $tf_bg.find('.imgFondo'),
    $tf_bg_img			    = $tf_bg_images.eq(0),
    $tf_thumbs			    = $('#tf_thumbs'),
    total				        = $tf_bg_images.length,
    current				      = 0,
    $tf_content_wrapper	= $('#tf_content_wrapper'),
    $tf_next			      = $('#tf_next'),
    $tf_prev			      = $('#tf_prev'),
    $tf_loading			    = $('#tf_loading');
        
    if (!inicio){
      init();
    }
    
  function init(){
     inicio = true;
     
     //CAMPO NEWSLETTER
     $('#txtNewsletter').focus(function() {
        if ($(this).val() == "E-mail"){
          $(this).val('')          
        }
      });
      
      $('#txtNewsletter').focusout(function() {
        if ($(this).val() == ""){
          $(this).val('E-mail')          
        }
      });
    
    preparaForm();
    $("#btShowContent").hide();
    actAb     = $("#irRoom");
  
    for (a = 0; a < total; a++){
      $tf_bg_img			= $tf_bg_images.eq(a);
      //get dimentions for the image, based on the windows size
    	var dim	= getImageDim($tf_bg_img);
    	//set the returned values and show the image
    	$tf_bg_img.css({
    		width	: dim.width,
    		height	: dim.height,
    		left	: dim.left,
    		top		: dim.top
    	}).fadeIn();
    }
  
    redim();
    
  	//resizing the window resizes the $tf_bg_img
    $(window).bind('resize',function(){
    	for (a = 0; a < total; a++){
    	  $tf_bg_img			= $tf_bg_images.eq(a);
        var dim	= getImageDim($tf_bg_img);
        $tf_bg_img.css({
      		width	: dim.width,
      		height	: dim.height,
      		left	: dim.left,
      		top		: dim.top
      	});
      }
       /*
      var dim	= getImageDim($(".imgFondo"));
      $(".imgFondo").css({
    		width	: dim.width,
    		height	: dim.height,
    		left	: dim.left,
    		top		: dim.top
    	});  */
    	
    	scroll(-1);
      redim();
    });
  	
  	//IR SECCIONEs
  	$("#irRoom").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(0);
  	});
  	
  	$("#irCafeteria").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(1);
  	});
  	
  	$("#irRestaurante").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(2);
  	});
  	
  	$("#irLounge").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(3);
  	});
  	
  	$("#irCatering").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(4);
  	});
  	
  	$("#irContacto").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(5);
  	});
  	
  	$("#irVisita").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
  			actAb = $(this); 
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$(this).attr('class', 'botonSuperiorMarcado');
  			scroll(6);
  	});
  	
  	$("#destacadoVigo").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
  			$('#fondDestVigo').attr('class', 'botonDestacadoMarcado');
  			scroll(7);
  	});
    
    
  	
  	$("#destacadoPontevedra").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
        $('#fondDestPonte').attr('class', 'botonDestacadoMarcado');
  			scroll(8);
  	});
  	
  	$("#destacadoCatas").bind('click',function(){
  		if($tf_bg_img.is(':animated'))
  			return false;
  			actAb.attr('class', 'botonSuperior');
        $('.botonDestacadoMarcado').attr('class', 'botonDestacado');
        $('#fondDestCatas').attr('class', 'botonDestacadoMarcado');
  			scroll(9);
  	});
  	
  	// BOTONES SIGUIENTE ANTERIOR
  	//$tf_next.bind('click',function(){
    $tf_next.click(function(){
  		scrollVert(1); 
  	});
  	
  	//click the arrow up, scrolls up
  	$tf_prev.bind('click',function(){
  	   scrollVert(-1);
  	});
    
    //OCULTAR APARTADOS
    $(".btCerrarApart").bind('click',function(){
      $('#envioconsulta').validationEngine('hide');
      apartOcult = true;
      $(this).parent().hide('fast', function() {
         $("#btShowContent").show();
      });     
    }); 
    
    $("#btShowContent").bind('click',function(){
      $(".btCerrarApart").parent().show('slow');
      apartOcult = false;
      $(this).hide();           
    }); 
    
    
    
    
  }
  
  //FUNCIONES DE REDIMENSIONADO COMUNES
  function redim(){
    $('#envioconsulta').validationEngine('hide');
    $("#ul_bg").css({
      width	: ancho * numSecciones
    });
    
    anchoMenu = ($(window).width()-1 - anchoLogo) / 7;
    $(".botonSuperior").css(
    	'width',anchoMenu
    );
    
    if (anchoMenu < 110){
      $(".botonSuperior").css(
      	'font-size','14px'
      );
      $(".botonSuperiorMarcado").css(
      	'font-size','14px'
      );
      
    } else {
      $(".botonSuperior").css(
      	'font-size','18px'
      );
      $(".botonSuperiorMarcado").css(
      	'font-size','18px'
      );
    }
    
    $(".botonSuperiorMarcado").css(
    	'width',anchoMenu
    );
    
    $("#destacados").css(
    	'top',$(window).height() - 120
    );
    
    
    posB = (alto - $(window).height()) + 80;
    heiLog = $(window).height() * 0.40; 
    
    $(".logoInfOut").css('height', heiLog);
    
    for (a = 0; a < 5; a++){
      posR = $(window).width() - ($(".imgLogoInf").width()/2) +  + (($(window).width()* 2) * a);
    
      $("#logoInfOut" + a).css('top',$(window).height() - 80 - heiLog);
      $("#logoInfOut" + a).css('left', posR);
    }
    
    for (a = 0; a < numSecciones; a++){
      $("#intApart" + a).css('top', -(alto + 3) * currVert[a]);
    }
    
    //POSICION APARTADOS
    $(".contAparts").css('top', (($(window).height() - 80)/2) - 120);
    $(".contAparts").css('left', ($(window).width()/2) - 350);
    
    $(".contApartsContact").css('left', ($(window).width()/2) - 350);
    $(".contApartsContact").css('top', (($(window).height() - 80)/2) - 180);
    
	$(".contVisit").css('left', ($(window).width()/2) );
    $(".contVisit").css('top', (($(window).height() - 80)/2));
    
  }
  
  //show next / prev image
  function scroll(dir){
    $('#envioconsulta').validationEngine('hide');
    $("#btShowContent").hide();
    $(".btCerrarApart").parent().show('slow');
    apartOcult = false;
  
    if (dir != -1){
      current = dir;
    }
  
    $("#tf_bg").stop().scrollTo(ancho*current, 800 )
    
    if (dir > 0){
      $(".fotoDest").stop().animate({ top: "100" }, 500 );
    } else {
      $(".fotoDest").stop().animate({ top: "0" }, 500 );  
    }
  }
  
  //show next / prev image
  function scrollVert(num){
    currVert[current] += num;
    if (currVert[current] < 0){
      currVert[current] = 0;
    } else if (currVert[current] > numFotosVerts[current]-1){
      currVert[current] = numFotosVerts[current]-1;
    }
  
    $("#intApart" + current).stop().animate({
      top		: -(alto + 3) * currVert[current]
    });
  }
  
  
  //get dimentions of the image, 
  //in order to make it full size and centered
  function getImageDim($img){
    var w_w	= $(window).width(),
    w_h	= $(window).height() - 80,
    r_w	= w_h / w_w,
    i_w	= $img.width(),
    i_h	= $img.height(),
    r_i	= i_h / i_w,
    new_w,new_h,
    new_left,new_top;
  	
  	if(r_w > r_i){
  		new_h	= w_h;
  		new_w	= w_h / r_i;
  	} else {
  		new_h	= w_w * r_i;
  		new_w	= w_w;
  	}
  
    ancho = new_w; 
    alto = new_h;
  
  	return {
  		width	: new_w + 'px',
  		height	: new_h + 'px',
  		left	: (w_w - new_w) / 2 + 'px',
  		top		: (w_h - new_h) / 2 + 'px'
  	};
  	}
        
    $("#aCargar").css("visibility","inherit");
    }
  });
});
