$().ready(function(){
	// Evento onclick para los botones de votación
	$('a.votes').click(function(){
		var side = null;
		var points = null;
		var id = null;
		
		if ($(this).hasClass('vote-ok-divinas')) {
			id = $(this).attr('rel').split('-')[2];
			side = 'divinas';
			points = 1;
		} else if ($(this).hasClass('vote-ko-divinas')) {
			id = $(this).attr('rel').split('-')[2];
			side = 'divinas';
			points = -1;
		} else if ($(this).hasClass('vote-ok-populares')) {
			id = $(this).attr('rel').split('-')[2];
			side = 'populares';
			points = 1;
		} else if ($(this).hasClass('vote-ko-populares')) {
			id = $(this).attr('rel').split('-')[2];
			side = 'populares';
			points = -1;
		} else {
			return false;
		}
		
		sqy_patito_vote(id, side, points);
		return false;
	});
	
	// Se apagan los botones si ya se ha votado
	$('a.votes-disabled.not-voted img').css('opacity', .25).click(function(){return false;});
	
	var $voted = $('a.votes-disabled.voted img');
	
	var bottom = parseInt($voted.css('bottom'));
	var left = parseInt($voted.css('left'));
	
	$voted
	.css({
		bottom:		(bottom - 3) + 'px',
		left:		(left - 3) + 'px',
		width:		($voted.width() + 6) + 'px',
		height:		($voted.height() + 6) + 'px'
	})
	.click(function(){return false;});
	
});

function sqy_patito_vote(id, side, points) {
	var $votes = $('.vota a.votes').attr('class', '');

	$.post('/api/', {
		f:		'vote',
		id:		id,
		side:	side,
		points:	points
	}, function(d){
		if (d && d.result && d.result == 'OK') {
			var $voter = $('#voto-ok');
			var $other = $('#voto-ko');
			if (points < 1) {
				$voter = $('#voto-ko');
				$other = $('#voto-ok');
			}
			
			$other.css('opacity', .25);
			
			var bottom = parseInt($voter.css('bottom'));
			var left = parseInt($voter.css('left'));
			
			$voter.css({
				// position:	'relative',
				bottom:		(bottom - 3) + 'px',
				left:		(left - 3) + 'px',
				width:		($('#voto-ok').width() + 6) + 'px',
				height:		($('#voto-ok').height() + 6) + 'px'
			});
			
			$voter.fadeOut('slow', function(){
				$voter.fadeIn('slow');
			});
		} else {
			// FAIL !
			$votes.css('opacity', .25);
		}
	}, 'json');
}

