﻿
if (!HasValidSession())
{
	LoginRedirect();	
}

if (ReadCookie("PlayerRole") == "Admin")
{
	var createPollLink = document.createElement("a");
	createPollLink.href = "createPoll.htm";
	createPollLink.innerHTML = "Create a new poll";
	document.getElementById("pollContainer").insertBefore(document.createElement("br"), document.getElementById("pollContainer").firstChild);
	document.getElementById("pollContainer").insertBefore(document.createElement("br"), document.getElementById("pollContainer").firstChild);
	document.getElementById("pollContainer").insertBefore(createPollLink, document.getElementById("pollContainer").firstChild);
}

PollingServices.GetCommunityPollList(EndGetCommunityPollList, null);

function EndGetCommunityPollList(response, state)
{
	if (!HandleCommonResults(response))
	{
		if (IsSuccessResult(response))
		{
			var polls = new Array();

			for (var i = 0; i < response.Polls.length; i++)
			{
				var poll = response.Polls[i];
				poll.Options = new Array();
				polls[poll.CommunityPollId] = poll;
			}

			for (var i = 0; i < response.Options.length; i++)
			{
				var option = response.Options[i];

				polls[option.PollId].Options.push(option);
			}

			for (var i = 0; i < response.Polls.length; i++)
			{
				var poll = response.Polls[i];
				var container = document.createElement("div");
				SetClass(container, "pollOptionsContainer");
				var pageLocation;

				var questionText = document.createElement("a");
				questionText.innerHTML = poll.PollQuestion;

				if (poll.IsOpen)
				{
					if (ReadCookie("PlayerRole") == "Admin")
					{
						var viewLink = document.createElement("a");
						viewLink.href = "pollDetails.htm?id=" + poll.CommunityPollId;
						viewLink.innerHTML = "view details";
						container.appendChild(viewLink);
						container.innerHTML += " - ";

						pageLocation = document.getElementById("openPollContainer");
						container.appendChild(questionText);
					}
					else
					{
						pageLocation = document.getElementById("openPollContainer");
						container.appendChild(questionText);

						if (poll.HasVoted)
						{
							questionText.href = "pollDetails.htm?id=" + poll.CommunityPollId;
							var selectedOption = document.createElement("span");
							selectedOption.innerHTML = "--> " + poll.PreferredOption;
							container.appendChild(selectedOption);
							container.appendChild(document.createElement("br"));
						}
						else
						{
							questionText.href = "#";
							questionText.onclick = ExpandPoll;
							container.appendChild(document.createElement("br"));

							var optionContainer = document.createElement("div");
							//uncomment to hide all radio option lists by default
							//optionContainer.style.display = "none";
							questionText.OptionContainer = optionContainer;

							for (var iOption = 0; iOption < poll.Options.length; iOption++)
							{
								var option = poll.Options[iOption];

								var radioButton = document.createElement("input");
								radioButton.type = "radio";
								radioButton.Poll = poll;
								radioButton.Option = option;
								radioButton.name = option.PollId;
								radioButton.value = option.PollOptionId;
								radioButton.onclick = SelectPollOption;

								var radioLabel = document.createElement("span");
								radioLabel.innerHTML = option.PollOptionText;

								optionContainer.appendChild(radioButton);
								optionContainer.appendChild(radioLabel);
								optionContainer.appendChild(document.createElement("br"));
							}

							container.appendChild(optionContainer);
						}
					}
				}
				else
				{
					questionText.href = "pollDetails.htm?id=" + poll.CommunityPollId;
					questionText.onclick = ViewPollDetails;
					
					pageLocation = document.getElementById("closedPollContainer");
					container.appendChild(questionText);
				}

				pageLocation.appendChild(container);
			}
		}
		else
		{
			UnknownResult(response);
		}		
	}
}

function ExpandPoll(e)
{
	if (e == null)
	{
		e = window.event;
	}

	var link = GetEventSource(e);
	var optionContainer = link.OptionContainer;

	if (optionContainer.style.display == "none")
	{
		optionContainer.style.display = "block";
	}
	else
	{
		optionContainer.style.display = "none"
	}
}

function SelectPollOption(e)
{
	if (e == null)
	{
		e = window.event;
	}

	var radio = GetEventSource(e);
	var option = radio.Option;

	PollingServices.SetCommunityPollPreference(option.PollOptionId, EndSetCommunityPollPreference, radio);
}

function EndSetCommunityPollPreference(response, radio)
{
	if (!HandleCommonResults(response))
	{
		if (IsSuccessResult(response))
		{
			var poll = radio.Poll;
			var option = radio.Option;
			var container = radio.parentNode.parentNode;

			ClearContainer(container);
			
			var pollLink = document.createElement("a");
			pollLink.innerHTML = poll.PollQuestion;
			pollLink.href = "pollDetails.htm?id=" + poll.CommunityPollId;
			container.appendChild(pollLink);

			var selectedOption = document.createElement("span");
			selectedOption.innerHTML = "--> " + option.PollOptionText;
			container.appendChild(selectedOption);
		}
		else
		{
			UnknownResult(response);
		}
	}
}

function ViewPollDetails()
{
	
}
