﻿//<!--
// please note that this is a 1 per page implementation of polls
// for more than one instance per page this code will need to be changed
// this also assumes a single question poll with 1 option selected

$("#poll").ready(function() {

	Salem.RegisterNamespace("Poll");
	Salem.Poll = function(serviceUrl) {
		var Instance = this;
		this.Service = new Salem.Ajax.Service(serviceUrl);
		this.surveyId = $("div#poll").attr("SurveyId");
		this.cookieKey = $("div#poll").attr("CookieKey");
		this.cookieExpiresInDays = $("div#poll").attr("DaysBetweenVotes");
		this.surveyQuestionCtl = $("div#poll div.question");
		this.surveyQuestionId = this.surveyQuestionCtl.attr("SurveyQuestionId");
		this.optionCtls = $("div#poll div.options div.option");
		this.resultCtls = $("div#poll div.options div.result");
		this.btnVote = $("div#poll div.button input");
		this.msgVote = $("div#poll div.message label");

		this.SetCookieForSurvey = function() {
			$.cookie(this.cookieKey, 'voted', { expires: this.cookieExpiresInDays, path: '/' });
		};

		this.SetDisplay = function() {
			if ($.cookie(this.cookieKey) == null) {
				this.resultCtls.hide();
				this.btnVote.show();
				this.msgVote.show();
				this.optionCtls.show();
			} else {
				this.btnVote.hide();
				this.msgVote.hide();
				this.optionCtls.hide();
				this.SetResults();
				this.resultCtls.show();
			}
		};

		this.SubmitResponseOnSuccess = function(results) {
			if (results.Error) {
				alert("Sorry an error occured submitting your vote. Please try again.");
			} else {
				Instance.IncrementTallyForSurvey();
				Instance.SetCookieForSurvey();
				Instance.SetDisplay();
			}
		};

		this.IncrementTallyForSurvey = function() {
			Instance.OptionsChecked().each(function() {
				var result = Instance.resultCtls.filter("[SurveyResponseId=" + $(this).attr("SurveyResponseId") + "]");
				result.attr("Tally", Number(result.attr("Tally")) + 1);
				Instance.surveyQuestionCtl.attr("Tally", Number(Instance.surveyQuestionCtl.attr("Tally")) + 1);
			});
		}

		this.SetResults = function() {
			var maxwidth = $("div#poll div.wrapper").width() - 60;
			var tally = this.surveyQuestionCtl.attr("Tally");
			this.resultCtls.each(function() {
				$(this).find("span").html(Math.floor(100 * $(this).attr("Tally") / tally) + '%');
				$(this).find("div").width(Math.floor(maxwidth * $(this).attr("Tally") / tally));
			});
		}

		this.OptionsChecked = function() {
			return $("div#poll div.options div.option input:checked");
		}

		this.VoteJSON = function() {
			var returnValue = '';
			var selectedResponseValue = this.OptionsChecked().attr("SurveyResponseId");
			var responseJsonString = Salem.String.Format('"SurveyResponses":[{"SurveyResponseId":{0}}]', selectedResponseValue);
			var questionJsonString = Salem.String.Format('"SurveyQuestions":[{"SurveyQuestionId":{0},{1}}]', this.surveyQuestionId, responseJsonString);
			returnValue = Salem.String.Format('{"SurveyId":{0},{1}}', this.surveyId, questionJsonString);
			return returnValue;
		};

		this.AjaxError = function(err) {
			alert("Sorry an error occured submitting your vote. Please try again.");
		};

		this.SubmitResponse = function() {
			if (Instance.OptionsChecked().size() == 0) {
				alert("Please select an option before voting.");
			} else {
				Instance.Service.JsonGet("SubmitSurveyResponseVote", "VoteJSONString=" + Instance.VoteJSON(), Instance.SubmitResponseOnSuccess, Instance.AjaxError);
			}
		};

		// bind events to functions declared above
		this.btnVote.click(this.SubmitResponse);
		this.SetDisplay();

	};

	var poll = new Salem.Poll($("div#poll").attr("ProxyServiceUrl"));

});

// -->
