/* ---// FONTES //--- */
IncludeJavaScript("scripts/mainstage.js"); // Destaques
IncludeJavaScript("scripts/cufon.js"); // Cufon
IncludeJavaScript("scripts/fontes.js"); // Cufon
IncludeJavaScript("scripts/jquery.lightbox.js"); // Lightbox
IncludeJavaScript("scripts/jquery.facebox.js"); // Facebox
IncludeJavaScript("scripts/ezmaps.js"); // Maps
IncludeJavaScript("scripts/jquery.scrollto.js"); // Smooth Scroll na tela
IncludeJavaScript("scripts/jquery.ui.js"); // jQuery UI completo
IncludeJavaScript("scripts/jquery.cycle.js"); // jQuery Cycle
IncludeJavaScript("scripts/jquery.flash.js"); // Flash
IncludeJavaScript("scripts/jquery.easing.js"); // Easing
IncludeJavaScript("scripts/jquery.maskedinput.js"); // Máscaras para formulários

$(function() {

	Cufon.replace('.helv87', { fontFamily: 'Helvs87', hover: true });
	Cufon.replace('.helv47', { fontFamily: 'Helvs47', hover: true });
	Cufon.replace('.helv77', { fontFamily: 'Helvs77', hover: true });
	Cufon.replace('.fffu', { fontFamily: 'FFFU', hover: true });
	Cufon.replace('.helv87s', { fontFamily: 'Helvs87', textShadow: '1px 1px #004e56', hover: true });
	Cufon.replace('.helv87sg', { fontFamily: 'Helvs87', textShadow: '1px 1px #000', color: '-linear-gradient(#FFF,#e1e1e1)', hover: true });
	Cufon.replace('.helv87sg2', { fontFamily: 'Helvs87', textShadow: '1px 1px #000', color: '-linear-gradient(#7faaca,#FFF)', hover: true });
	Cufon.replace('.helv47s', { fontFamily: 'Helvs47', textShadow: '1px 1px #004e56',hover: true });
	Cufon.replace('.fffus', { fontFamily: 'FFFU', textShadow: '1px 1px #000', hover: true });
	if (typeof $.fn.cycle != "undefined") {
		
		$(".pessoas-ajudar").cycle({
			cleartype:  1,
			cleartypeNoBg: true
		});
		
		$(".pagina-mensagens #depoimentos").cycle({
			cleartype:  1,
			cleartypeNoBg: true
		}); 
		
	}
	// if (typeof $.fn.cycle != "undefined") { $("ul.cycle").cycle({ fx: "scrollHorz", prev: ".arrow-left", next: ".arrow-right", timeout: 0 }); }
	// if (typeof $.fn.flash != "undefined") { $("div.classe-flash").flash({ src: "swf/preloader.swf", width: '100%', height: 650, mode: "transparent", expressInstall: true }); }
	// if (typeof $.fn.mask != "undefined") { $(".masc_telefone").mask("(99)9999-9999"); $(".masc_cpf").mask("999.999.999-99"); $(".masc_data").mask("99/99/9999"); $(".masc_cep").mask("99999-999"); }

	// LINKS EXTERNOS
	$("a[rel*=external]").attr('target','_blank');
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");
	$(".bt-voltar").click(function(){ history.back(); return false; });

	// TRANSPARENCIA
	$(".opac").css("opacity","0.5");

	$("#nav ul li a").css("opacity","0.9");
	$("#nav ul li a").hover(function(){
		$(this).animate({ paddingTop: 10, opacity: 1, height:45 }, 200);
		setTimeout(function() {
			$("#q").focus();
		}, 1000);
	},function(){
		$(this).animate({ paddingTop: 18, opacity: 0.9 }, 200);
	});

		setTimeout(function() {
			Cufon.refresh();
		}, 1500);
		
	$('.busca-bt').click(function() {
		if($(this).hasClass("ativadoBusca")) {
			$("#busca").hide( 'slide', {direction: 'right'}, 800 );
			$(this).removeClass("ativadoBusca");
		} else {
			$("#busca").show( 'slide', {direction: 'right'}, 800 );
			$(this).addClass("ativadoBusca");
		}
	});
	
	var pegaAlturaConteudo = $("#conteudo-meio").height()
	$(".sidebar").css("height", pegaAlturaConteudo - 10);


	$('.bubble').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 500;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var popup = $('.popup', this).css('opacity', 0);
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				popup.css({ top: -30, left: -30, display: 'block' }).animate({ marginTop: '-=' + distance + 'px', opacity: 1 }, time, 'swing', function() { beingShown = false; shown = true; });
			}
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({ marginTop: '-=' + distance + 'px', opacity: 0 }, time, 'swing', function () { shown = false; popup.css('display', 'none'); popup.css('margin-top', '0px'); });
			}, hideDelay);
		});
	});



	$('.bubble2').each(function () {
		var distance2 = 10;
		var time2 = 500;
		var hideDelay2 = 500;
		var hideDelayTimer2 = null;
		var beingShown2 = false;
		var shown2 = false;
		var trigger2 = $('.trigger2', this);
		//var posTrigger2 = $(this).children('.trigger2').offset();
		var popup2 = $('.popup2', this).css('opacity', 0);
		$([trigger2.get(0), popup2.get(0)]).mouseover(function () {
			if (hideDelayTimer2) clearTimeout(hideDelayTimer2);
			if (beingShown2 || shown2) {
				return;
			} else {
				//alert(posTrigger.top);
				beingShown2 = true;
				var posTop = $(this).offset().top+30;
				var posLeft = $(this).offset().left-32;
				popup2.css({ top: posTop, left: posLeft, display: 'block' }).animate({ marginTop: '+=' + distance2 + 'px', opacity: 1 }, time2, 'swing', function() { beingShown2 = false; shown2 = true; });
			}
		}).mouseout(function () {
			if (hideDelayTimer2) clearTimeout(hideDelayTimer2);
			hideDelayTimer2 = setTimeout(function () {
				hideDelayTimer2 = null;
				popup2.animate({ marginTop: '+=' + distance2 + 'px', opacity: 0 }, time2, 'swing', function () { shown2 = false; popup2.css('display', 'none'); popup2.css('margin-top', '0px'); });
			}, hideDelay2);
		});
	});

	var input = document.getElementsByTagName('input');
	for (var a=input.length-1; a >= 0; a--) {
		if (input[a].type != 'text') { continue; }
		input[a].mask = input[a].value;
		input[a].onfocus = function() {
			if (this.value == this.mask) { this.value = ''; }
		};
		input[a].onblur = function() {
			if (this.value == '') { this.value = this.mask; }
		};
	}

	// Botão
	$("button").css("cursor", "pointer");

	// Aspas
	$('.text-aspas').before('<span class="aspa-inicio"><span class="none">&quot;</span></span>');
	$('.text-aspas').after('<span class="aspa-final"><span class="none">&quot;</span></span>');

	// Mascaras formularios
	if (typeof $.fn.mask != "undefined") {
		$(".masc_telefone").mask("(99) 9999-9999");
	}
});

