IncludeJavaScript("scripts/cufon.js"); // Núcleo do Cufón
IncludeJavaScript("scripts/fontes.js"); // Núcleo do Cufón
IncludeJavaScript("scripts/jquery.cycle.js"); // Cycle
IncludeJavaScript("scripts/jquery.facebox.js"); // Facebox
IncludeJavaScript("scripts/jquery.lightbox.js"); // Lightbox
IncludeJavaScript("scripts/jquery.easing.js"); // Facebox
IncludeJavaScript("scripts/jquery.flash.js"); // Flash
IncludeJavaScript("scripts/jquery.ui.js"); // jQuery UI completo
IncludeJavaScript("scripts/jquery.maskedinput.js"); // Máscaras para formulários
IncludeJavaScript("scripts/jquery.form.js"); // Post form ajax

// Funcoes
$(function() {
		
	// Links externos
	$("a[rel=external]").attr("target","_blank");
	$("a[rel=external nofollow]").attr("target","_blank");

	// Botao submit
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");

	// Voltar
	$(".bt-voltar").click(function(){ history.back(); return false; });

	// Transparencia
	$(".opac").css("opacity","0.5");
	
//	var pegaVar = $(".flash-home").attr("rel");
//	var pegaVarInterno = $(".flash-interno").attr("rel");

	
	if ($("*").hasClass(".flash-home")) {
		var pegaRelFlash = getTrim($(".flash-home").attr("rel"));
	}else{
		var pegaRelFlash = getTrim($(".flash-interno").attr("rel"));
	}

	var arrAux = pegaRelFlash.split('<!-- SEPARADOR -->');
	var pegaVar = arrAux[0];
	var pegaVarLang = arrAux[1];

	// Flash home
	$(".flash-home").flash({
	    src: "swf/preloader.swf",
	    width: 940,
	    height: 430,
		wmode: "transparent",
		flashvars: { cmd: pegaVar, lang: pegaVarLang },
		expressInstall: true
	});
	
	// Flash interno
	$(".flash-interno").flash({
	    src: "swf/top.swf",
	    width: 940,
	    height: 190,
		wmode: "transparent",
		flashvars: { cmd: pegaVar, lang: pegaVarLang },
		expressInstall: true
	});
	
	// Flash representantes
	$(".flash-representantes").flash({
	    src: "swf/representantes.swf",
	    width: 940,
	    height: 280,
		wmode: "transparent",
		expressInstall: true
	});

	// Fontes
	Cufon.replace(".futura", { fontFamily: "Futura-BoldOblique", hover: true });
	Cufon.replace(".futura-s", { fontFamily: "Futura-BoldOblique", hover: true, textShadow: "#24343a 1px 1px" });
	Cufon.replace(".futura-g", { fontFamily: "Futura-BoldOblique", hover: true, color: "-linear-gradient(#4ec2e8, #4caee0)", textShadow: "#24343a 1px 1px" });
	Cufon.replace(".vag-s", { fontFamily: "VAG Round", textShadow: "#24343a 1px 2px", hover: true });
	
	// Cycle
	$(".box-holder ul").cycle({
		fx: "scrollHorz",
		easeIn: 'easeInOutBack',
		easeOut: 'easeInOutBack',
		prev: ".arrow-left",
		next: ".arrow-right",
		timeout: 0
	});
	
	// Listagem Tecnologia
	$(".listagem-tecnologia li a").click(function(){
		if ($(this).hasClass("sel")) {
			$($(this).attr("href")).slideUp(350);
			setTimeout(function(){ $(".listagem-tecnologia li a").removeClass("sel"); }, 350);
		} else {
			$(".listagem-tecnologia li a").removeClass("sel");
			$(this).addClass("sel");
			$(".lista-desc").slideUp(350);
			$($(this).attr("href")).slideDown(350);
		}
		return false;
	});
	
	// Mascaras formularios
	if (typeof $.fn.mask != "undefined") {
		$(".masc_telefone").mask("(99) 9999-9999");
	}
	
	// Estiliza listas
	$(".lista li:even").addClass("even");
	$(".lista li:odd").addClass("odd");
	$(".lista li:first-child").addClass("first");
	$(".lista li:last-child").addClass("last");

});

/**
  * 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 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 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 envia dados
  * @param form Identificador do form
  * @return nada
  **/
function validarFormPostAjax(form) {
	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);
			
			$(document).ready(function(){
				$("#" + form).ajaxForm(function(txt){
					arrtxt = txt.split('<!-- SEPARADOR -->');
					cod = getTrim(arrtxt[0]);
					msg = getTrim(arrtxt[1]);
					reload = getTrim(arrtxt[2]);
					url = getTrim(arrtxt[3]);
					switch (cod) {
						case '0':
						case 'false':
							alert(msg);
						break;

						case '1':
						case 'true':
						case 'ok':
							if(msg != ''){
								alert(msg);
							}
							switch (reload) {
								case '1':
								case 'true':
								case 'ok':
									if (url != '') {
										window.location.href = url;
									}
									else {
										window.location.reload();
									}
								break;

								case '':
								default:
									document.getElementById(form).reset();
								break;
							}
						break;

						default:
							alert(txt);
						break;
					}
					habilitaDesabilitaSubmit(form,false);
				});
			});
			return true;
		}
	}else{
		return false;
	}
}

