﻿/// <reference path="global.js" />
/// <reference path="jquery-1.3.2-vsdoc.js" />

/************************************************************************************************************
* Poll
***********************************************************************************************************/

function RenderPoll(obj, data) {
	var poll = $(obj),
		total = data.total,
		item, percentValue, rightValue, leftValue;
	
	// clears all child nodes
	poll.empty();
	poll.append("<h2>" + data.question + "</h2>");
	poll.append("<ul class=\"poll-options\">");
	
	// go through each option and render it
	for(var i = 0; i < data.options.length; i++) {
		item = data.options[i];
		percentValue = Math.round(item.votes / total * 100);

		poll.append("<li class=\"option\" id=\"option-" + item.optionId + "\">"
		+ "<h3>" + item.text + "</h3>"
		+ "<div class=\"graph\"><img src=\"/Content/images/poll-graph.gif\" height=\"10\" width=\"" + percentValue + "%\" /></div>"
		+ "<div class=\"values\">" + percentValue + "% (" + item.votes + " votes)</div>"
		+ "</li>");
	}
	
	poll.append("</ul>");
	poll.append("<div class=\"total\">There are " + total + " total votes for this poll.</div>");
}

$(".poll form").submit(function() {
	var selection = $(this).find(":checked").val();

	if (selection != undefined) {
		$.post(
			"/poll/vote",
			{ optionId: selection },
			function(data, textStatus) {
    			$.cookie('poll_' + data.pollId, selection, { path: '/' });
				// render the poll for the given data
				RenderPoll($("#poll-" + data.pollId), data);
			},
			"json"
		);
	}

	return false;
});