﻿$(document).ready(function() 
    {   
//				/***** BEGIN: News Accordion Code ******/
//				
//				$('div.accordion> div').hide();  
//        $('div.accordion> h3').click(function() {
//					$('div.accordion> h3').children('p.teaser').show();
//					$(this).children('p.teaser').hide();
//          $(this).next('div').slideToggle('fast')
//          .siblings('div:visible').slideUp('fast');
//        });
//        
//				$("[id*=newsStory2]").slideToggle('fast');
//				$("[id*=newsTeaser2]").hide();
//				/***** END: News Accordion Code ******/
        
        var imgPoll = new Image();
        imgPoll.src = '/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
                $("#widget_01").css("cursor", "wait");
                $("#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: "/poll/poll_CS.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
                    $("#widget_01").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'); 
            });
        }
    });