﻿$(function () {
    MSvillage.poll.init();
});

MSvillage.feedContainer = {

    resizeFeedsContainer: function () {

        var containerHeight = $("#bd-l").height(),
            feedContainerHeight = $("#bd-content").height(),
            feedsHeight = $("#feeds").height();

        if (containerHeight > feedContainerHeight) {
            var newHeight = feedsHeight + (containerHeight - feedContainerHeight);
            $("#feed-content").height(newHeight - 60);
        }

    }

};

MSvillage.poll = {
    $pollHtmlContainer: null,
    localeId: "4105",
    pollId: null,
    totalVotes: 0,
    answerBarMaxWidth: 150,
    init: function () {

        this.$pollHtmlContainer = $("#splash-poll");
        if (this.$pollHtmlContainer.length) {
            this.localeId = $("#lcid").val();
            this.$pollHtmlContainer.show().click(this.onPollContainerClick);
            this.getCurrentPoll(MSvillage.feedContainer.resizeFeedsContainer);
        }

    },
    getCurrentPoll: function (callback) {

        $.ajax({
            type: 'POST',
            url: '/services/msvillage.asmx/GetCurrentPoll',
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            data: '{"locale": ' + this.localeId + '}',
            success: function (data) {
                var pollHtml = '';
                if (data.d) {

                    MSvillage.poll.pollId = data.d.PollID;
                    MSvillage.poll.totalVotes = data.d.NumVotes;

                    // build html
                    pollHtml = '<p>' + data.d.Poll + '</p>';
                    pollHtml += '<form method="POST" action="#" id="splash-poll-form">';

                    $.each(data.d.Answers, function (i, item) {
                        pollHtml += '<input type="radio" class="poll-option" name="poll" id="poll_' + item.AnswerID + '" value="' + (i + 1) + '" /><label class="poll-option" for="poll_' + item.AnswerID + '">' + item.Answer + '</label><div class="splash-poll-resultsbar">' + item.NumVotes + '</div>';
                    });

                    pollHtml += '<p id="splash-poll-buttons">';
                    pollHtml += '<a href="#" id="splash-poll-vote">' + MSvillage.localization.get().vote + '</a>';
                    pollHtml += '<a href="#" id="splash-poll-results">' + MSvillage.localization.get().results + '</a>';
                    pollHtml += '</p>';
                    pollHtml += '</form>';

                } else {
                    MSvillage.poll.$pollHtmlContainer.hide();
                }
                MSvillage.poll.$pollHtmlContainer.append(pollHtml);
            },
            complete: callback
        });

    },
    vote: function () {
        var rank = $("input:radio:checked", MSvillage.poll.$pollHtmlContainer).val();
        if (rank) {

            $.ajax({
                type: 'POST',
                url: '/services/msvillage.asmx/InsertPollVote',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: '{"pollID": ' + MSvillage.poll.pollId + ', "rank": ' + rank + ', "locale": ' + MSvillage.poll.localeId + '}',
                error: function () {
                    alert(MSvillage.localization.get().votingError);
                },
                success: function (data) {

                    // don't worry about messaging for those who already voted.
                    if (data.d === 0) {
                        // add one vote to total votes
                        MSvillage.poll.totalVotes += 1;
                        // add one vote to div
                        var $resultsBar = $("div:eq(" + (parseInt(rank) - 1) + ")", MSvillage.poll.$pollHtmlContainer);
                        $resultsBar.text(parseInt($resultsBar.text()) + 1);

                    }
                    MSvillage.poll.showResults();
                    $("#splash-poll-vote").hide();
                }
            });

        } else {
            alert(MSvillage.localization.get().optionSelectionRequired);
        }

    },
    showResults: function () {
        $("#splash-poll-results").hide();
        $("label", MSvillage.poll.$pollHtmlContainer).addClass("inline");
        $("input:radio", MSvillage.poll.$pollHtmlContainer).each(function () {
            $(this).hide();

            var $resultsBar = $(this).next().next(".splash-poll-resultsbar"),
                percentage = 0,
                votes = parseInt($resultsBar.text()),
                barPixelWidth = 1;

            if (MSvillage.poll.totalVotes > 0) {
                percentage = votes / MSvillage.poll.totalVotes;
                barPixelWidth = Math.round(percentage * MSvillage.poll.answerBarMaxWidth);
            }

            $resultsBar.attr("title", ((votes > 0) ? Math.round(percentage * 100) : 0) + "%").attr("style", "width:" + ((barPixelWidth > 0) ? barPixelWidth : 1) + "px;").show();

        });

    },
    hideResults: function () {
        $("#splash-poll-results").show();
        $("label", MSvillage.poll.$pollHtmlContainer).removeClass("inline");
        $("input:radio", MSvillage.poll.$pollHtmlContainer).show();
        $("div", MSvillage.poll.$pollHtmlContainer).hide();
    },
    onPollContainerClick: function (e) {

        var $target = $(e.target);

        if ($target.is("#splash-poll-vote")) {
            if ($("#splash-poll-results").is(":visible")) {
                MSvillage.poll.vote();
            } else {
                MSvillage.poll.hideResults();
            }
            return false;
        } else if ($target.is("#splash-poll-results")) {

            MSvillage.poll.showResults();

            return false;
        }

    }

};

