﻿var curPollIdx = 0;

$(document).ready(function() {
    $("#blogNavBtn").addClass("on");

    $("#topBarDropDown").hide(); // Not relevant

    // Move any element id'd "contentTitle" up over the banner image
    $("#bannerTitleInner").append($("#contentTitle"));

    loadPoll();
});

function loadPoll() {
    $("#pollBoxContent").fadeOut();
    $("#pollBoxOverlay").fadeIn("fast", function() {
        // Poll collection ID is stored in hiddenPollIDContainer's hidden input field
        var pollCollID = $("#hiddenPollIDContainer input:first").val();
        if (pollCollID != null && !isNaN(parseInt(pollCollID))) {
            $("#pollBoxContent").load("/ajaxcontent/BlogPoll.aspx?collid=" + pollCollID + "&pollIdx=" + curPollIdx + " #polls > *", function() {
                // Wire up button click events
                $("#rblAnswers").attr("cellspacing", "0").attr("cellpadding", "0");
                $("#btnSubmitPoll").click(function() {
                    if ($(".pollQuestions:first input:checked").length > 0) {
                        var pollID = $(".pollQuestions:first input:hidden:first").val();
                        submitPoll(pollID);
                    }
                });
                // Fade transparent overlay
                $("#pollBoxOverlay").fadeOut("fast");
                $("#pollBoxContent").fadeIn();
            });
        }
        else {
            $("#pollContainer").fadeOut("fast");
        }
    });
}

function nextPoll() {
    curPollIdx = parseInt($("#pollBoxContent input:hidden:last").val());
    loadPoll();
}

function submitPoll(pollID) {
    var checkedControls = $(".pollQuestions:first input:checked");
    if (checkedControls.length > 0) {
        // Fade in overlay
        $(".pollQuestions:first").fadeOut(function() {
            $("#pollBoxOverlay").fadeIn(function() {
                $.ajax({
                    type: "GET",
                    url: "/SubmitPoll.aspx",
                    data: {
                        pollID: pollID,
                        choice: $(checkedControls[0]).val(),
                        oneTime: "false"
                    },
                    dataType: "html",
                    cache: false,
                    beforeSend: function() {
                        // Disable the submit button
                        $("#btnSubmit").attr("disabled", "true");
                    },
                    success: function(html) {
                        loadPollResults(pollID);
                    },
                    error: function(xhr) {
                        $("#pollBoxContent").html("<h4>We're sorry.  An error has occurred with our polling system.  Please check back later.</h4>");
                        $("#pollBoxOverlay").fadeOut("fast");
                    }
                });
            });
        });
    }
}

function loadPollResults(pollID, cb) {
    var nextPollIdx = $("#pollBoxContent input:hidden:last").val();
    var showNextLink = (parseInt(nextPollIdx) == curPollIdx ? "0" : "1");
    $("#pollBoxContent").load("/PollResults.aspx?id=" + pollID + "&showNextLink=" + showNextLink + " #pollResults", function() {
        $(".pollResultValue").hide();
        $("#pollBoxOverlay").fadeOut("fast", function() {
            $(".pollResultRow").each(function() {
                // Extract the width value to use in animation
                var pct = $(".pollAnswerPct:first", this);
                if (pct != null) {
                    // pct is a string containing a floating-point number (2 dec places) and a % sign at the end.
                    // Truncate everything after the decimal point to get an integer.
                    var wholePct = pct.html().split(".")[0];
                    if (parseInt(wholePct) == 0) {
                        $(".pollResultValue", this).hide();
                    }
                    else {
                        $(".pollResultValue", this).animate({ width: wholePct + "%" }, 1500, "swing");
                    }
                }
            });
        });
        $("#pollBoxContent").append("<input type=\"hidden\" value=\"" + nextPollIdx + "\" />");

        if (cb != null) { cb(); }
    });
}