﻿$(document).ready(function () {
    SetupVoteDetails();
});

function SetupAnswerRadioInput() {
    $('#jsAnswers [id*=answerRd_]').each(function () {
        $(this).unbind("click");
        $(this).bind("click", function () {
            var $selectedRadio = $(this);
            $(this).siblings('[id*=answerRd_]:checked').each(function () {
                $(this).attr('checked', false);
            });
        });
    });

}

function SetupVoteDetails() {

    $('.jsPoll').each(function () {
        var Id = $(this).find('[id*=vote_]').attr('id').replace('vote_', '');
        $('#jsVotes_' + Id).children().remove();
        $('#jsAnswers_' + Id).children().remove();
        var DTO = { 'pollId': Id };

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/ajax/pollservice.asmx/GetAnswers",
            data: JSON.stringify(DTO),
            dataType: "json",
            success: function (data) {
                $(data.d).each(function () {
                    $("<input/>", {
                        'id': "answerRd_" + this.AnswerId,
                        'type': "radio",
                        'style': "margin-right:2px;"
                    }).appendTo('#jsAnswers_' + Id);

                    $("<label/>", {
                        'for': "answerRd_" + this.AnswerId,
                        'id': "answerLb_" + this.AnswerId

                    }).html(this.Answer).appendTo('#jsAnswers_' + Id);

                    $("<br/>").appendTo('#jsAnswers_' + Id);

                    $("<li/>", {
                        'id': "answerLi_" + this.AnswerId,
                        //'text': this.Answer + " (" + this.Count + ") " + this.Percent + "%"
                        'text': this.Answer + " " + this.Percent + "%"
                    }).appendTo('#jsVotes_' + Id);
                });
                SetupAnswerRadioInput();
                SetupVoteButton();
            }
        });
    });
}

function SetupVoteButton() {

    //    $('[id^=showResults_]').each(function () {

    //        $(this).unbind("click");
    //        $(this).bind("click", function (e) {
    //            e.preventDefault();



    //            if ($(this).text() == "Show Results") {
    //                
    //                $(this).siblings('#jsVotes').show();
    //                $(this).text("Hide Results");
    //                
    //            }
    //            else {
    //                
    //                
    //                $(this).siblings('#jsVotes').hide();
    //                $(this).text("Show Results");
    //                
    //                
    //            }

    //        });
    //    });

    $('[id*=vote_]').each(function () {
        $(this).unbind("click");
        $(this).bind("click", function (e) {
            e.preventDefault();
            $vote = $(this);
            var pollId = $(this).attr('id').replace('vote_', '');
            var answerSelected = $(this).siblings('#jsAnswers_' + pollId).find('input[type=radio]:checked').length > 0;
            var status = $('#statusLb_' + pollId);
            if (answerSelected) {
                var answerId = $(this).siblings('#jsAnswers_' + pollId).find('input[type=radio]:checked').attr('id').replace('answerRd_', '');
                var DTO = { 'pollId': pollId, 'answerId': answerId };
                $(status).text('');
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/ajax/pollservice.asmx/AddVote",
                    data: JSON.stringify(DTO),
                    dataType: "json",
                    success: function (data) {

                        //alert(data.d);
                        $(status).text(data.d);
                        SetupVoteDetails();
                        $vote.siblings('#jsVotes_' + pollId).show();
                        $vote.siblings('#jsAnswers_' + pollId).hide();
                        $vote.hide();
                    }
                });
            }
            else {
                $(status).text('Please select an option to vote');
            }
        });
    });
}
