jQuery(document).ready(function ($) {
	
	//slideshow:
	$('.bodyImage').cycle({
		timeout:  5000
	});
								 
								
    //altera a altura do scrollcontainer (SH), dependendo do tamenho do título (TH) e dos links laterais (LH):
    maxSH = 352;

    TH = $(".pageTitle").height();
    LH = $(".colNav").height();

    if (!TH) TH = 0;
    if (!LH) LH = 0;

    //agora coloco a altura do scrollcontainer, será sempre maxSH - (TH + LH)
    $('.colContentWrapper').css("height", maxSH - (TH + LH) + "px");

    //no caso das caixas de conteúdo:
    //tem que fazer para cada um:
    $('.individualBox').each(function (index) {
        boxMaxSH = 233;
		
		//no caso das smallBoxes
		if($('.smallBoxes').length > 0){
			boxMaxSH = 113;
		}
		
        boxTH = $(".boxTitle", this).height();
        if (!boxTH) boxTH = 0;
        $('.boxContentWrapper', this).css("height", boxMaxSH - boxTH + "px");
    });


	//para o caso da intro:
	if($('.introBox').length > 0){
		boxMaxSH = 210;
		
		boxTH = $(".boxTitleIntro", this).height();
        if (!boxTH) boxTH = 0;
        $('.contentContainerInsideBlock', this).css("height", boxMaxSH - boxTH + "px");
	}
	
    //scrollbars (jsScrollPane):
    $('.scroll-pane').jScrollPane({
        showArrows: true,
        verticalDragMinHeight: 20,
        verticalDragMaxHeight: 20
    });

    //---------boxes nav ------------
    navActive = false;

    //Número de caixas sempre visiveis:
    totalVisibleProducts = 3;

    //padding de cada:
    boxPadding = 23;

    //largura de cada:
    productsIndividualWidth = 213 + boxPadding;

    //funções para botões:
    prevActive = 0;
    nextActive = 0;

    //vê quantas caixas existem:
    numberOfProducts = $(".individualBox").length;

    startX = 0;
    hiddenAreas = numberOfProducts - totalVisibleProducts;

    //se for maior do que o máximo, activa a seta do seguinte:
    if (numberOfProducts > totalVisibleProducts) {
        navActive = true;
        $(".navButtonRight").show();
        nextActive = 1;
    } else {
        nextActive = 0;
    }

    if (navActive) {

        $(".navButtonRight a").click(function (event) {
            event.preventDefault();
            // o nextActive previne duplos cliques
            if (nextActive) {
                theLeft = parseInt($(".contentBoxWrapper").css("left"), 10);

                nextActive = 0;

                if (theLeft <= -((productsIndividualWidth * (hiddenAreas)) - startX)) {
                    nextActive = 0;
                    showOrHideNextBt("hide");
                } else {

                    showOrHidePrevBt("show");

                    $(".contentBoxWrapper").animate({
                        left: theLeft - productsIndividualWidth
                    },
                    800, function () {
                        // Animation complete.
                        prevActive = 1;
                        nextActive = 1;

                        showOrHidePrevBt("show");

                        if (parseInt($(".contentBoxWrapper").css("left"), 10) <= -((productsIndividualWidth * hiddenAreas) - startX)) {
                            nextActive = 0;
                            showOrHideNextBt("hide");
                        }
                    });
                }

            }
        });

        $(".navButtonLeft a").click(function (event) {
            event.preventDefault();
            // o prevActive previne duplos cliques
            if (prevActive) {
                theLeft = parseInt($(".contentBoxWrapper").css("left"), 10);

                prevActive = 0;

                if (startX >= -(theLeft)) {
                    prevActive = 0;
                    showOrHidePrevBt("hide");
                } else {

                    showOrHideNextBt("show");

                    $(".contentBoxWrapper").animate({
                        left: theLeft + productsIndividualWidth
                    },
                    800, function () {
                        // Animation complete.
                        prevActive = 1;
                        nextActive = 1;

                        showOrHideNextBt("show");

                        if (startX >= -(parseInt($(".contentBoxWrapper").css("left"), 10))) {
                            prevActive = 0;
                            showOrHidePrevBt("hide");
                        }
                    });
                }

            }
        });
    }
	
	
	//input validations:
	jQuery('.numbersOnly').keyup(function () { 
    	this.value = this.value.replace(/[^0-9\.]/g,'');
	});

});