/**
  * 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 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 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 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) {
				arr = txt.split("<!-- SEPARADOR -->");
				if (getTrim(arr[0]) == "1") {
					alert(arr[1]);
					document.getElementById(form).reset();
				} else {
					alert(arr[1]);
				}
			}
		});
	}
}

/**
  * 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]) == "1") {
					alert(arr[1]);
					document.getElementById(form).reset();
				} else {
					alert(arr[1]);
				}
			}
		});
	}
}
/**
  * Função Habilita/Desabilita Submit
  * @param form Identificador do form
  * @return 
  **/
function habilitaDesabilitaSubmit(form,parametro) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        var idObj 	= document.getElementById(form).elements[i].id;
        var tipObj 	= document.getElementById(form).elements[i].type;
		switch (tipObj) {
			case "submit":
				switch(parametro){
					case 'disabled':
						if(document.getElementById(form).elements[i].disabled){
							return true;
						}else{
							return false;
						}
					break;
					default:
						document.getElementById(form).elements[i].disabled = parametro;
					break;
				}
			break;
			default:
			break;
		}
    }
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarComPost(form) {
	validarComPostFacebox(form,false);
}

/**
  * Função validar
  * @param form: id do form
  * @return nada
  **/
function validarComPostFacebox(form,fechar) {
	if (validaForm(form)) {
		if(habilitaDesabilitaSubmit(form,'disabled')){ 
			//Submit está desabilitado
			//alert('Aguarde o término do processo. Obrigado!'); 
		}else{ 
			//Submit está habilitado
			habilitaDesabilitaSubmit(form,true); 
 
			var str = $("#" + form).serialize();
			$.ajax({
				type: "POST",
				url: "lib/acao-" + form + ".php",
				data: str,
				beforeSend: function() {
				},
				success: function(txt) {
					arr = txt.split("<!-- SEPARADOR -->");
					switch (getTrim(arr[0])){
						case "0":
						case "false":
							alert(arr[1]);
						break;
						case "1":
						case "ok":
						case "true":
							alert(arr[1]);
							document.getElementById(form).reset();
							if(fechar){
								if (typeof $.facebox != "undefined") {
									$.facebox.close();
								}
								if (typeof $.facebox2 != "undefined") {
									$.facebox2.close();
								}
							}
						break;
						default:
							alert(txt);
						break;
					}
					habilitaDesabilitaSubmit(form,false);
				},
				error: function(txt) {
					habilitaDesabilitaSubmit(form,false);
					alert("Erro: " + txt);
				}
			});
		}
	}
}

