
function validDays(date){
  natDays = [ [4, 01],[4, 02],[4, 03],[4, 04],[4, 05], [8, 01], [8, 02]];
  var today             = new Date();
  var todayDayOfWeek    = today.getDay();
  var todayDay          = today.getDate();
  var todayMonth        = today.getMonth() + 1;
  natDays.push([todayMonth, todayDay]);
  if (todayDayOfWeek == 5  ){
    natDays.push( [todayMonth, todayDay ] , [todayMonth, todayDay+1 ] , [todayMonth, todayDay+2 ] , [todayMonth, todayDay+3 ] )
  }
  if (todayDayOfWeek == 6 ){
    natDays.push( [todayMonth, todayDay+1 ] , [todayMonth, todayDay+2 ] )
  }
  if (todayDayOfWeek == 7 ){
    natDays.push( [todayMonth, todayDay+1 ] )
  }
  for (i = 0; i < natDays.length; i++) {
      if (date.getMonth() == natDays[i][0] - 1
          && date.getDate() == natDays[i][1]) {
        return [false, natDays[i][2] + '_day'];
      }
    }
  return [true, ''];

}

$(function() {

    $("#datepicker").datepicker({
      showOn:           'button',
      buttonImage:      '/imagenes/calendario_peq.gif',
      buttonImageOnly:  true
    });


    $.datepicker.regional['es'] = {
      closeText       : 'Cerrar',
      minDate         : '+1D',
      maxDate         : '+1Y',
      prevText        : '&#x3c;Ant',
      numberOfMonths  : 2,
      nextText        : 'Sig&#x3e;',
      currentText     : 'Hoy',
      monthNames      : ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
      monthNamesShort : ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
      dayNames        : ['Domingo','Lunes','Martes','Mi&eacute;rcoles','Jueves','Viernes','S&aacute;bado'],
      dayNamesShort   : ['Dom','Lun','Mar','Mi&eacute;','Juv','Vie','S&aacute;b'],
      dayNamesMin     : ['Do','Lu','Ma','Mi','Ju','Vi','S&aacute;'],
      dateFormat      : 'dd/mm/yy',
      firstDay        : 1,
      isRTL           : false,
      culture         : 'es',
      beforeShowDay   : validDays
    };
    
	$.datepicker.setDefaults($.datepicker.regional['es']);

});


//PAGINATION NAV
function pagination_nav(pagination_holder, current_page, total_pages){
  var new_link = function(v){
    return (v==current_page) ?
      "<a href=\"#\" class=\"selected\" onclick=\"pagination($('UL#resultados>LI'),$('DIV.pagination-links'),"+v+"); return false;\">"+v+'</a>':
      "<a href=\"#\" onclick=\"pagination($('UL#resultados>LI'),$('DIV.pagination-links'),"+v+"); return false;\">"+v+'</a>' ;
  }
  low  = Math.max (Math.floor(current_page - ( pages_in_pager / 2)), 1);
  high = Math.min(Math.floor(current_page + ( pages_in_pager / 2)), total_pages);
  pagination_holder.html('Páginas: ');
  for (var x=low; x <= high; x ++){
    pagination_holder.append(new_link(x));
  }
}

//PAGINATION
function pagination(element_holder, pagination_holder, current_page){

  element_holder.hide();

  total_results = element_holder.length;

  items_per_page = 10;
  pages_in_pager = 4;

  total_pages = ( Math.floor(total_results / items_per_page) );
  if (!(total_results % items_per_page) == 0) total_pages += 1;

  current_page = current_page || 1;

  //element_holder =$('.hotel-result');
  //pagination_holder =$('#pagination-links');
  pagination_nav(pagination_holder, current_page, total_pages);

  lower_index = (current_page * items_per_page) - items_per_page;
  upper_index = Math.min((lower_index + items_per_page-1),total_results-1);

  for (var i = lower_index; i <= upper_index; i++){
    element_holder.eq(i).show();
  }

}


function showModal()
{
  $('DIV#introListado').hide();
  $('DIV.hotelListado').hide();
  $('#buscando').show();
}


