$(function() {
	
	var n_destaques = 0;
	$.each($('div.glidecontent'),function(){
		n_destaques++;
		$('a#bt_proximo').before('<a href="#" class="toc" title="'+$('span#link_'+n_destaques).html()+'">'+n_destaques+'</a>');
		var id = $(this).attr('id');
		var partes = id.split('_');
		var imagem = $(this).attr('title');
		var numero = partes[1];
		var titulo = $('span#link_'+numero).html();
		var link = $('span#link_'+numero).attr('title');
		$(this).attr('title',titulo);
		var a = '<a class="trans">';
		var a2 = '</a>';
		if ( link != '' ){
			a = '<a class="trans" href="'+link+'" title="'+titulo+'">';
			a2 = '</a>';
		}
		$('span#link_'+numero).html(a+titulo+a2);
		$(this).css('background-image','url('+imagem+')');
	});
	
	featuredcontentglider.init({
		gliderid: "destaques", //ID of main glider container
		contentclass: "glidecontent", //Shared CSS class name of each glider content
		togglerid: "p-select", //ID of toggler container
		remotecontent: "", //Get gliding contents from external file on server? "filename" or "" to disable
		selected: 0, //Default selected content index (0=1st)
		persiststate: false, //Remember last content shown within browser session (true/false)?
		speed: 500, //Glide animation duration (in milliseconds)
		direction: "leftright", //set direction of glide: "updown", "downup", "leftright", or "rightleft"
		autorotate: true, //Auto rotate contents (true/false)?
		autorotateconfig: [5000, 5] //if auto rotate enabled, set [milliseconds_btw_rotations, cycles_before_stopping]
	})
	
	$('#enquete_votar').bind('click',function(){
		enquete_votar();
	});
	
	$('#enquete_ver_parcial').bind('click',function(){
		enquete_get_parcial();
	});
	
	$('#enquete_fechar').bind('click',function(){
		enquete_fechar();
	}).addClass('ponteiro').attr('title','Fechar');
	
	$("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='+$('#c_news_nome').val()+'&email='+$('#c_news_email').val(),
				type: 'POST',
				dataType: 'text',
				cache: false,
				beforeSend: function(){
					$('#news_send').hide().after('<img id="news_enviando" src="img/carregando.gif" alt="" />');
				},
				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_fechar(){
	$('#enquete_fechar').hide();
	$('#enquete_parcial').fadeOut().removeClass('carregando');
}

function enquete_get_parcial(msg){
	if ( msg != null && msg != '' && msg != undefined ){
		msg = '/ok';
	} else {
		msg = '';
	}
	$('#enquete_parcial').html('').fadeIn('fast',function(){
		$('#enquete_fechar').show();		
		$.ajax({
			url: 'enquetes/parcial'+msg,
			data: "",
			type: 'GET',
			dataType: 'text',
			cache: false,
			beforeSend: function(){
				$('#enquete_parcial').html('').addClass('carregando');
			},
			error: function(){
				alert('Erro na requisição');
				enquete_fechar();
			},
			success: function(retorno){
				$('#enquete_parcial').html(retorno).removeClass('carregando');
			}
		});
	});
}

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').html('...');
			},
			error: function(){
				alert('Erro na requisição');
				$('#enquete_votar').html('Votar');
			},
			success: function(retorno){
				$('#enquete_votar').html('Votar');
				if ( retorno == '1' ){
					enquete_get_parcial('ok');
				} else {
					alert(retorno);
				}
			}
		});
	}
}