/* Online poll functions */
(function($) {
	$.fn.onlinepoll = function(options)
	{
		return this.each(function()
		{
			$this = $(this);
			setup($this.attr("id"));
		});
	}
	
	function setup(id)
	{
		// Change click event for submit function
		$("#" + id + "Submit").click(function()
		{
			submitPoll(id);
			return false;
		});
	}
	function submitPoll(id)
	{
		var items = 1;
		var checkedItems = "";
		while ($("#" + id + "Opt" + items).length > 0)
		{
			if ($("#" + id + "Opt" + items).attr("checked"))
				checkedItems += $("#" + id + "Opt" + items).val() + ",";
			items++;
		}
		
		if (checkedItems.length == 0)
		{
			alert("You must select an item to vote");
			return;
		}
		
		// Remove last comma
		checkedItems = checkedItems.substring(0, checkedItems.length - 1);
		$.ajax({
			type: "GET",
			url: "/onlinepollvote.asp",
			data: "PollId=" + $("#" + id + "Id").val() + "&Votes=" + checkedItems,
			success: function(data)
			{
				loadResults(id, data);
			}
		});
	}
	
	function loadResults(id, data)
	{
		$("#" + id).fadeTo("slow", 0.1, function()
		{  
			$(this).html(data);
			$(this).fadeTo("slow", 1.0);
		});
	}
})(jQuery);
/* End Online poll functions */
