$(document).ready(function(){
	loadBgMenu();
	
	$(window).resize(function(){
		loadBgMenu();
	});
});

function loadBgMenu(){
	var wWidth = $(window).width();
	var vHeight = $(window).height();
	
	if(wWidth < 1020){ wWidth = 1020; }
	if(vHeight < 550){ vHeight = 550; }
	
	var w_w	= wWidth,
		w_h	= vHeight,
		r_w	= w_h / w_w,
		i_w	= $(".bg img").width(),
		i_h	= $(".bg img").height(),
		r_i	= i_h / i_w;
	
	if(r_w > r_i){
		new_h = w_h;
		new_w = w_h / r_i;
	} else {
		new_h = w_w * r_i;
		new_w = w_w;
	}
	
	$(".bg, .wrap").css({
		width: wWidth,
		height: vHeight
	});
	
	$(".bg img").css({
		width: new_w,
		height: new_h
	});
	$(".bg img:eq(0)").show();
	
	$("div.menu div.itens").css("width", (wWidth - 420)+"px");
	$("div.internacao div.coldir").css("width", (wWidth - 380)+"px");
	$("div.artigos").css("top", (((vHeight - 78)/2)-132)+"px");
	$("div.contato").css("top", (((vHeight - 78)/2)-132)+"px");
	
	$("div.contato div.forms").css("width", (wWidth - 425)+"px");
	$("div.contato div.forms div.col2").css("width", "45%");
	$("div.contato div.forms div.col2 table td").css("padding-left", (($("div.contato div.forms div.col2").width()-265)/2)+"px");
	$("div.contato div.forms div.col3").css("width", "45%");
	$("div.contato div.forms div.col3 div").css("padding-left", (($("div.contato div.forms div.col3").width()-275)/2)+"px");
}

function setImagens(){
	$("div.coldir img").click(function(){
		if(!$(this).parent().hasClass("on")){
			$(".bg img").fadeOut(1000);
			$("div.coldir div.thumb").removeClass("on");
			
			$(".bg img:eq(" + $(this).attr("rel") + ")").fadeIn(1000);
			$(this).parent().addClass("on");
		}
	});
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string") {
        if(er.test(mail)) { return true; }
	}
	else if(typeof(mail) == "object") {
		if(er.test(mail.value)) { return true; }
	}
    else {
        return false;
    }
}

function EnviarContato(){
	var nome = $("#formContato #nome");
	var email = $("#formContato #email");
	var assunto = $("#formContato #assunto");
	var mensagem = $("#formContato #mensagem");
	
	if(nome.val() == ""){
		alert("O campo nome é obrigatório.");
		nome.focus();
	} else if(email.val() == ""){
		alert("O campo e-mail é obrigatório.");
		email.focus();
	} else if(!checkMail(email.val())){
		alert("E-mail no formato inválido, tente novamente.");
		email.focus();	 
	} else if(assunto.val() == ""){
		alert("O campo assunto é obrigatório.");
		assunto.focus();
	} else if (mensagem.val() == ""){
		alert("O campo mensagem é obrigatório.");
		mensagem.focus();
	} else {
		$.post("contato_enviar.php", $("#formContato").serialize(), function(data){
			arr = data.split("|");
			if(arr[0] == 1){
				nome.val("");
				email.val("");
				assunto.val("");
				mensagem.val("");
			}
			alert(arr[1]);
		});
	}
	
	return false;
}
