

$.fn.poll = function(params) {
    params = $.extend({
        vote: '/poll/vote',
        results: '/poll/results'
    }, params)
    
    var poll_id = params.poll_id || this[0].id
    var poll = this
    var options = this.find('li')
    
    var show_results = function() {
        $.getJSON(params.results + '/' + poll_id + '?rnd='+Math.random(), function(data) {
            var tot = 0
            var percent
            $.each(data, function() {
                tot += parseInt(this)
            })

            var html = ['<div class="poll-results">']
            $.each(data, function(i) {
                
                var votes = this
                var percent = Math.round((parseInt(votes)/parseInt(tot)) * 100)
                var label = $(options[i]).find('label').html()
                if(!label) {
                    label = options[i].innerHTML
                }
                html.push('<div class="bar-container"><div id="bar'+i+'" style="width: 0%;">' + label + '</div><strong>'+percent+'%</strong></div>')
            })
            html.push("</div>\n") // <p>Total Votes: "+tot+"</p>
            html = html.join('')

            poll.html(html).show()
            poll.find(".bar-container").each(function() { 
                var percentage = $(this).find('strong').text();
                if(percentage != '0%') {
                    $(this.firstChild).css({width: "0%"}).animate({width: percentage}, 'slow');
                }
            });
            
        })
    }
    
    var show_form = function() {
        poll.each(function() {
            $(this).children('li').each(function(i) {
                var id = poll_id + i
                $(this).html('<input type="radio" name="'+poll_id+'" id="'+id+'" value="'+i+'"/> <label for="'+id+'">'+$(this).html()+'</label>')
            })
        })


        poll.find('input').click(function() {
            var radio = this
            var data = {
                poll_id: poll_id,
                option: radio.value
            }
            var a = {
                url: params.vote,
                data: data,
                type: 'post',
                error: function(xhr, errType, ex) {
                    if($.jGrowl) {
                        $.jGrowl.defaults['position'] = 'center'
                        $.jGrowl(xhr.responseText, {header: 'Poll Error', life: 5000})
                    }
                    else {
                        alert(xhr.responseText)
                    }
                },
                success: function(resp) {
                    $.cookie('voted_'+poll_id, radio.value, {expires: 365})
                },
                complete: function() {
                    poll.fadeOut('slow', function() {
                        show_results(poll)
                    })
                }
            }
            return $.ajax(a)

        })    
    }
    
    // Already voted?
    if($.cookie('voted_' + poll_id)) {
        show_results(this)
    }
    else {
        show_form(this)
    }    
}
