$(window).load(function(){

	// 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");
	$(".facebox-active").facebox();
	$(".bt-voltar").click(function(){history.back();});
	$(".bt-voltares").click(function(){history.back();});
	$(".opac").css({opacity:"0.7"});
	
	// Mascaras formularios
	if (typeof $.fn.mask != "undefined") {
		$(".telefone").mask("(99)9999-9999");
		$(".cep").mask("99999-999");
		$(".cpf").mask("999.999.999-99");
	}
	
	//$(".lista-cursos li a:lt(9)").css("padding-left","8px");
	$(".tables tr td:last-child").addClass("beige");
	$(".tables tr td a:first-child").addClass("font-grande");
	
	// Lightbox
	if ( typeof $.fn.lightBox != "undefined") { $(".lightbox-active").lightBox(); }
	
	// FLASH's
	$('.nav').flash({
	    src: 'swf/nav.swf',
	    width: 200,
	    height: 330,
		wmode: 'transparent',
		expressInstall: true
	});
	
	$('.homeee').flash({
	    src: 'swf/header.swf',
		flashvars: { cmd: 3 },
	    width: 760,
	    height: 315,
		wmode: 'transparent',
		expressInstall: true
	});
	
	// FLASH TOP
	var flashVar = $(".topolhes").attr("id");
	$('.topolhes').flash({
	    src: 'swf/top.swf',
	    width: '100%',
		flashvars: { cmd: flashVar },
	    height: 189,
		wmode: 'transparent',
		expressInstall: true
	});
	
	$(".vinicolas").cycle({
		fx: "scrollHorz",
		timeout: 7000
	});
	
});	
	
	$(document).ready(function() {

		$('.bg').click(function(){
			var produtosImg = $(this).attr("href") ;
			$('.clearfix-img').hide();
			$('.vinhos-estados').hide();
			$(produtosImg).show();
			return false;
		});	
		
	});

/**
  * Função limpa espaço
  * @param str String
  * @return nada
  **/
function getTrim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}

/**
  * 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 validar
  * @param nada
  * @return nada
  **/
function validaFormPost(form) {
	switch(form){
		case 'form-curso':
			msgOK 	= "Cadastro efetuado com sucesso!";
			msgERRO = "Não foi possível efetuar seu cadastro!";
		break;
		default:
			msgOK 	= "Mensagem enviada com sucesso!";
			msgERRO = "Não foi possível enviar sua mensagem!";
		break;
			
	}
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-" + form + ".php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == "0") {
					alert(msgERRO);
				} else {
					if (getTrim(txt) == "ok" || getTrim(txt) == "1") {
						alert(msgOK);
						document.getElementById(form).reset();
					} else {
						if(getTrim(txt)!=""){
							alert(txt);
						}else{
							alert("Nenhum retorno.");
						}
					}
				}		
			}
		});
	}
}
/**
  * 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!");
				}
			}
		});
	}
}

	



