function voting_change_vote(direction, e) {
    
    e.preventDefault();

    var ballot = $(e.target).parents('.vote').get(0);
    
    $.post(e.target.href, {}, function(data) {
        if(data.success) {
            if($(ballot).parents('#awards').length > 0) {
                voting_update_score(ballot, data.score.positive_score, data.score.num_votes);
            } else {
                voting_update_score(ballot, data.score.score, data.score.num_votes);
            }
            var current = $(ballot).hasClass('up') ? 'up' : ($(ballot).hasClass('down') ? 'down' : 'clear');
            voting_toggle_ballot(ballot, current, direction);
        } else {
            $(e.target).qtip({
                content: '<a href="/accounts/login/?next=/stream/list/">Log in</a> or <a href="/accounts/register/?next=/stream/list/">register</a> to vote.',
                position: {
                    corner: {
                        target: 'leftMiddle',
                        tooltip: 'topRight'
                    }
                },
                show: {
                    when: false,
                    ready: true
                },
                hide: { delay: 3000 },
                style: {
                    width: 200,
                    padding: 5,
                    border: {
                        width: 5,
                        radius: 5
                    },
                    tip: true,
                    'font-size': '12px',
                    name: 'dark'
                }
            });
        }
    }, 'json');

    return false;
    
}

function voting_toggle_ballot(ballot, from, to) {
    
    var button_up = $(ballot).children('.buttons').children('.button-up').get(0);
    var button_down = $(ballot).children('.buttons').children('.button-down').get(0);
    
    $(ballot).removeClass(from);
    $(ballot).addClass(to);
    if(from == 'clear' && to == 'up') {
        voting_set_button(button_up, 'clear');
        voting_set_button(button_down, 'down');
    } else if(from == 'clear' && to == 'down') {
        voting_set_button(button_up, 'up');
        voting_set_button(button_down, 'clear');
    } else if(from == 'up' && to == 'down') {
        voting_set_button(button_up, 'up');
        voting_set_button(button_down, 'clear');
    } else if(from == 'down' && to == 'up') {
        voting_set_button(button_up, 'clear');
        voting_set_button(button_down, 'down');
    } else if(to == 'clear') {
        voting_set_button(button_up, 'up');
        voting_set_button(button_down, 'down');
    }
    
}

function voting_set_button(button, to) {
    from = $(button).hasClass('upvote') ? 'up' : $(button).hasClass('downvote') ? 'down' : 'clear';
    $(button).removeClass(from + 'vote');
    $(button).addClass(to + 'vote');
    button.href = button.href.replace(from + 'vote', to + 'vote');
}

function voting_update_score(ballot, total, votes) {
    $(ballot).children('.score').children('.total').text(total);
    $(ballot).children('.score').children('.votes').text(votes + ' vote' + (votes != 1 ? 's' : ''));
}

function voting_upvote(e) {
    return voting_change_vote('up', e);
}

function voting_downvote(e) {
    return voting_change_vote('down', e);
}

function voting_clearvote(e) {
    return voting_change_vote('clear', e);
}

function voting_setup() {
    $('.vote form').each(function(i, element) {
        $(element).replaceWith('<a id="' + element.id + '" class="' + $(element).attr('class') + '" href="' + element.action + '"></a>');
    });
    $('.vote .upvote').live('click', voting_upvote);
    $('.vote .downvote').live('click', voting_downvote);
    $('.vote .clearvote').live('click', voting_clearvote);
}