function showOrHidePrevBt(which) {
    switch (which) {
    case "show":
        $(".navButtonLeft").show();
        break;
    case "hide":
        $(".navButtonLeft").hide();
        break;
    }
}

function showOrHideNextBt(which) {
    switch (which) {
    case "show":
        $(".navButtonRight").show();
        break;
    case "hide":
        $(".navButtonRight").hide();
        break;
    }
}

//preload images:
jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src" + arguments[i]);
		
		if(i == arguments.length -1){
			//esconde preload image:
			$(".loadingAnimation").hide();
		}
	}
}

function validateGuestbookForm(){
	//valida os campos: (apenas os obrigatórios):
	formList = [];

	formList[0] = $("#vNome").val();
	formList[1] = $("#vEmail").val();
	formList[2] = $("#vTestemunho").val();
	
	//algum campo vazio:
	showWarning = 0;
	i = formList.length;
	
	while(i>=0){
		if(formList[i] == 0) {
			showWarning = 1;
			break;
		}
		i--
	}
	
	//[tb no caso do email não estar em branco, pode estar mal preechido]
	if(showWarning == 1 || checkInvalidEmail("#vEmail") == 1){
			
		//dependendo da língua:
		theLang = $("#lang").val();
		
		switch(theLang) {
			case "pt":
				alert("Por favor preencha/corrija os campos obrigatórios");
				break;
			case "en":
				alert("Please correct/complete the mandatory fields");
				break;
			case "es":
				alert("Por favor rellene/corrija los campos obligatorios");
				break;
			case "fr":
				alert("S'il vous plaît corriger/compléter les champs obligatoires");
				break;
		}
		
		
	} else {
		
		//submit do form:
		$("#guestbookForm").submit();
	}
}

function validateReservationsForm(){
	//valida os campos: (apenas os obrigatórios):
	formList = [];

	formList[0] = $("#vNome").val();
	formList[1] = $("#vTelefone").val();
	formList[2] = $("#vEmail").val();
	formList[3] = $("#vChegada").val();
	formList[4] = $("#vPartida").val();
	formList[5] = $("#vNAdultos").val();
	formList[6] = $("#vNCriancas").val();
	
	//algum campo vazio:
	showWarning = 0;
	i = formList.length;
	
	
	while(i>=0){
	    if (formList[i] == "") {
	        
			showWarning = 1;
			break;
		}
		i--
	}
	
	
	if(formList[5] == 0){
		showWarning = 1;
	}


	
	
	//[tb no caso do email não estar em branco, pode estar mal preechido]
	if(showWarning == 1 || checkInvalidEmail("#vEmail") == 1){
			
		//dependendo da língua:
		theLang = $("#lang").val();
		
		switch(theLang) {
			case "pt":
				alert("Por favor preencha/corrija os campos obrigatórios");
				break;
			case "en":
				alert("Please correct/complete the mandatory fields");
				break;
			case "es":
				alert("Por favor rellene/corrija los campos obligatorios");
				break;
			case "fr":
				alert("S'il vous plaît corriger/compléter les champs obligatoires");
				break;
		}
		
		
	} else {
		
		//submit do form:
		$("#reservationsForm").submit();
	}
}

