$(function() {
	
	// Endereço do tema
	var endereco_do_tema = $("body").attr("rel");

	// Links externos
	$("a[rel=external]").attr("target", "_blank");
	$("a[rel=external nofollow]").attr("target", "_blank");
	$("a[rel=nofollow external]").attr("target", "_blank");
	
	// Submit
	$("input[type=submit]").css("cursor", "pointer");

	// Botão
	$("input[type=button]").css("cursor", "pointer");
	
	// Opacidade
	$(".opac-50").css("opacity", .5);
	$(".opac-90").css("opacity", .9);
	$(".wpcf7-form br").remove();
	
	// Tool tip
	$(function() {
		$(".tip").tipTip({ maxWidth: "auto", edgeOffset: 10 });
	});
	
	// Internas faq
	$("#content-internas ul.lista-faq li").click(function() {
		$("#content-internas ul.lista-faq li.ativado").removeClass("ativado").find("div").slideUp();
		$(this).addClass("ativado").find("div").slideDown();
	});
	
	// Botão enviar
	$("a.submit-button span").mouseover(function() {
		$(this).stop().animate({ opacity: "0" }, { duration: 400 })
	}).mouseout(function() {
		$(this).stop().animate({ opacity: "1" }, { duration: 400 })
	});
	
	// Flash
	$("#banner-top").flash({
	    src: "swf/header.swf",
	    width: 960,
	    height: 370,
		wmode: "transparent",
		expressInstall: true
	});
	
	// Menu	a pousada
	$("#nav ul li.nav-pousada").mouseover(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-471px 0px)" }, { duration: 200 })

	}).mouseout(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-471px 8px)" }, { duration: 200 })
	});
	
	// Menu acomodações
	$("#nav ul li.nav-acomodacoes").mouseover(function() {		
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-763px 0px)" }, { duration: 200 })
	}).mouseout(function() {		
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-763px 8px)" }, { duration: 200 })
	});
	
	// Menu pacotes
	$("#nav ul li.nav-pacotes").mouseover(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-371px 0px)" }, { duration: 200 })
	}).mouseout(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-371px 8px)" }, { duration: 200 })
	});
	
	// Menu serviços
	$("#nav ul li.nav-servicos").mouseover(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-371px 0px)" }, { duration: 200 })
	}).mouseout(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-371px 8px)" }, { duration: 200 })
	});
	
	// Menu faq
	$("#nav ul li.nav-faq").mouseover(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-578px 0px)" }, { duration: 200 })
	}).mouseout(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-578px 8px)" }, { duration: 200 })
	});
	
	// Menu localizacao
	$("#nav ul li.nav-localizacao").mouseover(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-84px 0px)" }, { duration: 200 })
	}).mouseout(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-84px 8px)" }, { duration: 200 })
	});
	
	// Menu reservas
	$("#nav ul li.nav-reservas").mouseover(function() {		
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-371px 0px)" }, { duration: 200 })
	}).mouseout(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-371px 8px)" }, { duration: 200 })
	});
	
	// Menu contato
	$("#nav ul li.nav-contato").mouseover(function() {
		$(this).find("span.unsel").stop().animate({ opacity: "0.9", backgroundPosition: "(-763px 0px)" }, { duration: 200 })
	}).mouseout(function() {	
		$(this).find("span.unsel").stop().animate({ opacity: "0.5", backgroundPosition: "(-763px 8px)" }, { duration: 200 })
	});

	// Sliders
	$("#banner-interna").nivoSlider({
		effect: "sliceUpDown", //Specify sets like: "fold,fade,sliceDown"
		slices: 10,
		animSpeed: 500, //Slide transition speed
		pauseTime: 5000,
		startSlide: 0, //Set starting Slide (0 index)
		directionNav: false, //Next & Prev
		directionNavHide: false, //Only show on hover
		controlNav: false, //1,2,3...
		controlNavThumbs: false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel: false, //Use image rel for thumbs
		keyboardNav: false, //Use left & right arrows
		pauseOnHover: false, //Stop animation while hovering
		manualAdvance: false, //Force manual transitions
		captionOpacity: 0.8
	});

	// Mascara
	$(".masc_telefone").mask("(99)9999-9999");
	$(".masc_data").mask("99/99/9999");
	
	$(document).pngFix(); 
	
});

/**
  * Função limpa espaço
  * @param str String
  * @return nada
  **/
function getTrim(str) {
	if(typeof(str) !== "undefined"){
		return str.replace(/^\s+|\s+$/g, "");
	}else{
		return "";
	}
}

/**
  * Formata número
  * @param $campo int id
  * @param $event Evento
  * @return String
  **/
function formataNumero(campo, e) {
	t = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(t)) || (t == 32)) && (t != 8 && t != 0)) {
		return false;
	}
}

/**
  * Função seleciona os checkbox
  * @param name Nome do input
  * @param checked Verifica se está selecionado
  * @return nada
  **/
