$(document).ready( function(){


	//carousel de promociones
	/*$(".lomas_slider").jCarouselLite({
        btnNext: ".fotosig",
        btnPrev: ".fotoatras",
		visible: 1,
		auto: 4000,
		speed: 800
    });*/
	
	//efecto slider de las noticias del blog
	$(".scroll-pane").jCarouselLite({vertical:true, visible:1, auto:6000});

    $(".rollover").hover(
		 function()
		 {
		  this.src = this.src.replace("_off","_on");
		 },
		 function()
		 {
		  this.src = this.src.replace("_on","_off");
		 }
	);

	//cuando den click alas fotos del slider principal
	$(".widget img").click(function() {
		var idactual = $(this).attr("id");
		
		if(  idactual=="imgchatsupport" )
			$(".liveSupport").trigger('click');
			
			
		if(  idactual=="mayairmerida" )
			document.location.href="http://www.mayair.com.mx";
			
	})
	
	//Efecto del carrusel de las imagenes de hasta abajo
	$(".tour_slideshow").jCarouselLite({
        btnNext: ".tour_slideshow .next",
        btnPrev: ".tour_slideshow .prev",
		visible : 4
    });

	//agrega la ventana de chat a los links
	var LigaChat="<a class='hidden' id=\"_lpChatBtn3\" href='http://server.iad.liveperson.net/hc/39769008/?cmd=file&file=visitorWantsToChat&site=39769008&byhref=1&imageUrl=http://www.lomas-travel.com/images/livehelp&SESSIONVAR!skill=Lomas Travel Services' target='chat39769008'  onClick=\"lpButtonCTTUrl = 'http://server.iad.liveperson.net/hc/39769008/?cmd=file&file=visitorWantsToChat&site=39769008&imageUrl=http://www.lomas-travel.com/images/livehelp&referrer='+escape(document.location); lpButtonCTTUrl = (typeof(lpAppendVisitorCookies) != 'undefined' ? lpAppendVisitorCookies(lpButtonCTTUrl) : lpButtonCTTUrl); window.open(lpButtonCTTUrl,'chat39769008','width=472,height=320,resizable=yes');return false;\" >Live Support</a>";

	/***************************************************************************************/
	//live chat
	$("#contenedor").append(LigaChat);
	$(".liveSupport").live('click',function(e){
		e.preventDefault();
		$.post("/php/control_sitio_chat.php","",function(){$("#_lpChatBtn3").click();});
		/*
		$.ajax({
			   cache: false,
			   async: false,
			   type: "POST",
			   url: "http://www.lomas-travel.com/php/verifica_livehelp.php",
			   success: function(e){
				 if(e.length==4)
					{
						$("#contenedor").prepend("<div id='live_false' class='hidden'></div>");
						$("#live_false").append(e);
					}
			   }
		});


		var dato_live = $("#live_false").text();
		if(  dato_live=='bien' )
		{
			alert("Live Help Unavailable / Chat no disponible");
			document.location.href="http://www.lomas-travel.com/contactus/index.php";
		}
		else
			$("#_lpChatBtn3").click();*/



	});
	//fin del chat
	/************************************************************************************************/

	//efecto de carousel en related tours
	$('#slideshow').cycle({
		fx:     'fade',
		speed:       100,
		timeout:     5000,
		pagerEvent: 'click',
		pagerClick: function(idx){
			var idtour = 0;

			if( idx == 0 )
				idtour = 16;

			if( idx == 1 )
				idtour = 51;

			if( idx == 2 )
				idtour = 50;

			if( idx == 3 )
				idtour = 22;

			$.post("http://www.lomas-travel.com/xml-files/tours_slider.xml","", function(data){
				success:
						var xml;
						if(typeof data == "string"){
							xml = new ActiveXObject("Microsoft.XMLDOM");
							xml.async = false;
							xml.loadXML(data);
						} else {
							xml = data;
						}

						$(xml).find('tour').each(function(){
							var id = $(this).attr("id");
							if( idx == id )
							{
								var title = $(this).find('title').text();
								var url = $(this).find('url').text();
								var desc = $(this).find('desc').text();
								var precio = $(this).find('precio').text();

								var html_t='<h3><a href="'+url+'">'+title+'</a></h3><p>'+desc+'<a href=\"'+url+'">more info...</a></p><!--<p id="precio">Just <strong>'+precio+' usd</strong></p>-->';
								$("#info_carousel").empty().append(html_t);
							}
						});
					//$("#spot_light").empty().append(e);
			},($.browser.msie) ? "text" : "xml");
		},
		pauseOnPagerHover: true,
        pager:  '#nav',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#nav li:eq(' + (idx) + ') a';
        },
		after: onAfter
    });

	function onAfter() {

		var idtour = 0;

			if( this.id == 1 )
				idtour = 0;

			if( this.id == 2 )
				idtour = 1;

			if( this.id == 3 )
				idtour = 2;

			if( this.id == 4 )
				idtour = 3;

			//alert(this.id);

		//if( this.id!=0){

			$.post("http://www.lomas-travel.com/xml-files/tours_slider.xml","", function(data){
					success:
							var xml;
							if(typeof data == "string"){
								xml = new ActiveXObject("Microsoft.XMLDOM");
								xml.async = false;
								xml.loadXML(data);
							} else {
								xml = data;
							}

							var id;
							$(xml).find('tour').each(function(){
							id = $(this).attr('id');
							if( idtour == id )
							{
								var title = $(this).find('title').text();
								var url = $(this).find('url').text();
								var desc = $(this).find('desc').text();
								var precio = $(this).find('precio').text();

								var html_t='<h3><a href="'+url+'">'+title+'</a></h3><p>'+desc+'<a href=\"'+url+'">more info...</a></p><!--<p id="precio">Just <strong>'+precio+' usd</strong></p>-->';
								$("#info_carousel").empty().append(html_t);
							}
						});
						//$("#spot_light").empty().append(e);
				},($.browser.msie) ? "text" : "xml");
		//}
	}
	//fin del efecto carousel
	/************************************************************/


	$("#btn_search").click( function(){
		$("#cse-search-box").submit();
	});

	//codigo para el contact us
	$("#SubmitContactUs").click(function(){

		if(($("#frm_support").val())=="Select"){
			alert("Please select Department support");
			$("#frm_support").focus();
			return false;
		}

		if (!filters["requerido"]("#frm_usrname")) {
			alert("Please type your Client Full Name");
			$("#frm_usrname").focus();
			return false;
		}

		if (!filters["requerido"]("#frm_usremail")) {
			alert("Please type your e-mail address");
			$("#frm_usremail").focus();
			return false;
		}

		if (!filters["email"]("#frm_usremail")) {
			alert("Please change the format of your e-mail address");
			$("#frm_usremail").focus();
			return false;
		}

		if($("#frm_country").val()=="Select"){
			alert("Please select country");
			$("#frm_country").focus();
			return false;
		}

		if (!filters["requerido"]("#frm_usrphone")) {
			alert("Please type your phone number");
			$("#frm_usrphone").focus();
			return false;
		}

		if (!filters["telefono"]("#frm_usrphone")) {
			alert("Please change the format of your Phone Number. The numbers have to be continuous, without any spaces nor symbols (such as _, /, *,).");
			$("#frm_usrphone").val('');
			$("#frm_usrphone").focus();
			return false;
		}

		if (!filters["requerido"]("#frm_usrcomm")) {
			alert("Please your Comments");
			$("#frm_usrcomm").focus();
			return false;
		}

		var datos_form = $("#formContact").serialize();
		$("#areaMensaje").show();
		$("#imgMensaje").show();
		$("#msjTexto1").show();
		$("#msjTexto2").hide();
		$("#msjTexto3").hide();
		$.post("enviarSolicitudContact.php",datos_form,function (data){
			success:

				$("#formContact").find("input, textarea").each(function(indice,id){
		      		$(id).attr("value",'');
		      	});

				$("#imgMensaje").hide();
				$("#msjTexto1").hide();
				$("#msjTexto2").hide();
				$("#msjTexto3").hide();

				if (data==1)
					$("#msjTexto2").show();
				else
					$("#msjTexto3").show();

		});

		return false;
	});

	//Sirve para mostrar/ocultar el formulario Afiliado
	$("#links_signin").click(function(e){
		e.preventDefault();
		$("#login_affiliates").toggle("slow");
	});

	$("#informacion_show").click(function(){


		$(".informacion_hide").fadeIn('slow');


	});

	//Para el submit del formulario de Afiliados
	$("#btn_signin").click(function(){

		if (!filters["requerido"]("#txtUsername")) {
			alert("Please type your e-mail address");
			$("#txtUsername").focus();
			return false;
		}

		if (!filters["email"]("#txtUsername")) {
			alert("Please change the format of your e-mail address");
			$("#txtUsername").focus();
			return false;
		}

		if (!filters["requerido"]("#txtPassword")) {
			alert("Please type your e-mail address");
			$("#txtPassword").focus();
			return false;
		}

		var datos_form = $("#form_affiliates").serialize();
		$("#msgAfiliado").show();
		$.post("afiliados/validarUsuarioAfiliado.php",datos_form,function (data){
			success:
				$("#msgAfiliado").hide();
				if (data==1)
					$("#form_affiliates").submit();
				else{
					alert("Please check your details and try again");
					$("#txtUsername").focus();
					return false;
				}

		});

		return false;
	});


});


var filters = {
    requerido: function(el) {return ($(el).val() != '' && $(el).val() != -1);},
    telefono: function(el){return /^[0-9]*$/.test($(el).val());},
    email: function(el) {return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(el).val());
     }
};







