﻿$(document).ready(function() {
    var imgPoll = new Image();
    imgPoll.src = 'poll/images/red-bar.png';

    if ($("#divVoted").length > 0) //Already voted
    {
        animateResults();
    }
    else {
        $("#rdoPoll0").attr("checked", "checked"); //default select the first Choice
        // Add the page method call as an onclick handler for the Vote button.
        // For more details about how to use JQuery to call Asp.Net AJAX page methods refer follwoing blog posts
        // http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
        // http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/
        $("#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 pID = $("input[id$=hidPollID]").val(); //get Poll ID
            var cID = $("input[name='rdoPoll']:checked").val(); //get the checked Choice
            var data = "{'pID':'" + pID + "', 'cID':'" + cID + "'}"; //create the JSON data to send to server

            $.ajax(
                { //call the Page method using JQuery ajax
                    type: "POST",
                    url: "default.aspx/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(); });
                    }
                });
        });
    }

    function animateResults() {
        $("div[id$=divAnswers] img").each(function() {
            var percentage = $(this).attr("val");
            $(this).css({ width: "0%" }).animate({ width: percentage }, 'slow');
        });
    }
});

