﻿function SWRVdownPollSubmitVote(
    gameID,
    userID,
    radioButtonIDs,
    tallyLabelIDs,
    voteButtonID,
    votingImg,
    voteAgainButtonID,
    voteContentBodyID,
    messageLabelID,
    thankYouMsg,
    thankYouMsgCSSClass,
    gameOverMsg,
    gameOverMsgCSSClass,
    winnerDivID,
    winnerLabelID,
    voteImg
)
{
    //Get selected value
    var selectedRadioButton = GetSelectedRadioButton(radioButtonIDs);
    if (selectedRadioButton == null || selectedRadioButton.length == 0)
        return;

    //Switch vote button image to VOTING image
    $("#" + voteButtonID).attr("src", votingImg);
    $("#" + voteButtonID).attr('disabled', 'disabled');

    setTimeout("SubmitVoteAndUpdateControls('" + selectedRadioButton.value + "'," +
        "'" + userID + "', " +
        gameID + ", " +
        "'" + radioButtonIDs + "', " +
        "'" + tallyLabelIDs + "', " +
        "'" + voteButtonID + "', " +
        "'" + voteAgainButtonID + "', " +
        "'" + messageLabelID + "', " +
        "'" + thankYouMsg + "', " +
        "'" + thankYouMsgCSSClass + "', " +
        "'" + gameOverMsg + "', " +
        "'" + gameOverMsgCSSClass + "', " +
        "'" + winnerDivID + "', " +
        "'" + winnerLabelID + "')", 100);
    /*
    //Submit vote
    $.ajax
            ({
                type: "POST",
                contentType: "application/json",
                url: "AJAXServices/InteractivityService.svc/AddSubmittal",
                data: '{ "Identifier": "' + selectedRadioButton.value + '", "UserId": "' + userID + '", "Text": "", "ImageId": "", "SubmitterURI": "" }',
                processData: true,
                async: false
            });

    //alert('vote submitted');

    //Get latest results and update controls
    //GetResultsAndUpdateTallyLabels(gameID, radioButtonIDs, tallyLabelIDs);
    GetResultsAndUpdateSWRVdownPollControls(
        gameID,
        radioButtonIDs,
        tallyLabelIDs,
        voteButtonID,
        voteAgainButtonID,
        messageLabelID,
        thankYouMsg,
        thankYouMsgCSSClass,
        gameOverMsg,
        gameOverMsgCSSClass,
        winnerDivID, 
        winnerLabelID);
    */    
}


function SubmitVoteAndUpdateControls(
    selectedRadioButtonValue,
    userID, 
    gameID,
    radioButtonIDs,
    tallyLabelIDs,
    voteButtonID,
    voteAgainButtonID,
    messageLabelID,
    thankYouMsg,
    thankYouMsgCSSClass,
    gameOverMsg,
    gameOverMsgCSSClass,
    winnerDivID,
    winnerLabelID)
{
    //Needed so IE will update with VOTING button before script finishes. I hate IE.
    //Submit vote
    //2/1/2010 - Changed async to TRUE
    $.ajax
            ({
                type: "POST",
                contentType: "application/json",
                url: "AJAXServices/InteractivityService.svc/AddSubmittal",
                data: '{ "Identifier": "' + selectedRadioButtonValue + '", "UserId": "' + userID + '", "Text": "", "ImageId": "", "SubmitterURI": "" }',
                processData: true,
                async: true
            });

    //alert('vote submitted');

    //Get latest results and update controls
    //GetResultsAndUpdateTallyLabels(gameID, radioButtonIDs, tallyLabelIDs);
    GetResultsAndUpdateSWRVdownPollControls(
        gameID,
        radioButtonIDs,
        tallyLabelIDs,
        voteButtonID,
        voteAgainButtonID,
        messageLabelID,
        thankYouMsg,
        thankYouMsgCSSClass,
        gameOverMsg,
        gameOverMsgCSSClass,
        winnerDivID,
        winnerLabelID);
}


