$(window).load(function(){
	
	// Cufon
	Cufon.replace('.swiss-1', { 
		fontFamily: 'Swis721 BlkCn BT'
	});	
	Cufon.replace('.swiss-2', { 
		fontFamily: 'Swis721 BlkCn BT',
		color: '-linear-gradient(#FFF, #e2e2e2)',
		textShadow: '#a2a2a2 1px 2px'
	});	
	Cufon.replace('.swiss-bd', { 
		fontFamily: 'Swis721 BdRnd BT'
	});	
	Cufon.replace('.swiss-hv', { 
		fontFamily: 'Swis721 Hv BT'
	});
	
	// Adiciona classe no primeiro e no ultimo item do menu lateral nos produtos
	
	
	var numItems = $('.bg-tabs li').length
	if(numItems > 2) {
		$(".bg-tabs li:first").addClass("prim");
		$(".bg-tabs li:last").addClass("ultm");
	} else {
		$(".bg-tabs li").addClass("unico");
	}
	
	// Links externos
	$("a[rel=external]").attr("target", "_blank");
	$("a[rel=external nofollow]").attr("target", "_blank");
	$("input[type=submit]").css("cursor", "pointer");
	$("input[type=button]").css("cursor", "pointer");
	$(".bt-voltar").click(function(){ history.back(); });
	$(".opac").css({opacity:"0.7"});
	
	// Flash pagina interna topo
	$("#flash-home").flash({
	    src: "swf/preloader.swf",
	    width: 960,
	    height: 340,
		wmode: "transparent",
		expressInstall: true
	});
	
	// Flash pagina interna topo
	$(".flash-paginas").flash({
	    src: "swf/nav.swf",
	    width: 940,
	    height: 210,
		wmode: "transparent",
		expressInstall: true
	});
	
	// Listagem
	$('.box-info a.bg-tabs-carac, .box-info a.bg-list').click(function(){
		if ($(this).hasClass('lista-active')) {
			$('.box-info a').removeClass('lista-active');
			$($(this).attr('href')).slideUp(250);
		} else {
			$('.box-info a').removeClass('lista-active');
			$(this).addClass('lista-active');
			$('.lista-desc').slideUp(250);
			$($(this).attr('href')).slideDown(150);
		}
		return false;
	});
	
	//Cycle-Produtos
	/*
	$(".troca-img, .banneresq, .bannerdir").cycle({
		timeout: 3000,
		cleartypeNoBg: true,
		cleartype: true
	});
	*/
	$(".troca-img").cycle({
		timeout: 3000,
		cleartypeNoBg: true,
		cleartype: true
	});
	
	
	
	
	// Cycle-Lista-Galeria
	if (typeof $.fn.cycle != "undefined") {
		$(".lista-galeria ul").cycle({
		    fx: "scrollHorz",
		    prev: ".bt-anterior",
		    next: ".bt-proximo",
		    timeout: 0
		});
	}
	
	if (typeof $.fn.cycle != "undefined") {
		$(".lista-galerias ul").cycle({
		    fx: "scrollHorz",
		    prev: ".bt-anteriors",
		    next: ".bt-proximos",
		    timeout: 0
		});
	}
	
	// Tabs
	$('.bg-tabs ul li.bt-escolhe a').click(function(){
		if ($('.bg-tabs ul li.bt-escolhe a').hasClass("sel")) {
			$('.bg-tabs ul li.bt-escolhe a').removeClass("sel");
			$(this).addClass("sel");
		} else {
			$(this).addClass("sel");
		}
		var agaref = $(this).attr("href");
		if($(agaref).hasClass('tab-aberto')){
		} else {
			$('.box-escolhe').removeClass('tab-aberto');
			$(agaref).addClass('tab-aberto');
			$('.box-escolhe').slideUp();
			$(agaref).slideDown();
		}
		return false;
	});	

	// 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 "";
	}
}

