$("*").each(function() {
	if ($(this).css("float") !== "none") {
		$(this).css("display", "inline");
	}
});

$(document).ready(function() {

	// Voltar
	$(".bt-back").click(function(){ history.back(); });

	// Imprimir
	$(".bt-imprimir").click(function(){ window.print(); });
	$(".box-legenda-foto").css("opacity","0.9");
	$(".legenda-foto-dest").css("opacity","0.9");

	// Lightbox
	if ( typeof $.fn.lightBox != "undefined") { $(".lightbox-active").lightBox(); }
	$(".box-downloads .conteudo-downloads:odd").addClass("odd");
	
	$(".lista-busca li:odd").addClass("pares");

	// Content
	$("#hivesliderContent li:first-child").addClass("disapear");

	// Flash principal topo
	$(".flash").flash({
	    src: "swf/header.swf",
	    width: '100%',
	    height: 230,
		wmode: "transparent",
		expressInstall: true
	});

	// Cycle
	$(".box-destaques-lista ul").cycle({
		fx: "scrollHorz",
		prev: ".anterior",
		speed: 700,
		next: ".proxima",
		timeout: 6000
	});

	// Cycle
	$(".destacao").cycle({
	fx: "scrollHorz",
	speedIn: 900,
	prev: ".bt-esquerda",
	next: ".bt-direita",
	timeout: 0
	});

	// Servicos
	$('.servicos-lista .servico').click(function(){
		if ($(this).hasClass('servico-active')) {
			$('.servicos-lista .servico').removeClass('servico-active');
			$('#'+$(this).attr('href')).slideUp(250);
		} else {
			$('.servicos-lista .servico').removeClass('servico-active');
			$(this).addClass('servico-active');
			$('.servico-desc').slideUp(250);
			$('#'+$(this).attr('href')).slideDown(500);
		}
		return false;
	});

	// Cursor
	$("input[type=submit]").css("cursor", "pointer");
	$("input[type=button]").css("cursor", "pointer");

	// Externo
	$("a[rel=external]").attr("target", "_blank");
	$("a[rel=external nofollow]").attr("target", "_blank");
	$("a[rel=nofollow external]").attr("target", "_blank");

	// Mascaras formularios
	if (typeof $.fn.mask != "undefined") {
		$(".masc_telefone").mask("(99)9999-9999");
		$(".masc_cep").mask("99999-999");
		$(".masc_data").mask("99/99/9999");
		$(".masc_hora").mask("99:99");
		$(".masc_cpf").mask("999.999.999-99");
	}

	// Box
	$('.box-imgs-menores ul li a').hover(function(){
		$(this).css("opacity","0.8");
	},function(){
		$(this).css("opacity","1");
	});

	// Imagens
	$(".thumbs-imagens li a").click(function(){
		if($(".thumbs-imagens li a").hasClass("active")) {
			$(".thumbs-imagens li a").removeClass("active");
			$(this).addClass("active");
		} else {
			$(".thumbs-imagens li a").removeClass("active");
			$(this).addClass("active");
		}
		var agarefi = $(this).attr("href");
		$("#box-seta").removeClass();
		$("#box-seta").addClass("posicao-seta" + agarefi);
		var imgaddr = $(this).find(":first-child").attr("src");
		$("#imagem-grande").attr("src",imgaddr);
		return false;
	});

	// Calendário
	$("#datepicker").datepicker({
		showOn: 'button',
		buttonImage: 'img/bt-calendario.png',
		buttonImageOnly: true,
		dateFormat: 'dd/mm/yy',
		dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
		monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'],
		monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro']
	});
	
	var position = $(".link-para-calendario").offset();
	$("#div-calendario").css(position);
	
	$(".listagem-agenda ul li .info").hover(function(){
		$(this).css("background-position","0px -86px");
	},function(){
		$(this).css("background-position","0px 0px");
	});
	
});

