﻿$(document).ready(function() {
    var imgPoll = new Image();
    if (alreadyVoted) //Already voted
    {
        $("#divPoll").css("cursor", "wait"); //show wait cursor inside Poll div while processing
        $("#btnSubmit").attr("disabled", "true") //disable the Vote button while processing
        var data = "{'pID':'" + p + "'}"; //create the JSON data to send to server

        $.ajax({
            //call the Page method using JQuery ajax
            type: "POST",
            url: "/controls/poll.asmx/getResults",
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg)  //show the result
            {
                $("#divPoll").css("cursor", "default"); //remove the wait cursor
                $("#btnSubmit").attr("disabled", "false") //enable the Vote button
                $("div[id$=divAnswers]").fadeOut("fast").html(msg.d).fadeIn("fast", function() { animateResults(); });
            }
        });
    }
    else {
        $("#rdoPoll0").attr("checked", "checked"); //default select the first Choice
        $("#btnSubmit").click(function() {
            $("#divPoll").css("cursor", "wait"); //show wait cursor inside Poll div while processing
            $("#btnSubmit").attr("disabled", "true") //disable the Vote button while processing

            var cID = $("input[name='rdoPoll']:checked").val(); //get the checked Choice
            var data = "{'cID':'" + cID + "'}"; //create the JSON data to send to server

            $.ajax({
                //call the Page method using JQuery ajax
                type: "POST",
                url: "/controls/Poll.asmx/UpdatePollCount",
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg)  //show the result
                {
                    $("#divPoll").css("cursor", "default"); //remove the wait cursor
                    $("#btnSubmit").attr("disabled", "false") //enable the Vote button
                    $("div[id$=divAnswers]").fadeOut("fast").html(msg.d).fadeIn("fast", function() { animateResults(); });
                }
            });

            return false;

        });
    }

    function animateResults() {
        $("div[id$=divAnswers] img").each(function() {
            var percentage = $(this).attr("val");
            $(this).css({ width: "0%" }).animate({ width: percentage }, 'slow');
        });
    }
});