jQuery(document).ready(function($) {
	$('form.poll').each(function() {
		var $this = $(this),
			handle = $('input[name=handle]', $this).val();
		
		/* GET request when form is first loaded. */
		$.ajax({
			type: 'GET',  
			url: handle +".html",
			cache: false, // make sure requests are not cached
			data: $this.serialize(),  
			success: function(data) {
				$('fieldset', $this).html(data);
			}
		});

		/* POST request when form is submitted */
		$this.submit(function() {
		    var $this = $($(this)[0]);
			
			$.ajax({
				type: 'POST',  
				url: handle +".html?",
				data: $this.serialize(),  
				success: function(data) {
					if (data.indexOf('poll-error') >= 0) {
						$('.poll-message', $this).show().html(data);
					} else {
			            $('fieldset', $this).html(data);
					}
				}
			});
			return false;
		});
	});
});

