$(document).ready(function(){
	$('a.vote-up').bind('click',function(e){
		e.preventDefault();
		doVote($(this));
		$(this).hide();
		$('a.vote-down').show();
	});
	$('a.vote-down').bind('click',function(e){
		e.preventDefault();
		doVote($(this));
		$(this).hide();
		$('a.vote-up').show();
	});
});

function doVote(vote){
	$.post(vote.attr('href'),{}, function(data){
		if (data.success !== true){
			if(data.error_message == 'Not authenticated.'){
				$.facebox('Please log in / register first');
			}
		}else{
			$.facebox('Thanks for voting!');
			updateScore(data);
		}
	}, 'json');
}

function updateScore(data){
    $('#votes').html(data.score.num_votes);
    $('#position').html(data.position)
	//$('#vote-score').html(data.score.score +' out of '+ data.score.num_votes)
}