function selecinaCampos(name,checked) {
	var elements = document.getElementsByTagName("input");
	for(i = 0; i < elements.length; i++) {
		if(elements[i].type == "checkbox" && elements[i].name.indexOf(name) != -1) {
			if (!checked) {
				elements[i].checked = "";
			} else {
				elements[i].checked = "checked";
			}
		}
	}
}

/**
  * Função que valida a data
  * @param String $value data
  * @return boolean
  **/
function validaData(campo) {
    var valor = campo.split("/");
    if(valor[0] != "" && valor[1] != "" && valor[2] != "") {
        data = new Date(valor[2], valor[1]-1, valor[0]);
        if (valor[2] != data.getFullYear() || valor[1] !=  data.getMonth()+1 || valor[0] != data.getDate() || valor[2] < 1902 || valor[2] > 2037) {
            return false;
        }
    }
    return true;
}

/**
  * Função que valida CPF
  * @param String $s CPF
  * @return boolean
  **/
function validaCpf(s) {
	if(s != "") {
		var cpf = s.replace(/\D/g, "");
		if (
			cpf == "00000000000" ||
			cpf == "11111111111" ||
			cpf == "22222222222" ||
			cpf == "33333333333" ||
			cpf == "44444444444" ||
			cpf == "55555555555" ||
			cpf == "66666666666" ||
			cpf == "77777777777" ||
			cpf == "88888888888" ||
			cpf == "99999999999" ||
			cpf.length != 11)
		{
			return false;
		}

		var soma = 0;
		for ( var i = 10 ; i > 1; i--) {
			soma += cpf.charAt(10 - i) * i;
		}
		d1 = (soma % 11 < 2) ? 0 : (11 - (soma % 11));
		if (d1 != cpf.charAt(9)) {
			return false;
		}
		soma = 0;
		for ($i=11 ; $i>1; $i--) {
			soma += cpf.charAt(11 - i) * i;
		}
		d2 = (soma % 11 < 2) ? 0 : (11 - (soma % 11) );
		if (d2 != cpf.charAt(10)) {
			return false;
		}
		return true;
	}
}

/**
  * Função chaca o valor do radio
  * @param String $radioObj
  * @return boolean
  **/
function getCheckedValue(radioObj) {
	if (!radioObj) {
		return "";
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined) {
		if (radioObj.checked) {
			return radioObj.value;
		} else {
			return "";
		}
	}
	for (var i = 0; i < radioLength; i++) {
		if (radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

/**
  * Função Validar Formulário
  * @param form Identificador do form
  * @return Boolean
  **/
function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        var nomeObj = document.getElementById(form).elements[i].name;
        var idObj 	= document.getElementById(form).elements[i].id;
		var relObj 	= document.getElementById(form).elements[i].getAttribute("rel");
		switch (relObj) {
			case "email":
				expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
				if (!expressao.test(document.getElementById(form).elements[i].value)) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].value = "";
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "required":
				if ((document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "cpf":
				if ((!validaCpf(document.getElementById(form).elements[i].value)) || (document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].value = "";
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "data":
				if ((!validaData(document.getElementById(form).elements[i].value)) || (document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].value = "";
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "radio":
			case "checkbox":
				if(getCheckedValue(document.getElementsByName(nomeObj))) {
				} else {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			default:
			break;
		}
    }
    return true;
}

/**
  * Função monta caixa de seleção
  * @param div DIV
  * @param id Identificador
  * @return nada
  **/
function montaCaixa(div, id, padrao) {
	$.ajax({
		type: "POST",
		url: "lib/acao-monta-caixa.php",
		data: "div=" + div + "&id=" + id + "&padrao=" + padrao,
		success: function(txt) {
			$("#" + div).html(txt);
		}
	});
}

/**
  * Função validar contato
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarContato(form) {
	if (validaForm(form)) {		
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-contato.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == "0") {
					alert("Não foi possível enviar a mensagem, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Mensagem enviada com sucesso.");
					document.getElementById(form).reset();
				} else {
					alert("Não foi possível enviar a mensagem, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar reserva
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarReserva(form) {
	if (validaForm(form)) {		
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-reserva.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == "0") {
					alert("Não foi possível enviar a mensagem, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Mensagem enviada com sucesso.");
					document.getElementById(form).reset();
				} else {
					alert("Não foi possível enviar a mensagem, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar newsletter
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarNewsletter(form) {
	if (validaForm(form)) {		
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-newsletter.php",
			data: str,
			success: function(txt) {
				arr = txt.split("<!-- SEPARADOR -->");
				if (getTrim(arr[0]) == "0") {
					alert("E-mail já cadastrado.");
				} else if (getTrim(arr[0]) == "1") {
					alert("E-mail cadastrado com sucesso.");
					document.getElementById(form).reset();
				} else {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar depoimento
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarDepoimento(form) {
	if (validaForm(form)) {		
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-depoimento.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == "0") {
					alert("Não foi possível enviar a mensagem, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Mensagem enviada com sucesso.");
					document.getElementById(form).reset();
				} else {
					alert("Não foi possível enviar a mensagem, tente novamente.");
				}
			}
		});
	}
}