/**
  * 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 "";
	}
}

/**
  * 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 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");
		if ((relObj == "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;
			}
		} else if (relObj == "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;
			}
		} else if (relObj == "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;
			}
		} else if (relObj == "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;
			}
		}
    }
    return true;
}

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

/**
  * Função validar
  * @param nada
  * @return nada
  **/
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) == "1") {
					alert("Mensagem enviada com sucesso!");
					document.getElementById(form).reset();
				} else {
					alert("Não foi possível enviar sua mensagem!");
				}
			}
		});
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarCadastro(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-cadastro.php",
			data: str,
			success: function(txt) {
				/**
				   * 0 - Não foi possível realizar o cadastro, tente novamente.
				   * 1 - CPF já cadastrado.
				   * 2 - E-mail já cadastrado.
				   * 3 - Cadastro alterado com sucesso.
				   * 4 - Cadastro realizado com sucesso.
				   **/
				if (getTrim(txt) == "0") {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("CPF já cadastrado.");
				} else if (getTrim(txt) == "2") {
					alert("E-mail já cadastrado.");
				} else if (getTrim(txt) == "3") {
					alert("Cadastro alterado com sucesso.");
					document.getElementById(form).reset();
					$.facebox.close();
				} else if (getTrim(txt) == "4") {
					alert("Cadastro realizado com sucesso.");
					window.location = "index.php?cmd=promocoes";
				} else {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarLogin(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-login.php",
			data: str,
			success: function(txt) {
				/**
				   * 0 - E-mail ou senha inválido!
				   * 1 - Login realizado com sucesso!
				   **/
				if (getTrim(txt) == "0") {
					alert("E-mail ou senha inválido!");
				} else if (getTrim(txt) == "1") {
					if ($("#log_url").val() != "") {
						window.location = $("#log_url").val();
					} else {
						window.location = "index.php?cmd=promocoes";
					}
				} else {
					alert("Não foi possível realizar o login, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarNewsletter(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-newsletter.php",
			data: str,
			success: function(txt) {
				/**
				   * 0 - Não foi possível realizar o cadastro, tente novamente.
				   * 1 - Cadastro realizado com sucesso.
				   * 2 - E-mail já cadastrado, tente novamente.
				   **/
				if (getTrim(txt) == "0") {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Cadastro realizado com sucesso.");
					document.getElementById(form).reset();
					$.facebox.close();
				} else if (getTrim(txt) == "2") {
					alert("E-mail já cadastrado, tente novamente.");
				} else {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função listagem de thumbs
  * @param id Identificador
  * @return nada
  **/
function listagemGaleriasThumbs(id, foto) {
	$.ajax({
		type: "POST",
		url: "lib/acao-form-galerias-thumbs.php",
		data: "id=" + id + "&foto=" + foto,
		success: function(txt) {
			arr = txt.split("<!-- SEPARADOR -->");
			listagem = arr[0];
			imagem = arr[1];
			if (getTrim(listagem)) {
				$("#div-listagem-thumbs").html(listagem);
			}
			if (getTrim(imagem)) {
				$("#div-imagem").html(imagem);
			}
		}
	});
}

/**
  * Função listagem de thumbs
  * @param id Identificador
  * @return nada
  **/
function listagemGaleriasThumbsFacebox(id, foto, email) {
	$.ajax({
		type: "POST",
		url: "lib/acao-form-galerias-thumbs-facebox.php",
		data: "id=" + id + "&foto=" + foto + "&email=" + email,		
		success: function(txt) {
			arr = txt.split("<!-- SEPARADOR -->");
			faceboxx = arr[0];
			btanterior = arr[1];
			btproximo = arr[2];
			if (getTrim(faceboxx)) {
				$("#div-imagem-facebox").html(faceboxx);
			}			
			if (getTrim(btanterior)) {
				$("#ant-identificador").attr("href", btanterior);
				$("#ant-identificador").attr("style", "visibility:visible;");
			} else {
				$("#ant-identificador").attr("style", "visibility:hidden;");
			}
			if (getTrim(btproximo)) {
				$("#pro-identificador").attr("href", btproximo);
				$("#pro-identificador").attr("style", "visibility:visible;");
			} else {
				$("#pro-identificador").attr("style", "visibility:hidden;");
			}
		}
	});
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarEnviaFoto(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-foto.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == "1") {
					alert("Mensagem enviada com sucesso!");
					document.getElementById(form).reset();
				} else {
					alert("Não foi possível enviar sua mensagem!");
				}
			}
		});
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarQueroParticipar(id) {	
	$.ajax({
		type: "POST",
		url: "lib/acao-form-quero-participar.php",
		data: "id=" + id,
		success: function(txt) {
			/**
			   * 0 - Não foi possível realizar o cadastro, tente novamente.
			   * 1 - Cadastro realizado com sucesso.
			   * 2 - Você já está participando desta promoção.
			   * 3 - Você não está logado, tente novamente.
			   **/
			if (getTrim(txt) == "0") {	
				alert("Não foi possível realizar o cadastro, tente novamente.");
			} else if (getTrim(txt) == "1") {
				alert("Cadastro realizado com sucesso.");
			} else if (getTrim(txt) == "2") {
				alert("Você já está participando desta promoção.");
			} else if (getTrim(txt) == "3") {
				alert("Você não está logado, tente novamente.");
			} else {
				alert("Não foi possível realizar o cadastro, tente novamente.");
			}
		}
	});	
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarQueroParticiparCadastro(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-quero-participar-cadastro.php",
			data: str,
			success: function(txt) {
				/**
				   * 0 - Não foi possível realizar o cadastro, tente novamente.
				   * 1 - CPF já cadastrado.
				   * 2 - E-mail já cadastrado.
				   * 3 - Cadastro realizado com sucesso.
				   * 4 - Você já está participando desta promoção.
				   * 5 - Promoção não encontrada.
				   **/
				if (getTrim(txt) == "0") {	
					alert("Não foi possível realizar o cadastro, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("CPF já cadastrado.");
				} else if (getTrim(txt) == "2") {
					alert("E-mail já cadastrado.");
				} else if (getTrim(txt) == "3") {
					alert("Cadastro realizado com sucesso.");
					window.location = "index.php?cmd=promocoes&id=" + $("#cad_promocao").val();
				} else if (getTrim(txt) == "4") {
					alert("Você já está participando desta promoção.");
				} else if (getTrim(txt) == "5") {
					alert("Promoção não encontrada.");
				} else {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarQueroParticiparLogin(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-quero-participar-login.php",
			data: str,
			success: function(txt) {
				/**
				   * 0 - Não foi possível realizar o cadastro, tente novamente.
				   * 1 - Cadastro realizado com sucesso.
				   * 2 - Você já está participando desta promoção.
				   * 3 - E-mail ou senha inválido.
				   * 4 - Promoção não encontrada.
				   **/
				if (getTrim(txt) == "0") {	
					alert("Não foi possível realizar o cadastro, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Cadastro realizado com sucesso.");
					window.location = "index.php?cmd=promocoes&id=" + $("#lem_promocao").val();
				} else if (getTrim(txt) == "2") {
					alert("Você já está participando desta promoção.");
				} else if (getTrim(txt) == "3") {
					alert("E-mail ou senha inválido.");					
				} else if (getTrim(txt) == "4") {
					alert("Promoção não encontrada.");
				} else {
					alert("Não foi possível realizar o cadastro, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarComentario(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-comentario.php",
			data: str,
			success: function(txt) {
				/**
				   * 0 - Não foi possível realizar o comentário, tente novamente.
				   * 1 - Cadastro realizado com sucesso.
				   * 2 - Não foi possível realizar o comentário, tente novamente.
				   * 3 - Blog não encontrado.
				   **/
				if (getTrim(txt) == "0") {	
					alert("Não foi possível realizar o comentário, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Cadastro realizado com sucesso.");
					document.getElementById(form).reset();
					$.facebox.close();
				} else if (getTrim(txt) == "2") {
					alert("Não foi possível realizar o comentário, tente novamente.");
				} else if (getTrim(txt) == "3") {
					alert("Blog não encontrado.");
				} else {
					alert("Não foi possível realizar o comentário, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função mostrar calendário
  * @param nada
  * @return nada
  **/
function mostrarCalendario() {
	if($(".link-para-calendario").hasClass("ativado")) {
		$(".link-para-calendario").removeClass("ativado");
		$("#div-calendario").fadeOut();
	} else {
		$(".link-para-calendario").addClass("ativado");
		$("#div-calendario").fadeIn();
	}
}























