$(document).ready( function(){
	var orig_name = $('#cad_newsletter input[name="nome"]').val();
	var orig_mail = $('#cad_newsletter input[name="email"]').val();
							
	$("#uf").change(function(){
		id = $(this).val();
		$.ajax({
				url: 'fale-conosco/cidades.php', 
				dataType: 'html',
				type: 'POST',
				timeout: 1000,
				data: {'id' : id},
				success: function(data){
						$("#cidades").fadeOut('1000', function(){
								$(this).empty().html(data).fadeIn(1000);
						});
				},
				error: function(xhr,er) {
						alert("Ocorreu um erro ao carregar as cidades. Por favor tente novamente");
				}		
		});
	});

	$("input[name='acompanhamento-processual'], input[name='advocacia-apoio'], input[name='fale-conosco']").click(function(){
			if(verificaForm($(this).attr('name'))){;	
				$('#'+$(this).attr('name')).submit();
			} else {
				alert(unescape('Por favor preencha corretamente todos os campos'));
			}
	});
	
	if ($('#content').height() >= $('body').height()){
		$('#overlay').height($('#content').height());
	} else {
		$('#overlay').height($('body').height());
	}
	
	$('#bt_close_enquete').click(function(){
		$('#overlay').hide();
		$('#abs_enquete').hide();
	});
	
	$('#bg_site').height($('#content').height() - 145);
	
	$('.line, .active').mouseover(function(){
		$(this).css('background-position', '-450px 0');
	});
	
	$('.line').mouseout(function(){
		$(this).css('background-position', '0 0');
	});
	
	$('.line').click(function(){
		if ($(this).next('div').is(':visible')){
			$(this).children('.arrow').css('background-position', '0 -0');
			$(this).removeClass('active');
			//$('#center_content').height($('#center_content').height() - $(this).next('div').height());
		} else {
			$(this).children('.arrow').css('background-position', '0 -17px');
			$(this).addClass('active');
			//$('#center_content').height($('#center_content').height() + $(this).next('div').height());
		}
		$(this).next('div').slideToggle(300);
	});
	
	$('#newsletter #bt_submit').click(function(){
		var erro = '';
		$('#cad_newsletter input').each(function(){
			if($(this).val() != ''){
				if(orig_name == $('#cad_newsletter input[name="nome"]').val()){
					erro = 'Por favor, preencha seu nome corretamente\n';
				} else {
					if ((orig_mail == $('#cad_newsletter input[name="email"]').val())||(!checkMail($('#cad_newsletter input[name="email"]').val()))){
						erro = unescape('Por favor, preencha seu e-mail corretamente\n');
					}
				}
			} else {
				erro = 'Por favor, preencha todos os campos.\n';
			}
		});
		if (erro != ''){
			alert(erro);
		}else{
			
/*			$('#cad_newsletter').submit();
			$('#newsletter').html("Cadastro Efetuado com sucesso!");
			$("#newsletter").fadeOut('1000', function(){
				$(this).empty().html(data).fadeIn(1000);
			});*/
			
			var dados = $("form[id='cad_newsletter']").serialize();
			$.ajax({
					url: 'newsletter/cadastro.php', 
					dataType: 'html',
					type: 'GET',
					timeout: 1000,
					data: dados,
					success: function(data){
							$("#sub_newsletter").fadeOut('1000', function(){
									$(this).empty().html('<br /><br />'+data).fadeIn(1000);
							});
					},
					error: function(xhr,er) {
							alert("Ocorreu um erro ao ingressar na newsletter. Por favor tente novamente");
					}		
			});
			
			
			
		}
	});
	
	$("#sub_enquete #bt_votar img").click(function(){
		if(verificaEnquete(document.form_enquete)){
			var dados = $("form[name='form_enquete']").serialize();
			$.ajax({
				url: 'enquete/votar.php', 
				dataType: 'html',
				type: 'POST',
				timeout: 4000,
				data: dados,
				success: function(data){
					$("#resul_enquete").fadeOut('1000', function(){
						$(this).empty().html('<div style="margin-left: 40px">'+data+'</div>').fadeIn(1000);
					})
				},
				error: function() {
					alert('Ocorreu um erro ao votar, por favor, tente mais tarde !');
				}		
			});
		}
	});
	$('#bt_zoom_plus').click(function(){
			$('div').find('.texto').each(function(){
					var lenght = parseInt($(this).css('font-size'));
					if (lenght < 16){ 
							lenght++;
							$(this).css('font-size', lenght + 'px');
					}
			});	
	});
	$('#bt_zoom_less').click(function(){
			$('div').find('.texto').each(function(){
					var lenght = parseInt($(this).css('font-size'));
					if (lenght > 8){ 
							lenght--;
							$(this).css('font-size', lenght + 'px');
					}
			});	
	});
	
	
	
});

function verificaEnquete(obj)
{
    radiochk = false;

    for ( n = 0; n < obj.opt_id.length; n++ )
    {
        el = obj.opt_id[n];
        radiochk = radiochk || el.checked;
    }

    if ( !radiochk ) {
        alert(unescape("Nenhuma opção da enquete foi escolhida!"));
        return false;
    }
    
    return true;
}

function vota()
{
    if(verificaEnquete(document.form_enquete))
    {
        var nwin = window.open('', 'winEnquete', 'location=no, width=435, height=330, scrollbars=yes');
        document.form_enquete.submit();
        nwin.focus();
    }
}
  
function verResultado(id)
{
    var nwin = window.open('enquete/enquete_resultado.php?acao=ver_resultado&enq_id='+id, 'winEnquete', 'location=no, width=435, height=330, scrollbars=yes');
    nwin.focus();
}

var valido;

function verificaForm(form){
		var flag = true;
		
		$('form[id='+form+']').find('input,select,textarea').each(function(){	
			if(($(this).val().length <= 0) && ($(this).attr('req') == 'true')) {
				$(this).css('border-color','#f00');	
				flag = false;
			} else {
				if (($(this).attr('name') == 'email')&&(!checkMail($(this).val()))){
					$(this).css('border-color','#f00');	
					flag = false;
				} else {
					$(this).css('border-color','#dedede');
				}
			}
		});
		return flag;
}

function checkMail(mail){
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(mail))
		valido = true;
	else{
		//alert("Este endereço de e-mail não é válido!");
		valido = false;
	}
	return valido;
}


function limparPadrao(campo) {
        if (campo.value == campo.defaultValue) {
                campo.value = "";
        }
}

function escreverPadrao(campo) {
        if (campo.value == "") {
                campo.value = campo.defaultValue;
        }
}
