function get_id(id, pattern) {
    var id_re = new RegExp(pattern);
    return id_re.exec(id)[1];
}

function apply_poll_ajax(selector) {
	selector.each(function () {
        var poll_id = get_id(this.id, 'poll(\\d+)form');
        
        var options = { 
        	beforeSubmit: function() {
        		$('#poll' + poll_id + 'button').replaceWith('<img src="/files/design/icons/loading.gif" width="35" height="35" alt="..."/>');
        	},
        
		    success: function(data) {
		        $.get('/polls/'+poll_id+'/ajax/', function(results, textStatus) {
					$('#poll'+poll_id).replaceWith(results)
		        	$('#poll'+poll_id+'voted').show()
		        });
		    }
		};
	
		$(this).ajaxForm(options);
	})
}

$(document).ready(function() {
    apply_poll_ajax($('.pollform'));
    $('.poll_choice').click(function() {
        var poll_id = get_id(this.id, 'poll(\\d+)choice');
        
		$('#poll' + poll_id + 'button').enable(true)   	
    })
});