$j(document).ready(function(){
	$j("#poll-form").submit(function(){
		var selectedOption = null;
		$j("#poll-form input[type='radio']").each(function(){
			if($j(this).attr("checked")){
				selectedOption = this;
			}
		});
		if(selectedOption){
			jQuery.post("/mmlib/includes/drum/vote_on_poll.php", {option: $j(selectedOption).val()}, function(response){
				if(response != "failed"){
					//delete all the list items
					$j("#poll-form").empty();

					$j("#poll-form").append("<p class='success'>Thank you for voting</p>");
					$j("#poll-form").append("<ul>");					
					var barColours = new Array("#ADB145");//"#501A5D", "#949397", "#A89597", "#FF205C", "#C6810F", "#6A7B2B", "#5D9797", "#E36F1E");
					var colourIdx = 0;
					for(var i in response){
						var newOption = $j("<li>");	//create a holder for the new list item
						newOption.html("<p>" + response[i].option + "</p>");		//add the option
						
						var voteBar = $j("<div class='vote-bar'>");	//create a voting result bar
						var voteBarWidth = (response[i].votes_pc / 100) * 180;
						voteBar.css("width", 0);
						voteBar.css("background-color", barColours[colourIdx]);
						colourIdx = (colourIdx + 1) % barColours.length;
						
						var voteCount = $j("<span class='vote-count'></span>");
						voteCount.html(response[i].votes_pc + "%");

						//animate the voting bar, once it's finished fade in the percentage
						voteBar.animate({width: voteBarWidth}, 1000, "swing", function(){
							$j(this).parent().children(".vote-count").fadeIn(800);
						});
						newOption.append(voteBar);		//add it to the holder
						newOption.append(voteCount);
						newOption.append("<br class='clear'>");
						$j("#poll-form ul").append(newOption);	//add list item to the list
					}
				}
			}, "json");
		}

		return false;
	})
})