﻿$(document).ready(function () {  
    $('#jViewPoll').boxy({title: "Viewing Poll", closeable: true, draggable: true, modal: false, cache: false, modal: true, closeText: "x",
    afterShow: function() {
        // get the Poll Id
        $("#jOptions").html("");
        var pId = $("#hidPollId").val();
        
        // load Poll XML.
        $.ajax ({
            type: "POST",
            cache: false,
            url: "Resource/ajax/getPollData.aspx",
            data: "poll="+pId,
            dataType: "xml",
            success: handlePollArchiveLoadPass,
            error: handlePollArchiveLoadError
        });
    }});
    
    $("#jVotePoll").click(function () { // on VOTE CLICK
        $("input[id*='pOpt']").each( // for each radio button...
            function () {
                if($(this).attr("checked")) {
                    var rx=/([0-9]*)_pOpt_([0-9]*)/
                    var id = $(this).attr("id");
                    var matches = (id.match(rx));
                    var pId = matches[1];
                    var oId = matches[2];
                    
                    // register the vote...
                    $("#jSidebarOptions").slideUp(800, function() {
                        $("#jSidebarPollLoader").css("visibility","visible");
                        $("#jSidebarPollLoader").css("display","block");
                        $("#jSidebarPollLoader").slideDown(800, function() {
                            $.ajax ({
                                type: "POST",
                                cache: false,
                                url: "Resource/ajax/registerPollVote.aspx",
                                data: "poll="+pId+"&option="+oId,
                                dataType: "xml",
                                success: handleVotePass,
                                error: handleVoteError
                            });
                        });
                    });
                }
            }
        );
        return false;
    });
});


function handleVoteError(XMLHttpRequest, textStatus, errorThrown) {
    $("#jPollErr").remove();
    $("#jSidebarPollLoader").slideUp(600, function() {
        var html = $("#jSidebarOptions").html();
        $("#jSidebarOptions").html(html+"<p class='question' id='jPollErr'><br />Due to a server error your vote was not counted. Please try again later.</p>");
        $("#jSidebarOptions").slideDown(600);
    });
}

function handleVotePass(xml, textStatus) {
    if($(xml).find("error").length<1) {
        $("#jPollErr").remove();
        $("#jSidebarPollLoader").slideUp(600, function() { // hide loader
            // show the rest!
            var maxPixels = 150;
            var totalVotes = parseInt($(xml).find("totalvotes").text());
            var resDiv="<div id='jPollResultsSidebar'>";
            var optVotes = Array();
            var optIds   = Array();
            var i=0;
            $(xml).find("option").each(function() {
                var oId=$(this).find("id").text();
                var ans=$(this).find("answer").text();
                var vot=$(this).find("votes").text()
                
                optVotes[i]=vot;
                optIds[i]=oId;
                
                resDiv+="<div><p class='question'><strong>"+ans+"</strong> ("+vot+")<br />";
                resDiv+="<img id='jPollSidebarResult-"+i+"'src='Resource/gfx/poll-vote-bar.gif' style='margin-top: 5px; height: 10px; width: 1px;'/>";
                resDiv+="</p></div>";
                i++;
            });
            resDiv+="</div>";
            $("#jSidebarOptions").html(resDiv);
            $("#jSidebarOptions").slideDown(600, function() {
                // Resize images!
                for(i=0;i<optIds.length;i++) {
                    var perc= Math.floor((parseInt(optVotes[i])/totalVotes)*100);
                    var pxs = Math.floor((maxPixels/100)*perc);
                    var eId = "#jPollSidebarResult-"+i;
                    $(eId).animate({width: pxs+"px"},400);
                }
            });
        });
    } else {
        handleVoteError(null,null,null);
    }
}

function handlePollArchiveLoadError(XMLHttpRequest, textStatus, errorThrown) {
    //var html = $("#jOptions").html();
    $("#jArchivedPollOptions").html("<div id='jPollArchiveErr'><h2><span class='red'>Error</span></h2><p>The Poll you requested could not be displayed or does not exist. Please try again.</p></div>");
}

function handlePollArchiveLoadPass(xml, textStatus) {
    if($(xml).find("error").length<1) {
        if($(xml).find("id").text()=="0") {
            handlePollArchiveLoadError(null,null,null);
        } else {
            $("#jPollArchiveErr").remove();
            // show the rest!
            var maxPixels = 480;
            var totalVotes = parseInt($(xml).find("totalvotes").text());
            var resDiv="<div id='jPollResultsSidebar'>";
            var optVotes = Array();
            var optIds   = Array();
            var i=0;
            $(xml).find("option").each(function() {
                var oId=$(this).find("id").text();
                var ans=$(this).find("answer").text();
                var vot=$(this).find("votes").text()
                
                optVotes[i]=vot;
                optIds[i]=oId;
                
                resDiv+="<div class='archived-poll-option'><p class='archived-poll-question'><strong>"+ans+"</strong> ("+vot+")<br />";
                resDiv+="<img id='jArchivedPollResult-"+i+"'src='Resource/gfx/poll-vote-bar-big.gif' style='margin-top: 5px; height: 20px; width: 1px;'/>";
                resDiv+="</p></div>";
                i++;
            });
            resDiv+="</div>";
            $("#jArchivedPollOptions").html(resDiv);
            
            // Resize images!
            for(i=0;i<optIds.length;i++) {
                var perc= Math.floor((parseInt(optVotes[i])/totalVotes)*100);
                var pxs = Math.floor((maxPixels/100)*perc);
                var eId = "#jArchivedPollResult-"+i;
                $(eId).animate({width: pxs+"px"},800);
            }
        }
    } else {
        handlePollArchiveLoadError(null,null,null);
    }
}