/**
  * 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){
					arrtxt = txt.split('<!-- SEPARADOR -->');
					cod = getTrim(arrtxt[0]);
					msg = getTrim(arrtxt[1]);
					reload = getTrim(arrtxt[2]);
					url = getTrim(arrtxt[3]);
					switch (cod) {
						case '0':
						case 'false':
							alert(msg);
						break;

						case '1':
						case 'true':
						case 'ok':
							if(msg != ''){
								alert(msg);
							}
							if (fechar) {
								if ($.facebox) {
									$.facebox.close();
								}
							}
							switch (reload) {
								case '1':
								case 'true':
								case 'ok':
									if (url != '') {
										window.location.href = url;
									}
									else {
										window.location.reload();
									}
								break;

								default:
									document.getElementById(form).reset();
								break;
							}
						break;

						default:
							alert(txt);
						break;
					}
					habilitaDesabilitaSubmit(form,false);
				},
				error: function(txt){
					habilitaDesabilitaSubmit(form,false);
					alert("Erro: " + txt);
				}
			});
		}
	}
}

/**
  * Função monta caixa de seleção
  * @param div DIV
  * @param id Identificador
  * @return nada
  **/
function montaCaixa(div, id, sel) {
	$.ajax({
		type: "POST",
		url: "lib/acao-monta-caixa.php",
		data: 	"" + 
				"div=" + div + "&" + 
				"id=" + id + "&" + 
				"sel=" + sel + "&" + 
				"",
		success: function(txt) {
			$("#" + div).html(txt);
		}
	});
}

/**
  * Formata a digitação do CPF / CNPJ
  *
  * @param campo String campo que será atualizado
  * @param e event evento
  * @return void
  **/
function formataCpfCnpj(campo, e) {
	evento = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(evento)) || (evento == 32)) && (evento != 8 && evento != 0)) {
		return false;
	}
	mascara = false;
	if (evento != 45 && evento != 46 && evento != 8 && evento != 0 ) {
		var str = campo.value;
		str = str.replace('.','');
		str = str.replace('.','');
		str = str.replace('-','');
		str = str.replace('/','');

		if (str.length < 11) {
			mascara = 'cpf';
			temp = str.substr(0,3) ;
			if(temp.length == 3 && str.length != 3) {
				temp += '.';
			}
			temp += str.substr(3,3);
			if(temp.length == 7) {
				temp += '.';
			}
			temp += str.substr(6,3);
			if(temp.length == 11) {
				temp += '-';
			}
			temp += str.substr(9,2);
			campo.value= temp;
		}else{
			mascara = 'cnpj';
			temp = str.substr(0,2);
			if(temp.length == 2) {
				temp += '.';
			}
			temp += str.substr(2,3);
			if(temp.length == 6) {
				temp += '.';
			}
			temp += str.substr(5,3);
			if(temp.length == 10) {
				temp += '/';
			}
			temp += str.substr(8,4);
			if(temp.length == 15) {
				temp += '-';
			}
			temp += str.substr(12,2);
			campo.value= temp;
		}
	}
}
/**
  * Função monta lojas de reparo
  * @param div DIV
  * @param id Identificador
  * @return nada
  **/
function montaLojaReparo(div, id) {
	$.ajax({
		type: "POST",
		url: "lib/acao-monta-loja-reparo.php",
		data: 	"" + 
				"div=" + div + "&" + 
				"id=" + id + "&" + 
				"",
		beforeSend: function(){
			$("#" + div).addClass('loading');
		},
		success: function(txt) {
			$("#" + div).removeClass('loading');
			$("#" + div).html(txt);
		},
		error: function(txt) {
			$("#" + div).removeClass('loading');
		}
	});
}

function makepage(titulo,conteudo) {
  return "<html>\n" +
    "<head>\n" +
    "<title>" + titulo + "</title>\n" +
    "<scr" + "ipt>\n" +
    "function step1() {\n" +
    "  setTimeout('step2()', 10);\n" +
    "}\n" +
    "function step2() {\n" +
    "  window.print();\n" +
    "  window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +
    "</head>\n" +
    "<body onLoad='step1()'>\n" +
    "" + conteudo + "\n" +
    "</body>\n" +
    "</html>\n";
}

function printme(titulo,id) {
	conteudo = document.getElementById(id).innerHTML;
	link = "about:blank";
	var pw = window.open(link, "_new", "width=620, height=400, scrollbars=yes");
	pw.document.open();
	pw.document.write(makepage(titulo,conteudo));
	pw.document.close();
}