var baseurl = '/site/';

function reloadPagina(ms) {
	setTimeout(
		function() {
			window.location.reload();
		},
		ms
	);
}

function respostaAjax(txt) {
	$('.resposta-ajax:visible').html(txt);
	setTimeout(
		function() {
			$('.resposta-ajax:visible').fadeOut('slow');
		},
		7000
	);
}

function msgStatus(form, loading, msg) {
	var status = loading == true ? '<img src="' + baseurl + '/imgs/carregando.gif" alt="carregando" class="carregando" />' + msg : msg;
	var divResposta = '.resposta-ajax';
	var formID = '#' + form.attr('id');
	//var botaoSubmit = $(formID + ' p.botoes-form');
	var divRespostaID = '#' + $(formID + ' div.resposta-ajax').attr('id');
	
	$(divRespostaID).html(status).show();
}

function cadastrar(form, action) {
	msgStatus(form, true, "Cadastrando, aguarde...");
	
	var opcoes = {
		url: action,
		type: "post",
		success: function(resposta) {
			$('.resposta-ajax:visible').hide().html(resposta);
			alert(resposta);
		}
	};
	
	$(form).ajaxSubmit(opcoes);
}

function enviar(form, action) {
	msgStatus(form, true, "Enviando, aguarde...");
	update = false;
	msgResposta = null;
	
	var opcoes = {
		url: action,
		type: "post",
		success: function(resposta) {
			$('.resposta-ajax:visible').hide().html(resposta);
			alert(resposta);
			
			if(update) {
				reloadPagina(2000);
			}
		}
	};
	
	$(form).ajaxSubmit(opcoes);
}

$(document).ready(
	function() {
		/* MENU */
		$("ul#menu-lateral li.submenu").each(function(){
			var el = $('#' + $(this).attr('id') + ' ul:eq(0)');
			
			$(this).hover(function(){
				el.show();
			}, function(){
				el.hide();
			});
		});
		/* FIM MENU */
		
		/* VALUES CAMPOS */
		$("input[type=text], textarea").each(function() {
			if($(this).attr('value') != '') {
				$(this).val() == $(this).attr('value');
			}
		});
		
		$('input[type=text], textarea').focus(
			function() {
				$value = $(this).attr('value');
				$(this).attr('value', '');
				if($(this).attr('id') == 'contato-telefone') {
					$(this).mask("(99) 9999-9999");
				}
			}
		).blur(
			function() {
				if($(this).val() == '') {
					$(this).val($value);
				}
				else {
					return false;
				}
			}
		);
		/* FIM VALUES CAMPOS */
					
		/* CADASTRO USUÁRIOS */
		$("#form-cadastro").submit(
			function() {
				$nome = $("#nome");
				$email = $("#email");
				
				if($nome.val() == "") {
					alert("Informe seu nome!");
					$nome.focus();
					return false;
				}
				if($email.val() == "") {
					alert("Informe seu e-mail!");
					$email.focus();
					return false;
				}
				else {
					cadastrar($(this), $(this).attr("action"));
					return false;
				}
			}
		);
		/* FIM CADASTR USUÁRIOS */
		
		/* @-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@- */
		
		/* TRABALHE CONOSCO */		
		$("#form-tc").submit(
			function() {
				$nome = $("#tc-nome");
				$email = $("#tc-email");
				$msg = $("#tc-msg");
				$arquivo = $("#file");
				
				if($nome.val() == "") {
					alert("Informe seu nome!");
					$nome.focus();
					return false;
				}
				if($email.val() == "") {
					alert("Informe seu e-mail!");
					$email.focus();
					return false;
				}
				if($msg.val() == "") {
					alert("Digite uma mensagem!");
					$msg.focus();
					return false;
				}
				if($arquivo.val() == "") {
					alert("Selecione o arquivo!");
					$arquivo.focus();
					return false;
				}
				else {
					enviar($(this), $(this).attr("action"));
					return false;
				}
			}
		);
		/* FIM TRABALHE CONOSCO */
		
		/* @-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@-@- */
		
		/* CONTATO */		
		$("#form-contato").submit(
			function() {
				$nome = $("#contato-nome");
				$perfil = $("#contato-perfil");
				$email = $("#contato-email");
				$estado = $("#contato-estado");
				$msg = $("#contato-msg");
				
				if($nome.val() == "") {
					alert("Informe seu nome!");
					$nome.focus();
					return false;
				}
				if($perfil.val() == "") {
					alert("Selecione um perfil!");
					$perfil.focus();
					return false;
				}
				if($email.val() == "") {
					alert("Informe seu e-mail!");
					$email.focus();
					return false;
				}
				if($estado.val() == "nulo") {
					alert("Selecione o estado!");
					$estado.focus();
					return false;
				}
				if($msg.val() == "") {
					alert("Digite uma mensagem!");
					$msg.focus();
					return false;
				}
				else {
					enviar($(this), $(this).attr("action"));
					return false;
				}
			}
		);
		/* FIM CONTATO */
	}
);