/**
  * 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 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) {
	$.ajax({
		type: "POST",
		url: "lib/acao-monta-caixa.php",
		data: "div=" + div + "&id=" + id,
		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)) {
		if ($("#con_email").val() != $("#con_email_confirme").val()) {
			alert("Confirme seu e-mail corretamente.");
			$("#con_email_confirme").focus()
			return false;
		}
		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 sua mensagem, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Mensagem enviada com sucesso.");
					document.getElementById(form).reset();
				} else if (getTrim(txt) == "2") {
					alert("Código de segurança inválido.");
				} else {
					alert("Não foi possível enviar sua mensagem, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar revenda
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarRevenda(form) {
	if (validaForm(form)) {
		if ($("#rev_email").val() != $("#rev_email_confirme").val()) {
			alert("Confirme seu e-mail corretamente.");
			$("#rev_email_confirme").focus()
			return false;
		}
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-revenda.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == "0") {
					alert("Não foi possível enviar sua mensagem, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Mensagem enviada com sucesso.");
					document.getElementById(form).reset();
				} else if (getTrim(txt) == "2") {
					alert("Código de segurança inválido.");
				} else {
					alert("Não foi possível enviar sua mensagem, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar solicitação
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarSolicite(form) {
	if (validaForm(form)) {
		if ($("#sol_email").val() != $("#sol_email_confirme").val()) {
			alert("Confirme seu e-mail corretamente.");
			$("#sol_email_confirme").focus()
			return false;
		}
		if ($("#sol_sistema").val() == "1") {
			if ($("#sol_versao").val() == "") {
				alert("Digite a versão");
				$("#sol_versao").focus()
				return false;
			}
		}
		$(document).ready(function() {
			$("#" + form).ajaxForm(function(txt) {				
				if (getTrim(txt) == "0") {
					alert("Não foi possível enviar a solicitação, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Solicitação enviada com sucesso.");
					document.getElementById(form).reset();
					$.facebox.close();
				} else if (getTrim(txt) == "2") {
					alert("Código de segurança inválido.");
				} else {
					alert("Não foi possível enviar a solicitação, tente novamente.");
				}
			});
		});
	}
}

/**
  * Função validar solicitação
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarTrabalhe(form) {
	if (validaForm(form)) {
		if ($("#tra_email").val() != $("#tra_email_confirme").val()) {
			alert("Confirme seu e-mail corretamente.");
			$("#tra_email_confirme").focus()
			return false;
		}
		$(document).ready(function() {
			$("#" + form).ajaxForm(function(txt) {				
				if (getTrim(txt) == "0") {
					alert("Não foi possível enviar o trabalhe conosco, tente novamente.");
				} else if (getTrim(txt) == "1") {
					alert("Formulário enviado com sucesso.");
					document.getElementById(form).reset();
					$.facebox.close();
				} else if (getTrim(txt) == "2") {
					alert("Código de segurança inválido.");
				} else {
					alert("Não foi possível enviar o trabalhe conosco, tente novamente.");
				}
			});
		});
	}
}

/**
  * Função validar busca
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarBusca(form) {
	if (validaForm(form)) {
		document.getElementById(form).submit();
	}
}

/**
  * Função mostrar especialidade
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function mostrarEspecialidade(valor) {
	switch (valor) {	
		case "1":
			$("#div-outra-especialidade").hide();
			$("#div-modalidades").hide();
			$("#div-modalidades").show();
		break;
		case "outra":
			$("#div-outra-especialidade").hide();
			$("#div-modalidades").hide();
			$("#div-outra-especialidade").show();
		break;
		default:
			$("#div-outra-especialidade").hide();
			$("#div-modalidades").hide();
		break;
	}
}

/**
  * Função validar orçamento
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarOrcamento(form) {
	if (validaForm(form)) {
		if ($("#pro_email").val() != $("#pro_email_confirme").val()) {
			alert("Confirme seu e-mail corretamente.");
			$("#pro_email_confirme").focus()
			return false;
		}
		
		// Habilitar botao
//		if (habilitaDesabilitaBotao(form, "disabled")) {

			// Submit está desabilitado
//			alert("Aguarde o término do processo. Obrigado!");
//		} else {
			
			// Submit está habilitado
//			habilitaDesabilitaBotao(form, true);

			$(document).ready(function() {
				$("#" + form).ajaxForm({beforeSend: $('#loading').html('<img src="img/loading.gif" alt="Carregando..." />')});
				$("#" + form).ajaxForm(function(txt) {				
					
					// Submit está desabilitado
//					habilitaDesabilitaBotao(form, false);

					// Retorno
					if (getTrim(txt) == "0") {
						alert("Não foi possível enviar o orçamento, tente novamente.");
					} else if (getTrim(txt) == "1") {
						alert("Orçamento enviado com sucesso.");
						document.getElementById(form).reset();
						$.facebox.close();
					} else if (getTrim(txt) == "2") {
						alert("Código de segurança inválido.");
					} else {
						alert("Não foi possível enviar o orçamento, tente novamente.");
					}
				});
			});
//		}
	}
}

/**
  * Função Habilita/Desabilita Submit
  * @param form Identificador do form
  * @return 
  **/
function habilitaDesabilitaBotao(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) {
			
			// Submit
			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;
			
			// Padrão
			default:
			break;
		}
    }
}