$(document).ready(function() {
	
	$.each($('a[rel="externo"],a.externo,a[rel="out"],a.out'),function(){
		$(this).attr('target','_blank');
	});
	
	//Se o lighbox foi carregado, inicia-o	
	if (lbcarregado){
		$('a.lb').lightBox()
	}
	
	$('#enquete_votar').bind('click',function(){
		enquete_votar();
	}).addClass('ponteiro');
	
	$('#enquete_parcial').bind('click',function(){
		enquete_get_parcial();
	}).addClass('ponteiro');
	
	$("form#news").validate({
		messages:{
			nome			: 'Informe seu nome',
			email			: 'Informe seu e-mail corretamente'
		},
		showErrors: function(errorMap, errorList) {
		
		},
		invalidHandler: function(form, validator) {
			alert('Preencha os campos corretamente.');
		},
		submitHandler: function() {
			$.ajax({
				url: 'ajax/newsletter',
				data: '&nome='+$('#news_nome').val()+'&email='+$('#news_email').val(),
				type: 'POST',
				dataType: 'text',
				cache: false,
				beforeSend: function(){
					$('#news_send').hide().after('<li id="news_enviando" style="text-align:center"><img src="img/carregando.gif" alt="" /></li>');
				},
				error: function(){
					$('#news_enviando').remove();
					$('#news_send').fadeIn();
					alert('Erro na requisição, tente novamente');
				},				
				success: function(retorno){
					if ( retorno == '1' ){
						$('#news_enviando').remove();
						$('#news_send').fadeIn();
						alert("Seu cadastro foi realizado com sucesso!\nObrigado!");
						$('form#news').trigger('reset');
					} else {
						$('#news_enviando').remove();
						$('#news_send').fadeIn();
						alert(retorno);
					}
				}
			});
			
			return false;
		}
	});
	
});

function enquete_get_parcial(){
	$.ajax({
		url: 'enquetes/parcial',
		data: "",
		type: 'GET',
		dataType: 'text',
		cache: false,
		beforeSend: function(){
			$('ul#enquete').hide().html('').after('<div style="text-align:center"><img id="enquete_parcial" src="img/carregando.gif" alt="" /></div>');
		},
		error: function(){
			alert('Erro na requisição');
			$('ul#enquete').html('<li>Erro!</li>').fadeIn();
			$('#enquete_parcial').remove();
		},
		success: function(retorno){
			$('#enquete_parcial').remove();
			$('ul#enquete').html(retorno).fadeIn();
		}
	});
}

function enquete_votar(){
	var numero = 0;
	$('#form_enquete input:radio').each(function() {
		if ( $(this).is(':checked') ) {
			var partes = $(this).attr('id').split('_');
			numero = partes[1];
		}
	});
	if ( numero == 0 ){
		alert('Escolha uma das opções!');
	} else {
		$.ajax({
			url: 'enquetes/votar/'+numero,
			data: "",
			type: 'GET',
			dataType: 'text',
			cache: false,
			beforeSend: function(){
				$('#enquete_votar').after('<img id="enquete_carregando" src="img/carregando.gif" />').hide();
			},
			error: function(){
				alert('Erro na requisição');
				$('#enquete_votar').fadeIn();
				$('#enquete_carregando').remove();
			},
			success: function(retorno){
				$('#enquete_votar').fadeIn();
				$('#enquete_carregando').remove();
				
				if ( retorno == '1' ){
					enquete_get_parcial();
				} else {
					alert(retorno);
				}
			}
		});
	}
}
