(function(jQuery) {

    function init() {

        jQuery(document).delegate(".button.vote", "click", handleVoteClick)

    }

    function handleVoteClick(event) {

        var
                jTarget = jQuery(event.currentTarget),
                pollId = jTarget.closest(".poll").data("pollid"),
                choice = jTarget.data("choice");

        jQuery.ajax({
            url: '/polls/' + pollId + '/vote',
            data: {
                choice: choice
            },
            success: function() {
               var bar = jTarget.closest("tr").find(".bar");
               bar.text(+bar.text() + 1).show("highlight");
               jTarget.closest(".pollResults").find(".voteButtons .button").remove();
            }
        })

    }

    jQuery(document).ready(init);

})(jQuery);
