$(function(){
    $('#answer').click(function(){
        var answers = [];
        $.each($('#voting_body input:checked'), function(i,item){
            answers.push($(item).val());
        });
        $.post('/home/add_vote/'+$('#voting_id').val(), { aid:answers }, function(data){
            if (data.status == 'error'){
                $('#voting_body').hide();
                $('#voting_submit').hide();
                $('#voting_results').html('<span>'+data.message+'</span>').show();
            } else if(data.status == 'ok'){
                $('#voting_body').hide();
                $('#voting_submit').hide();
                var header = '<table width="100%;">';
                var footer = '</table>'
                var res = '';
                $.each(data.results, function(i, item){
                    res += '<tr><td width="80%"><span>'+item.title+'</span></td><td width="20%"><span><b>'+item.percent+'%</b></span></td></tr>';
                });
                $('#voting_body').hide();
                $('#voting_submit').hide();
                $('#voting_results').html(header+res+footer).show();
            }
        }, 'json');
    });
})

$(function(){
    $('#voting_body input:radio,#voting_body input:checkbox').change(function(){
        $('#answer').attr('disabled', false);
    });
})