$(document).ready(function(){


  //VENTAJAS TWO COLUMNS BORDER
  $('.ventajas:first').addClass('border-right');

  //BOTON HACERME SOCIO A MITAD DE RESERVA
  $('#altaSocios_tipo_socio').change(function(){
    $('FORM#formularioSocio').submit();
  });

  //BOTON RESPONDER
  $('.button-responder').click(function(){
    $(this).parent().parent().parent().next('tr.responder-form').slideToggle(300, function(cb){});
  });

  //DROPDOWN DE PERSONAS EN EL BUSCADOR DE HOTELES
  $('#personas').change(function(){
    var key = $(this).val();
    if (key == '1,1' ||  key == '2,1'){
      $('#edad1').show();
      $('#ninos').show();
      $('#edad2').val(0);
      $('#edad2').hide();
      $('#edad2Label').text('');
      $('#ninos').show();
    }
    else if (key == '1,2' ||  key == '2,2'){
      $('#ninos').show();
      $('#edad2').show();
      $('#edad2Label').text('Edad niño 2');
      $('#edad2Label').show();
      $('#ninos').show();
    }
    else {
      $('#ninos').hide();
      $('#edad1').val(0);
      $('#edad2').val(0);
    }

  })
  if ($('#edad2').val() == 0){
    $('#edad2').hide();
    $('#edad2Label').hide();
  }

  //INITIALIZE PAGINATION
  pagination($('UL#resultados>LI'),$('DIV.pagination-links'),1);

    //HEADER SLIDESHOW
  $('#portfolio').innerfade({
    speed: 5000,
    timeout: 10000,
    type: 'sequence',
    containerheight: '990px'
  });

  $("#enlaceCondiciones").click(function(event){
		event.preventDefault();
		$('#condicionesReserva').slideDown(1200);
	});

  //LISTADO GENERAL DE PETICIONES
	$(".button-meinteresa").click(function(){
    $(this).slideUp(600, function(){
      var elem = $(this).parent().parent().find('.peticion-llamame');
      elem.slideToggle(1400, function(){
        $.scrollTo(elem, 2400);
      });
    });
  });


  //PARTIAL PETICIONES_DER
	$(".button-meinteresa-der").click(function(){
		var elem = $(this).parent().parent().prev('.llamadaDer');
		elem.slideDown(1200, function(){
    });
	});

  $('.equis').click(function(){
    $(this).parent().parent().slideUp(1200);
  });

	//CONFIRMACIÓN RESERVAS SIN FACTURAR
	$("#btnSolicitar").click(function(event){
		event.preventDefault();
		$("#gracias").slideUp(1400);
		$("#btnSolicitar").slideUp(1000);
		$('#confirmarDatosFacturacion').slideDown(1600, function(){

					$("#btnConfirmar").slideDown(600, function(){
		$.scrollTo('#confirmarDatosFacturacion',2400);
		});});
	});


	$("#enlaceCondicionesContrato").click(function(event){
		event.preventDefault();
		$('#condicionesContratoTxt').slideDown(1200, function(){
		$.scrollTo('#condicionesContratoTxt',2400);
															  });

	});


	//TABLE SORTER FACTURAS
	
//	$("TABLE#facturacion").tablesorter({
//        // sort on the first column and third column, order asc
//     headers: {
//				3: {
//						sorter: false
//				},
//
//				4: {
//						sorter: false
//				}
//			},
//
//			sortList: [[1,0]]
//
//  });
//
//
//
//
//	$("TABLE#reservas_sin_facturar").tablesorter({
//        // sort on the first column and third column, order asc
//     headers: {
//				0: {
//						sorter: false
//				},
//
//				6: {
//						sorter: false
//				}
//			},
//
//			sortList: [[3,0]]
//
//  });
//
//
//	$("TABLE#table_reservas").tablesorter({
//        // sort on the first column and third column, order asc
//     headers: {
//				5: {
//						sorter: false
//				}
//			},
//
//			sortList: [[2,0]]
//
//  });


});

/* CHECK BOX DE LA SELECCION DE RESERVAS SIN FACTURAR */

function facturacion_selecciona_reservas_sin_faturar()
{	
	var form = document.getElementById("form_reservas_sin_facturar");
	var ids = document.getElementById("interfaz_usuario_ids").value;

	ids = "";

	for (i=0;i<form.elements.length;i++)
	{
		if(form.elements[i].type == "checkbox") {
			if(form.elements[i].checked == true){
					ids = ids + form.elements[i].value + ',';					
			}
	  }
	}

	document.getElementById("interfaz_usuario_ids").value = ids;

}