function GetResultsAndUpdateSWRVdownPollControls(
    gameID,
    radioButtonIDs,
    tallyLabelIDs,
    voteButtonID,
    voteAgainButtonID,
    messageLabelID,
    thankYouMsg,
    thankYouMsgCSSClass,
    gameOverMsg,
    gameOverMsgCSSClass,
    winnerDivID,
    winnerLabelID)
{
    var results;

    //2/1/2010 - Changed async to TRUE
    $.ajax
    ({
        type: "POST",
        contentType: "application/json",
        url: "AJAXServices/GamesService.svc/GetGameResult",
        data: '{ "GameID": "' + gameID + '" }',
        processData: true,
        async: true,
        success: function(msg)
        {
            results = RemoveMSJSONEnvelope(msg)
            //UpdateTallyLabels(msgWithoutEnvelope, radioButtonIDs, tallyLabelIDs);
            SWRVdownPollUpdateControls(
                results,
                radioButtonIDs,
                tallyLabelIDs,
                voteButtonID,
                voteAgainButtonID,
                messageLabelID,
                gameOverMsg,
                gameOverMsgCSSClass,
                thankYouMsg,
                thankYouMsgCSSClass,
                winnerDivID, 
                winnerLabelID);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Error occurred getting game results: " + errorThrown); }
    });

    /*
    if (results == null || results.Contestants == null || results.Contestants.length == 0)
        return;
    else
    {
        //alert('hey - radioButtonIDs = ' + radioButtonIDs + ', tallyLabelIDs = ' + tallyLabelIDs);
        var radiobuttons = $(radioButtonIDs);
        var tallylabels = $(tallyLabelIDs);

        if (radiobuttons.length == 0) return;

        //Loop through contestants in results.
        //Match contestant identifier with radio button value -- if equal, assign label at same index the
        //value of the result.
        var contestant;
        for (contestant = 0; contestant < results.Contestants.length; contestant++)
        {
            var i;
            for (i = 0; i < radiobuttons.length; i++)
            {
                if (radiobuttons[i].value == results.Contestants[contestant].Identifier)
                {
                    tallylabels[i].innerHTML = results.Contestants[contestant].ScorePercent + "%";
                    break;
                }
            }
        }

        //Make sure tally labels are visible
        $(tallyLabelIDs).css("display", "");

        //Hide vote button
        $("#" + voteButtonID).css("display", "none");

        $(radioButtonIDs).css("display", "none");

        //Remove any classes applied to the message label
        $("#" + messageLabelID).removeClass();

        //if (bGameClosed)
        if (results.Status == 2)
        {
            SWRVdownPollShowClosedState(radioButtonIDs, voteAgainButtonID, messageLabelID, gameOverMsg, gameOverMsgCSSClass, winnerDivID, winnerLabelID, results);

            //Hide vote again button
            //$("#" + voteAgainButtonID).css("display", "none");

            //Set game over message and apply style
            //$("#" + messageLabelID).addClass(gameOverMsgCSSClass).css("display", "");
            //$("#" + messageLabelID).html(gameOverMsg);

            //Show winner
            //var iWinnerIndex = GetIndexOfWinner(results);

            //alert('WinnerIndex = ' + iWinnerIndex);
            
            //for (var i = 0; i < radiobuttons.length; i++)
            //{
                //if (radiobuttons[i].value == results.Contestants[iWinnerIndex].Identifier)
                //{

                    //alert("Winning radio button = " + radiobuttons[i].id);
                    
                    //var radiobuttonlabeltext = $('input[name=' + radiobuttons[i].name + '] + label').text();
                    //var radiobuttonlabeltext = $('#' + radiobuttons[i].id + ' + label').text();

                    //alert('radiobuttonlabeltext = ' + radiobuttonlabeltext);

                    //alert('winnerDivID = ' + winnerDivID);
                    
                    //$("#" + winnerLabelID).html(radiobuttonlabeltext);
                    //$("#" + winnerDivID).css("display", "");
                    //break;
                //}
            //}
        }
        else
        {
            //Show vote again button
            $("#" + voteAgainButtonID).css("display", "");

            //Set thank you message and apply style
            $("#" + messageLabelID).css("display", ""); //ACH -- Added this 10/27/2009 to accomodate SWRVdown
            $("#" + messageLabelID).addClass(thankYouMsgCSSClass);

            $("#" + messageLabelID).html(thankYouMsg);
        }
    }

    $("#" + voteButtonID).attr('disabled', '');
    */
}


function SWRVdownPollUpdateControls(
    results,
    radioButtonIDs,
    tallyLabelIDs,
    voteButtonID,
    voteAgainButtonID,
    messageLabelID,
    gameOverMsg,
    gameOverMsgCSSClass,
    thankYouMsg,
    thankYouMsgCSSClass,
    winnerDivID, 
    winnerLabelID)
{
    if (results == null || results.Contestants == null || results.Contestants.length == 0)
        return;
    else
    {
        //alert('hey - radioButtonIDs = ' + radioButtonIDs + ', tallyLabelIDs = ' + tallyLabelIDs);
        var radiobuttons = $(radioButtonIDs);
        var tallylabels = $(tallyLabelIDs);

        if (radiobuttons.length == 0) return;

        //Loop through contestants in results.
        //Match contestant identifier with radio button value -- if equal, assign label at same index the
        //value of the result.
        var contestant;
        for (contestant = 0; contestant < results.Contestants.length; contestant++)
        {
            var i;
            for (i = 0; i < radiobuttons.length; i++)
            {
                if (radiobuttons[i].value == results.Contestants[contestant].Identifier)
                {
                    tallylabels[i].innerHTML = results.Contestants[contestant].ScorePercent + "%";
                    break;
                }
            }
        }

        //Make sure tally labels are visible
        $(tallyLabelIDs).css("display", "");

        //Hide vote button
        $("#" + voteButtonID).css("display", "none");

        $(radioButtonIDs).css("display", "none");

        //Remove any classes applied to the message label
        $("#" + messageLabelID).removeClass();

        //if (bGameClosed)
        if (results.Status == 2)
        {
            SWRVdownPollShowClosedState(radioButtonIDs, voteAgainButtonID, messageLabelID, gameOverMsg, gameOverMsgCSSClass, winnerDivID, winnerLabelID, results);
            /*
            //Hide vote again button
            $("#" + voteAgainButtonID).css("display", "none");

            //Set game over message and apply style
            $("#" + messageLabelID).addClass(gameOverMsgCSSClass).css("display", "");
            $("#" + messageLabelID).html(gameOverMsg);

            //Show winner
            var iWinnerIndex = GetIndexOfWinner(results);

            //alert('WinnerIndex = ' + iWinnerIndex);
            
            for (var i = 0; i < radiobuttons.length; i++)
            {
            if (radiobuttons[i].value == results.Contestants[iWinnerIndex].Identifier)
            {

                    alert("Winning radio button = " + radiobuttons[i].id);
                    
            //var radiobuttonlabeltext = $('input[name=' + radiobuttons[i].name + '] + label').text();
            var radiobuttonlabeltext = $('#' + radiobuttons[i].id + ' + label').text();

                    alert('radiobuttonlabeltext = ' + radiobuttonlabeltext);

                    //alert('winnerDivID = ' + winnerDivID);
                    
            $("#" + winnerLabelID).html(radiobuttonlabeltext);
            $("#" + winnerDivID).css("display", "");
            break;
            }
            }
            */
        }
        else
        {
            //Show vote again button
            $("#" + voteAgainButtonID).css("display", "");

            //Set thank you message and apply style
            $("#" + messageLabelID).css("display", ""); //ACH -- Added this 10/27/2009 to accomodate SWRVdown
            $("#" + messageLabelID).addClass(thankYouMsgCSSClass);

            $("#" + messageLabelID).html(thankYouMsg);
        }
    }

    $("#" + voteButtonID).attr('disabled', '');
}


function SWRVdownPollResetControls(
    gameID,
    radioButtonIDs,
    tallyLabelIDs,
    controlsToShowIfGameOpen,
    controlsToHideIfGameOpen,
    controlsToShowIfGameClosed,
    controlsToHideIfGameClosed,
    messageLabelID,
    messageLabelCSSClass,
    messageLabelGameClosedText,
    winnerDivID,
    winnerLabelID,
    voteButtonID,
    voteImg)
{
    //TODO: CLEAN THIS CODE UP BIG TIME!!!!

    //var open = new Boolean(IsGameOpen(gameID));
    //var gameopen = (IsGameOpen(gameID) == true);
    var gameopen;
    gameopen = IsGameOpen(gameID);


    $("#" + voteButtonID).attr("src", voteImg);
    
    //alert("gameopen = *" + gameopen + "*");
    //alert((gameopen == "true"));

    //alert(IsGameOpen(gameID));
    //alert("ResetControls, game open = " + open);

    //Get status of game -- is it still open
    //if (IsGameOpen(gameID) == true)
    //if (gameopen == "true")
    if (gameopen.toString() == "true")
    {
        //alert('game is open - controls to show = ' + controlsToShowIfGameOpen + ', controls to hide = ' + controlsToHideIfGameOpen);
        ToggleControls(controlsToShowIfGameOpen, controlsToHideIfGameOpen);
    }
    else
    {
        //alert(gameopen);
        var results;

        $.ajax
        ({
            type: "POST",
            contentType: "application/json",
            url: "AJAXServices/GamesService.svc/GetGameResult",
            data: '{ "GameID": "' + gameID + '" }',
            processData: true,
            async: false,
            success: function(msg)
            {
                results = RemoveMSJSONEnvelope(msg)
                //UpdateTallyLabels(msgWithoutEnvelope, radioButtonIDs, tallyLabelIDs);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Error occurred getting game results: " + errorThrown); }
        });
    
    
        //Game is CLOSED. Get the latest results, update controls, then show and hide appropriate controls.
        //alert('game is closed');
        
        
        GetResultsAndUpdateTallyLabels(gameID, radioButtonIDs, tallyLabelIDs);
        ToggleControls(controlsToShowIfGameClosed, controlsToHideIfGameClosed);

        if (messageLabelID != null)
        {
            //Set message label text and class, and make it visible
            if (messageLabelCSSClass != null)
            {
                $("#" + messageLabelID).removeClass();
                $("#" + messageLabelID).addClass(messageLabelCSSClass);
            }

            //$("#" + messageLabelID).text(messageLabelGameClosedText);
            $("#" + messageLabelID).html(messageLabelGameClosedText);
        }


        //Show winner
        var iWinnerIndex = GetIndexOfWinner(results);
        var radiobuttons = $(radioButtonIDs);

        for (var i = 0; i < radiobuttons.length; i++)
        {
            if (radiobuttons[i].value == results.Contestants[iWinnerIndex].Identifier)
            {

                //alert("Winning radio button = " + radiobuttons[i].id);

                //var radiobuttonlabeltext = $('input[name=' + radiobuttons[i].name + '] + label').text();
                var radiobuttonlabeltext = $('#' + radiobuttons[i].id + ' + label').text();

                //alert('radiobuttonlabeltext = ' + radiobuttonlabeltext);

                //alert('winnerDivID = ' + winnerDivID);

                $("#" + winnerLabelID).html(radiobuttonlabeltext);
                $("#" + winnerDivID).css("display", "");
                break;
            }
        }
        
    }
}



function SWRVdownPollShowClosedState(
    radioButtonIDs,
    voteAgainButtonID,
    messageLabelID,
    gameOverMsg,
    gameOverMsgCSSClass,
    winnerDivID,
    winnerLabelID,
    results)
{
    //Hide vote again button
    $("#" + voteAgainButtonID).css("display", "none");

    //Set game over message and apply style
    $("#" + messageLabelID).addClass(gameOverMsgCSSClass).css("display", "");
    $("#" + messageLabelID).html(gameOverMsg);

    //Show winner
    var iWinnerIndex = GetIndexOfWinner(results);
    var radiobuttons = $(radioButtonIDs);

    for (var i = 0; i < radiobuttons.length; i++)
    {
        if (radiobuttons[i].value == results.Contestants[iWinnerIndex].Identifier)
        {

            //alert("Winning radio button = " + radiobuttons[i].id);

            //var radiobuttonlabeltext = $('input[name=' + radiobuttons[i].name + '] + label').text();
            var radiobuttonlabeltext = $('#' + radiobuttons[i].id + ' + label').text();

            //alert('radiobuttonlabeltext = ' + radiobuttonlabeltext);

            //alert('winnerDivID = ' + winnerDivID);

            $("#" + winnerLabelID).html(radiobuttonlabeltext);
            $("#" + winnerDivID).css("display", "");
            break;
        }
    }
}
