$(document).ready(function() {
	//codigo
	$(".animaVotar").hover(function(){
			$(".animaVotar").stop(true, true).animate({'left': -99}, 200);
		}, function(){
			$(".animaVotar").stop(true, true).animate({'left': 0}, 200);
	});
	
	
	$(".bg").hover(function(){
			$(this).find('.detalhes').stop().animate({right: 5}, 200);
		}, function(){
			$(this).find('.detalhes').stop().animate({right: -100}, 200);
	});
	
	$(".aRastreamento").hover(function(){
			$(this).find('.detalhesRastreamento').stop().animate({right: 5}, 200);
		}, function(){
			$(this).find('.detalhesRastreamento').stop().animate({right: -100}, 200);
	});
	
});

function chamaDetalhes( evento, id ){
	if ( evento == "over" ){
		$("#detalhes"+id).stop(true,true).animate({'right': 5 }, 200);
	} else {
		$("#detalhes"+id).stop(true,true).animate({'right': -100 }, 200);
	}
	
}

function votarEnquete(){
	
	if ( !$("input[name=resposta_enquete]:checked").val() ){
		alert( "Escolha uma opção na enquete." );
		return false;
	}
	
	$.post("/votarEnquete/", {
			
			enquete: $("#aEnquete").val(),
			opcao: $("input[name=resposta_enquete]:checked").attr('id')
			
		}, function (response){
			
			if ( response == "erro" ){
				alert("Não foi possível realizar a votação.")
				return false;
			}
			
			json = eval( response )
			
			total = parseInt(json[0].a) + parseInt(json[0].b);
			if ( json[0].c ) total = total + parseInt( json[0].c );
			if ( json[0].d ) total = total + parseInt( json[0].d );
			
			a_percentual = Math.round(parseFloat((parseInt(json[0].a) * 100) / total), 2)
			b_percentual = Math.round(parseFloat((parseInt(json[0].b) * 100) / total), 2)
			a_width = Math.round(a_percentual)*1.5
			b_width = Math.round(b_percentual)*1.5
			
			html = '<p class="resultado">Resultado</p>'
			html += '<p class="label">'+json[0].label_a+'</p>'
			html += '<div id="a_width" class="oWidth"></div>'
			html += '<div id="a_percentual" class="percentual">'+a_percentual+'%</div>'
			html += '<p class="label">'+json[0].label_b+'</p>'
			html += '<div id="b_width" class="oWidth"></div>'
			html += '<div id="b_percentual" class="percentual">'+b_percentual+'%</div>'
			
			if ( json[0].c ){
				c_percentual = Math.round(parseFloat((parseInt(json[0].c) * 100) / total), 2)
				c_width = Math.round(c_percentual)
				html += '<p class="label">'+json[0].label_c+'</p>'
				html += '<div id="c_width" class="oWidth"></div>'
				html += '<div id="c_percentual" class="percentual">'+c_percentual+'%</div>'
			}
			
			if ( json[0].d ){
				total = total + parseInt( json[0].d );
				d_percentual = Math.round(parseFloat((parseInt(json[0].d) * 100) / total), 2)
				d_width = Math.round(d_percentual)
				html += '<p class="label">'+json[0].label_c+'</p>'
				html += '<div id="c_width" class="oWidth"></div>'
				html += '<div id="c_percentual" class="percentual">'+c_percentual+'%</div>'
			}
			
			$(".votar").hide();
			$("#votacao").html(html);
			$("#a_width").stop().animate({width:a_width}, 200);
			$("#b_width").stop().animate({width:b_width}, 200);
			if ( json[0].c ) $("#c_width").stop().animate({width:c_width}, 200);
			if ( json[0].d ) $("#d_width").stop().animate({width:d_width}, 200);
	});
	
}
