var urlPost = (("https:" == document.location.protocol) ? "https://www.mayair.com.mx" : "http://www.mayair.com.mx");

$(document).ready(function() {
    try {
    $("#fancybox").msDropDown();
    //$("#websites2").msDropDown({mainCSS:'dd2'});
    //alert($.msDropDown.version);
    //$.msDropDown.create("body select");
    $("#ver").html($.msDropDown.version);
    } catch(e) {
    alert("Error: "+e.message);
    }
    
  //Con esto ocultamos la barra del FB
	$("#iconoCerrarFB a").click(function(e){
		e.preventDefault();
		
		$("#ShareSidebar").remove();
	});
    

$("#fancybox").change( function(){
    idioma = $(this).val();

    if( idioma=='esp' )
        document.location.href=urlPost+"/es";
});
    
    /************************************/

    $('a#localrate').qtip({
        content: 'Local Promotion is only for people living in <strong>Yucatan, Q.Roo and Campeche.</strong><br /><br />Foreign People can enjoy this promotion by showing a local ID like <strong>IFE</strong> or <strong>FM3</strong>.',
        show: 'mouseover',
        hide: 'mouseout',
        style: {name:'dark'}
    });

    $('a#ironmanrate').qtip({
        content: '<strong>IRONMAN rate</strong> is only for people who comes to Cozumel to the <strong>IRONMAN Contest</strong>.<br /><br /> MAYAir offers an <strong>special Promotion fare</strong> by taking contenders to Cozumel island.',
        show: 'mouseover',
        hide: 'mouseout',
        style: {name:'dark'}
    });
    
    
    $(".ver_detalle_noche").click(function(e){
			e.preventDefault();
			var nombre = $(this).attr("name");
			//$(".contenido-ficha").hide();
			$("#ficha_"+nombre).toggle();
	});
	
	
	$("#cboOrigenVuelo").change(function(){

		var cboOrigenVuelo = $("#cboOrigenVuelo").val();
		var bookTipo = $("#bookTipo").val();

		if(cboOrigenVuelo=="select")
			return false;

		$("#cboDestinoVuelo").empty().append("<option>Wait, processing data...</option>");
		$("#cboDestinoVuelo").attr("disabled","disabled");
		$.post("/includes/cargarDestinosVuelos.php",{cboOrigenVuelo:cboOrigenVuelo,bookTipo:bookTipo},function(data){
			sucess:
			$("#cboDestinoVuelo").empty().append(data);
			$("#cboDestinoVuelo").attr("disabled","");
			var valor = $("#cboDestinoVuelo").val();
            actualizarHotel(valor);
		});

	});
	
	$("#cboDestinoVuelo").change(function(){
    	var valor = $(this).val();	
    	actualizarHotel(valor);
    });
	
	
	$("#cboDestinoVuelo").change(function(){
    	var valor = $(this).val();	
    	actualizarHotel(valor);
    });
    
    function actualizarHotel(destino){
    	
    	$("#txtHotel").autocomplete("/includes/cargarHotelesVenta.php?destino="+destino, {
			width: 260,
			selectFirst: false
		});
    }
	
	
	$("#rdHotelVuelo, #rdHotel").click(function(){
			var id_sel = $(this).attr("id");
			
			if(id_sel=="rdHotel"){
				$("#fila_hotel").show();
				$(".fila_hotel_avion").hide();
				$("#contenedor_paquete").css("padding","10px 0 0 0");
				$("#bookTipo").val('H');
			}else{
				$(".fila_hotel_avion").show();
				$("#fila_hotel").hide();
				$("#contenedor_paquete").css("padding","0px");
				$("#bookTipo").val('HV');
			}
	});
	
		
	
	
	$("input.date-picker").datepicker({minDate:1,onClose:function(input){moverDia($(this).attr("id"));},numberOfMonths: 1});	
	
	function moverDia(id){
			var startDate = $("#"+id).datepicker('getDate');
			if (startDate != null) {
				
				startDate.setDate(startDate.getDate()+1);
				
				if(id=="txtCheckIn_2" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtCheckOut_2").datepicker('option', 'minDate',startDate);
					$("#txtCheckOut_2").datepicker('setDate',startDate);
				}

			}
		}
		
	$("#frmHoteles").submit(function(){
		var fe_1 = $("#txtCheckIn").val();
		var fe_2 = $("#txtCheckOut").val();
		var destino = $("#cboDestino").val();
		var cboOrigenVuelo = $("#cboOrigenVuelo").val();
		var cboDestinoVuelo = $("#cboDestinoVuelo").val();
		var keyword_hotel = $("#keyword_hotel").val();
		var txtHotel = $("#txtHotel").val();
		var rdServicio = $("#rdHotel").attr("checked");

		if(rdServicio){
			if(keyword_hotel=="select"){
				$("#txtHotel").val("");
				if(destino=="select"){
					alert("Please select a Destination");
					$("#cboDestino").focus();
					return false;
				}
	
				if(fe_1=="mm/dd/yyyy"){
					alert("Please select your arrival date");
					$("#txtCheckIn").focus();
					return false;
				}
	
				if(fe_2=="mm/dd/yyyy"){
					alert("Please select your departure date");
					$("#txtCheckOut").focus();
					return false;
				}
	
				desplegarInfoEspera();
			}
			$("#frmHoteles").attr("action","/hotels/procesa_parametros_busqueda.php");
		}else{
				if(cboOrigenVuelo=="select"){
					alert("Please select a Destination");
					$("#cboOrigenVuelo").focus();
					return false;
				}
				
				if(cboDestinoVuelo=="select"){
					alert("Please select a Destination");
					$("#cboDestinoVuelo").focus();
					return false;
				}
	
				if(fe_1=="mm/dd/yyyy"){
					alert("Please select your arrival date");
					$("#txtCheckIn").focus();
					return false;
				}
	
				if(fe_2=="mm/dd/yyyy"){
					alert("Please select your departure date");
					$("#txtCheckOut").focus();
					return false;
				}
				desplegarInfoEspera();
				$("#frmHoteles").attr("action","/hotels/procesa_parametros_busqueda_pqt.php");
				
		}

		return true;
	});
	
	//funcion que despliega el overlay cuando se cotiza
    function desplegarInfoEspera()
    {
        $('#overlay').show();

    	$('#overlay').fadeIn('fast',function(){
            $('#box').show();
            $('#box').animate({'top':'160px'},700);
	});
    }

		$("#cboHab").change(function(){
		var cboHab = $("#cboHab").val();

		$(".filaHab").hide();

		for(var i=1;i<=cboHab;i++){
			$("#filaHab_"+i).show();
		}

	});

	$(".cboNinoHab").change(function(){
		var id_grl = $(this).attr("id");
		var valores = id_grl.split("_");
		var id = valores[1];
		var numNinos = $(this).val();

		$("#lbEdad_"+id).hide();
		$("#cboEdadUno_"+id).hide();
		$("#cboEdadDos_"+id).hide();
		$("#cboEdadTres_"+id).hide();

		if(numNinos==1){
			$("#lbEdad_"+id).css("display","block");
			$("#cboEdadUno_"+id).show();
		}

		if(numNinos==2){
			$("#lbEdad_"+id).css("display","block");
			$("#cboEdadUno_"+id).show();
			$("#cboEdadDos_"+id).show();
		}

		if(numNinos==3){
			$("#lbEdad_"+id).css("display","block");
			$("#cboEdadUno_"+id).show();
			$("#cboEdadDos_"+id).show();
			$("#cboEdadTres_"+id).show();
		}

	});	
	
	//Esto es para mostrar los detalles de las tarifas y promos de los hoteles
	$("a.mostrarDetallePrecioHotel").click(function(e){
			e.preventDefault();
	});

    $('a.mostrarDetallePrecioHotel[href][title]').qtip({
      content: {
         text: false // Use each elements title attribute
      },
     style: {
      width: 600,
      name: 'green',
      background:'#f6f6f6',
      border:{color:'#009092',width:6},
      color:'#696969',
      tip: 'topRight'
   },
   position: { adjust: { x: -600, y: 0 } }
   });
   
   
   var orden_grl = 0;
	$("#r_ordenacion").change(function(){
		$("#frmBusqueda").attr("action","/hotels/asignar_orden_grl.php");
		orden_grl = 1;
		$("#frmBusqueda").submit();
	});
	
	//Codigo de hoteles
	$("#r_ordenacion2").change(function(){
		orden_grl = 0;
		$("#frmBusqueda").submit();
	});
	
	$(".cssBtnCompraHotel").click(function(){
			var datos = $(this).val();
			$("#datosHab").val(datos);
			desplegarInfoEspera();
			$("#frmSeleccionHab").submit();
	});
	
	$(".cboVuelos_ida").change(function(){
		var id = $(this).attr("id");
		var op = $(this).val();
		var op = op.split("#");
		var valor = id.split("-");
		var abuelo = $(this).parent().parent();

		$("tr#"+ abuelo.attr("id") + " td:eq(1) .contenedor_horarios").hide();
		$("#contenedor_horarios_"+valor[1]+"_"+op[0]).show();
	});

	$(".cboVuelos_regreso").change(function(){
		var id = $(this).attr("id");
		var op = $(this).val();
		var op = op.split("#");
		var valor = id.split("-");
		var abuelo = $(this).parent().parent();

		$("tr#"+ abuelo.attr("id") + " td:eq(1) .contenedor_horarios").hide();
		$("#contenedor_horarios_"+valor[1]+"_"+op[0]).show();
	});

	$(".cssBtnCompraHotelAvion").click(function(){
			var valores = $(this).attr("id");
			var id = valores.split("_");
			var datos = $(this).val();

			$("#datosHab").val(datos);
			$("#datosVueloIda").val($("#cboVuelos-ida_"+id[1]).val());
			$("#datosVueloRegreso").val($("#cboVuelos-regreso_"+id[1]).val());
			desplegarInfoEspera();
			$("#frmSeleccionHab").submit();
	});
	
	$("ul.cambiarPaginaBusqueda li a").click(function(e){
			e.preventDefault();
			var nombre = $(this).attr("name");
			var valores = nombre.split("_");
			$("#numPagina").val(valores[1]);
			$("#frmPaginacion").submit();
	});
    
    
});