function validateVoucherForm(){
	//valida os campos: (apenas os obrigatórios):
	formList = [];

	formList[0] = $("#vNome").val();
	formList[1] = $("#vTelefone").val();
	formList[2] = $("#vEmail").val();
	formList[3] = $("#vNome2").val();
	formList[4] = $("#vTelefone2").val();
	formList[5] = $("#vEmail2").val();
	
	//algum campo vazio:
	showWarning = 0;
	i = formList.length;
	
	while(i>=0){
		if(formList[i] == 0) {
			showWarning = 1;
			break;
		}
		i--
	}
	
	//[tb no caso do email não estar em branco, pode estar mal preechido]
	if(showWarning == 1 || checkInvalidEmail("#vEmail") == 1 || checkInvalidEmail("#vEmail2") == 1){
	//if(showWarning == 1){
		//dependendo da língua:
		theLang = $("#lang").val();
		
		switch(theLang) {
			case "pt":
				alert("Por favor preencha/corrija os campos obrigatórios");
				break;
			case "en":
				alert("Please correct/complete the mandatory fields");
				break;
			case "es":
				alert("Por favor rellene/corrija los campos obligatorios");
				break;
			case "fr":
				alert("S'il vous plaît corriger/compléter les champs obligatoires");
				break;
		}
		
		
	} else {
	    //mostra opções:
	    //alert($("#vEmail").val());
		$(".formVoucher1").hide();
		$(".formVoucher2").removeClass("displayNone").show();
		
		//resetVoucherOptionsForm();
		
	}
}

function validateVoucherFormFinal(){
	//valida os campos: (apenas os obrigatórios):
    //alert($("#vEmail").val());
	theLang = $("#lang").val();
		
	showWarning = 0;
	
	if($("#voucherOpcoes1").attr("checked") == true) {
		//console.log("1");
		formList = [];
	
		formList[0] = $("#vNoitesSemana").val();
		formList[1] = $("#vNoitesFDS").val();
		
		
		i = formList.length;
		
		while(i>=0){
			if(formList[i] == 0) {
				showWarning = 1;
				break;
			}
			i--
		}
		
		if($("#vTipologia").val() == "0"){
			showWarning = 1;
		}
		
	
	}
	
	if($("#voucherOpcoes2").attr("checked") == true) {
		
		formList = [];
	
		formList[0] = $("#vValor").val();
		
		
		//algum campo vazio:
		i = formList.length;
		
		while(i>=0){
			if(formList[i] == 0) {
				showWarning = 1;
				break;
			}
			i--
		}
	
	}
	
	if(showWarning == 1){
			
		//dependendo da língua:
		
		switch(theLang) {
			case "pt":
				alert("Por favor preencha/corrija os campos obrigatórios");
				break;
			case "en":
				alert("Please correct/complete the mandatory fields");
				break;
			case "es":
				alert("Por favor rellene/corrija los campos obligatorios");
				break;
			case "fr":
				alert("S'il vous plaît corriger/compléter les champs obligatoires");
				break;
		}
		
		
	} else {
		
		
		//submit do form:
		$("#voucherForm").submit();
	}
}

function resetReservationsForm(){
	$(".formElements input, .formElements textarea").val("");
}

function showOption1(){
	$(".voucherOptions2").hide();
	$(".voucherOptions1").removeClass("displayNone").show();
}
function showOption2(){
	$(".voucherOptions1").hide();
	$(".voucherOptions2").removeClass("displayNone").show();
}
function resetVoucherOptionsForm(){
	$(".formElements input, .formElements textarea").val("");
	showOption1();
	$("#voucherOpcoes1").attr("checked", "checked");
	$("#voucherOpcoes2").attr("checked", "");
	$("select").val("0");
}

function checkInvalidEmail(thefield){
	invalidEmail = 0;
	AtPos = $(thefield).val().indexOf("@");
	StopPos = $(thefield).val().lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1) {
		invalidEmail = 1;
	}

	else if (StopPos < AtPos) {
		invalidEmail = 1;
	}

	else if (StopPos - AtPos == 1) {
		invalidEmail = 1;
	}
	return invalidEmail;
}
