$j(function(){ Site.popfloat(); });

var Site = {
	popfloat:function(acao) {
		if(acao == 0) {
			$j('.banner-flutuante').fadeOut();	
		} else if(location.href.indexOf('?') == -1) {
			$j('.banner-flutuante').fadeIn();
			setTimeout(function() { $j('.banner-flutuante').fadeOut(); }, 40000);
		}
	}
}

var Newsletter={	
	cadastrar: function(form){   
		var nome = form.nome.value.toLowerCase();
		var email = form.email.value.toLowerCase();
	  
		if ((nome == 'nome') || (email == 'e-mail')) {
			$j.alert({
				type: 'error', timeout: 5000, width:450,
				html: 'Para receber informativos, preencha corretamente o formulário!'
			});
			
			$j('#nome-newsletter').focus();
		} else {
			if ($Form.validate(form)) {
				$j.alert({ type:'loading', html:'Processando, aguarde!', width:200, hide:false });
						
				$j.ajax({
					url: 'acoes.php?newsletter&inserir', type: 'post', data: $j(form).serialize(),
					success: function(response){ //alert(response);
						if (response == "") {
							$j.alert({
								html: 'Seus dados foram cadastrados com sucesso! Em breve enviaremos nosso informativo.',
								width:540
							});
							
							$j('#nome-newsletter, #email-newsletter').val('');
						}else if(response.indexOf('Duplicate') != -1) {
							$j.alert({
								type:'error', html:'O e-mail ' +form.email.value+ ' já consta em nossa base de dados!'
							});
						} else {
							alert(response);
							$j.alert({
								type:'error', html:'Houve uma falha ao salvar o cadastro. Tente novamente mais tarde!'
							});
						}
					}
				});
			}
		}
        return false;
    }
}

var Evento = {
	acoes : "acoes.php?",
	
	inscricao:function(form) {
		if ($Form.validate(form)) {
			$j.ajax({
				url: this.acoes + 'evento&inscricao', type: 'post', data: $j(form).serialize(),
				success: function(response) { //alert(response);
					if (response == "" || response) {
						$j.alert({
							type: "success", html: 'Inscrição efetuada com sucesso! Aguarde nosso contato.',
							modal: false, timeout: 6000, width:400
						});
						$j('#formInscricao-container').slideUp();
						$j(form)[0].reset();
					} else {
						$j.alert({
							type: 'error', modal: false, width: 480,
							html: 'Não foi possível realizar a inscrição. Verifique os dados digitados e tente novamente.'
						});
					}					
				}
			});
		}		
		return false;
	}
}

var Associado = {	
	acoes : "Associados/Associados.actions.php?",
	
	salvar:function(form){
		if (form.tip_pessoa.value == '') {
			$j.alert({type:'error', html:'Selecione o tipo de pessoa!'});
		} else {
			if ($Form.validate(form)) {
				//$j.alert({ type:'loading', html:'Salvando dados, aguarde...', hide:false, modal:true });
				$j(form).submit(); 
			}
		}
        return false;
    },	
    salvo:function() {
		var iframe = ($j('#formupload').contents().find('body').html());
		
		if(iframe != null && iframe.indexOf('Duplicate entry') != -1) {
			parent.$j.alert({ type:'error', width:500, html:'Cadastro não efetuado. O e-mail digitado já consta em nossa base de dados.' });
		} else {
			var href = location.href.replace('#form', '');
			href = href.replace('&ok', '');
			
			location.replace(href+'&ok');
		}
    },	
	// recarrega o componente Cadastro Associado
	login:function(form){
		$j.alert({ type:'loading', hide:false, width:300, html:'Validando seus dados, aguarde...' });
		
		if (form.email.value == "email" || form.senha.value == "senha") {
			$j.alert({
				type: 'error', timeout: 5000, width:450,
				html: 'Para efetuar login, preencha corretamente o formulário!'
			})
		}else{
			$j.ajax({
				url: this.acoes+'login', type: 'post', data: $j(form).serialize(),
				success: function(response) { //alert(response);
					if(response == "logado"){
						$j.alert({
							type:"success", html:'Login efetuado com sucesso, aguarde...', timeout:3000, 
							out:function() { location.href=form.url.value }, width:350
						});
					} else if(response == "inativo") {
						$j.alert({
							type:'error', modal:false, width:500,
							html:'Seu acesso está inativo! Entre em contato conosco para avaliarmos sua situação.'
						});
					} else{
						$j.alert({
							type:'error', modal:false, width:450,
							html:'Erro ao efetuar login. Verifique os dados digitados e tente novamente.'
						});
					}
					
				}
			});
		}
		return false;
	},	
	logout:function(){
		$j.alert({type:'loading', html:'Efetuando Logout, aguarde...', hide:false, modal:true});
		$j.ajax({
			url: this.acoes+'logout',
			success: function(response){
				location = "index.php";
			}
		});
	}
}