$(document).ready(function() {
		
	$('#enquete_votar').bind('click',function(){
		enquete_votar();
	}).addClass('ponteiro');
	
	$('#enquete_parcial').bind('click',function(){
		enquete_get_parcial();
	}).addClass('ponteiro');
	
	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]
	})
	
});

function enquete_get_parcial(){
	$.ajax({
		url: 'enquetes/parcial',
		data: "",
		type: 'GET',
		dataType: 'text',
		cache: false,
		beforeSend: function(){
			$('div#enq').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');
			$('div#enq').html('<li>Erro!</li>').fadeIn();
			$('#enquete_parcial').remove();
		},
		success: function(retorno){
			$('#enquete_parcial').remove();
			$('div#enq').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);
				}
			}
		});
	}
}

var parceiros = [];
function add_parceiro(titulo,foto,link,rel){
	var parceiro = [titulo,foto,link,rel];
	parceiros.push(parceiro);
}

var ultimo = -1;
function get_parceiros(numero){
	var n = numero;
	$('#parc').html('');
	var total = parceiros.length;
	if ( numero > total ){
		numero = total
	}
	var a1 = '';
	var a2 = '';
	var target = '';
	var cont = 0;
	$.each(parceiros,function(index, item){
		if ( index > ultimo ){
			cont++;
			if ( cont <= numero ){
				a1 = '';
				a2 = '';
				target = '';
				if ( item[2] != '' ){
					if ( item[3] == 'out' ){
						target = ' target="_blank" ';
					}
					a1 = '<a '+target+' '+item[2]+'>';
					a2 = '</a>';
				}
				$('#parc').append(a1+'<img id="parc_'+cont+'" src="'+item[1]+'" alt="'+item[0]+'" style="display:none;" />'+a2);
				$('#parc_'+cont).fadeIn('slow');
				//alert(item[0]);
				ultimo++;
			}
		}
	});
	if ( ultimo == (total-1) ){
		ultimo = -1;
	}
	
	if ( total > n ){
		setTimeout(function(){
			get_parceiros(n);
		},5000);
	}
}
