/* 
 * obsluga systemu punktowania (vote)
 * 
 */

$(document).ready(function() {
    vote.setup()
})

vote = {
    
    save_vote: function(obj,sign) {
        root = obj.parent().parent()
        vote_id = $('.VOTE_ID',root).html()
        
        
        $('.vote_foot',root).html('...')
        $.ajax({
            url: '/common/save_vote',
            type: 'get',
            dataType: 'json',
            data: {
                'vote_id': vote_id,
                'sign': sign
            },
            success: function(ret) {
                
                $('.vote_foot',root).html(ret.vote)
                if( !ret.result ) {
                    alert('Twój głos został już zapisany wcześniej.')
                } 
            }
        })
    },
    
    setup: function() {
        $('.loggedin .vote .plus').unbind().click(function() {
            vote.save_vote($(this),'p')
            return false;
        })
        
        $('.loggedin .vote .minus').unbind().click(function() {
            vote.save_vote($(this),'m')
            return false;
        })
        
        $('.notloggedin .vote .minus, .notloggedin .vote .plus').unbind().click(function() {
            alert('Musisz być zalogowany.')
            return false;
        })
    }
}


