$("*").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(); });
	$(".bg-fotos-troca").css("opacity","0.9");

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

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

		
	var language = $("body").attr("xml:lang");
	//alert(language);
	
	// Flash topo
	$(".flash-top").flash({
	    src: "swf/top.swf",
	    width: '100%',
	    height: 260,
		wmode: "transparent",
		flashvar: { lang: language },
		expressInstall: true
	});
	
	// Flash home
	$(".flash-home").flash({
	    src: "swf/preloader.swf",
	    width: '100%',
	    height: 690,
		wmode: "transparent",
		flashvar: { lang: language },
		expressInstall: true
	});
	
	// Flash Onde Atuamos
	$(".mapa-representantes").flash({
	    src: "swf/ondeatuamos.swf",
	    width: 950,
	    height: 650,
		wmode: "transparent",
		flashvar: { lang: language },
		expressInstall: true
	});
	
	$(".lista-fotos li a").click(function(){
		if(!$(this).hasClass("selecionada")){
			$(".lista-fotos li a").removeClass("selecionada");
			$(this).addClass("selecionada");
		}
		
		return false;
	});
	
	$(".lista-fotos li a, .lista-videos li a").css("opacity","0.5");
	$(".lista-fotos li a, .lista-videos li a").hover(function(){
        $(this).fadeTo("normal", 1.0);
    },function(){
        $(this).fadeTo("normal", 0.5);
    });
	
	$(".lista-videos li a").click(function(){
		if(!$(this).hasClass("selecionada")){
			$(".lista-videos li a").removeClass("selecionada");
			$(this).addClass("selecionada");
		}
		
		return false;
	});
	
	// Cycle
	if (typeof $.fn.cycle != "undefined") {
		$(".holder-marcas").cycle({
		    fx: "scrollHorz",
		    prev: ".bt-anterior",
		    next: ".bt-proximo",
		    timeout: 0
		});
	}
	
	// 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") {
		$(".telefone").mask("(99) 9999-9999");
		$(".cepp").mask("99999-999");
		$(".data").mask("99/99/9999");
		$(".cpf").mask("999.999.999-99");
	}

	//Produtos - Categoria
	$('.links-prod').click(function() {
		var idDiv = $(this).attr('href');
		if ($(idDiv).hasClass('aberto')) {
			$('.list-prod').removeClass('aberto');
			$(idDiv).slideUp(500);
		} else {
			$(".list-prod").slideUp(500);
			$(idDiv).slideDown(500);
			$(".list-prod").removeClass('aberto');
			$(idDiv).addClass('aberto');
		}
		return false;
	});

	$('body.bg-apresentacao').stop().animate({
		backgroundPosition:"(center 0px)"
	}, { duration:1000 });
	$('.home').stop().animate({
		backgroundPosition:"(center -270px)"
	}, { duration:500 });
	
	$('body.bg-contato').stop().animate({
		backgroundPosition:"(center -1700px)"
	}, { duration:3000 });
	
	$('body.bg-novidades').stop().animate({
		backgroundPosition:"(center -650px)"
	}, { duration:3000 });
	
	$('body.bg-onde-atuamos').stop().animate({
		backgroundPosition:"(center -750px)"
	}, { duration:3000 });
	
	$('body.bg-produtos').stop().animate({
		backgroundPosition:"(center -650px)"
	}, { duration:3000 });

	$('body.bg-responsabilidade-social').stop().animate({
		backgroundPosition:"(center -1000px)"
	}, { duration:3000 });
	
	$('body.bg-terceirizacao').stop().animate({
		backgroundPosition:"(center -1000px)"
	}, { duration:3000 });


	// Script do google maps
	var textodomapa = $("a.mapa-google-info").attr("rel");
	var urldomapa = $("a.mapa-google-info").attr("title");

});


/**
  * 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 == "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 limpa espaço
  * @param str String
  * @return nada
  **/
function getTrim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}

/**
  * 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 validarLoginRepresentante(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-login-representantes.php",
			data: str,
			success: function(txt) {
				var retorno = txt.split("<!-- SEPARADOR -->");
				if (getTrim(retorno[0]) == "0") {
					alert("Erro: Login ou Senha incorreto(s).");
				} else if (getTrim(retorno[0]) == "1") {
					window.location.href="index.php?cmd=representantes";//&id="+retorno[1];
				} else {
					alert("Não foi possível efetuar o login, tente novamente.");
				}
			}
		});
	}
}

