﻿/// <reference path="jquery-vsdoc.js" />

// By adding the tripple commented reference tag at the top of this page it will pull in the vsdoc
// file and give us intellisense for this script.

$(document).ready(function() {
    SetSubscriptionElements();

    // Set up functionality for when up or down vote arrow is clicked
    $(".vote > div").click(function(event) {
        //var qtID = $(this).parent.attr("id");
        //alert('clicked');
        //debugger;
        var qtID = this.parentNode.id;
        var up = $(this).hasClass("vote-up");
        var upDiv = $('#' + qtID + ' > .vote-up');
        var dnDiv = $('#' + qtID + ' > .vote-down');
        var voteSpan = $('#' + qtID + ' > .vote-count-post');

        // Get the current states so that we can restore if the update fails
        var prevUp = upDiv.hasClass("sprite-icon_vote_up_on");
        var prevDn = dnDiv.hasClass("sprite-icon_vote_down_on");
        var prevVotes = parseInt(voteSpan.html());

        if (up) {
            var val = ToggleUp(upDiv, dnDiv);
        } else {
            var val = ToggleDown(upDiv, dnDiv);
        }

        voteSpan.html(val + prevVotes);

        $.post("/Ajax/VoteTopicReply",
        { id: qtID, up: up },
        function(data) {
            // jr.Data = new { @success=true, @message = String.Empty };

            // Someone else may have voted in the interim so we'd get the total
            // votes from the server and set them.
            //voteSpan.html(data.votes);
            // If success is returned then up/down vote buttons don't change but
            // if false is returned then we need to reset the buttons as they
            // originally were. (The number of votes has already been correctly
            // set by the line above.)
            if (!data.success) {
                upDiv.removeClass("sprite-icon_vote_up_off").removeClass("sprite-icon_vote_up_on");
                dnDiv.removeClass("sprite-icon_vote_down_off").removeClass("sprite-icon_vote_down_on");
                // Reset the up arrow to how it was
                if (prevUp) {
                    upDiv.addClass("sprite-icon_vote_up_on");
                } else {
                    upDiv.addClass("sprite-icon_vote_up_off");
                }
                // Reset the down arrow to how it was
                if (prevDn) {
                    dnDiv.addClass("sprite-icon_vote_down_on");
                } else {
                    dnDiv.addClass("sprite-icon_vote_down_off");
                }
                // Reset the votes to the previous value
                voteSpan.html(prevVotes);
                // Alert user why this failed
                alert(data.message);
            } else {
                // Show alert is message is not empty
                if (data.message.length > 0) {
                    alert(data.message);
                }
            }
        }, "json");
    });

});    // end ready

function ToggleUp(upDiv, downDiv) {
    var changeVal = 0;
    if (upDiv.hasClass("sprite-icon_vote_up_off")) {
        upDiv.removeClass("sprite-icon_vote_up_off").addClass("sprite-icon_vote_up_on");
        changeVal++;
        if (downDiv.hasClass("sprite-icon_vote_down_on")) {
            downDiv.removeClass("sprite-icon_vote_down_on").addClass("sprite-icon_vote_down_off");
            changeVal++;
        }
    } else {
        upDiv.removeClass("sprite-icon_vote_up_on").addClass("sprite-icon_vote_up_off");
        changeVal--;
    }
    return changeVal;
}

function ToggleDown(upDiv, downDiv) {
    var changeVal = 0;
    if (downDiv.hasClass("sprite-icon_vote_down_off")) {
        downDiv.removeClass("sprite-icon_vote_down_off").addClass("sprite-icon_vote_down_on");
        changeVal--;
        if (upDiv.hasClass("sprite-icon_vote_up_on")) {
            upDiv.removeClass("sprite-icon_vote_up_on").addClass("sprite-icon_vote_up_off");
            changeVal--;
        }
    } else {
        downDiv.removeClass("sprite-icon_vote_down_on").addClass("sprite-icon_vote_down_off");
        changeVal++;
    }
    return changeVal;
}

function Subscribe(level, num) {
    $.post("/Ajax/ForumSubscribe",
        { level: level, num: num },
        function(data) { // data is data returned by server. textStatus is one of "timeout", "error", "notmodified", "success", "parsererror" // NOTE: Apparently, only "success" is returned when you make an Ajax call in this way. Other errors silently fail. See above note about using $.ajax.
            var alertText = 'None';
            switch (level) {
                case 'b': // board level
                    var selector = '.subscription.board';
                    if (data.switchedOn) {
                        $(selector).removeClass('False').addClass('True');
                        $(selector + ' > div').removeClass('sprite-icon_subscribe').addClass('sprite-icon_unsubscribe');
                        // Change all category and forum anchors to False and all images to subscribe and then hide them.
                        $('.subscription.category').removeClass('True').addClass('False');
                        $('.subscription.category').hide();
                        $('.subscription.forum').removeClass('True').addClass('False');
                        $('.subscription.forum').hide();
                        alert('You are subscribed to all posts on this board');
                    } else {
                        $(selector).removeClass('True').addClass('False');
                        // Show the topic images
                        $('.subscription.category').show();
                        $('.subscription.forum').show();
                        alert('You have unsubscribed from all posts on this board');
                    }
                    SetSubscriptionElements();
                    break;
                case 'c': // category
                    var selector = '.subscription.category.' + num;
                    if (data.switchedOn) {
                        $(selector).removeClass('False').addClass('True');
                        $(selector + ' > div').removeClass('sprite-icon_subscribe').addClass('sprite-icon_unsubscribe');
                        // Change all forum anchors to False and all images to subscribe and then hide them.
                        // TODO: Need to do this for each forum under this category and not all forums under the board...
                        $('.subscription.forum.cat' + num).removeClass('True').addClass('False');
                        $('.subscription.forum.cat' + num).hide();
                        alert('You are subscribed to all posts in this category');
                    } else {
                        $(selector).removeClass('True').addClass('False');
                        // Show the forum images
                        // TODO: Need to do this for each forum under this category and not all forums under the board...
                        $('.subscription.forum.cat' + num).show();
                        alert('You have unsubscribed from all posts in this category');
                    }
                    SetSubscriptionElements();
                    break;
                case 'f': // forum
                    var selector = '.subscription.forum.' + num;
                    if (data.switchedOn) {
                        $(selector).removeClass('False').addClass('True');
                        $(selector + ' > div').removeClass('sprite-icon_subscribe').addClass('sprite-icon_unsubscribe');
                        // Change all topic anchors to False and all images to subscribe and then hide them.
                        $('.subscription.topic').removeClass('True').addClass('False');
                        $('.subscription.topic').hide();
                        alert('You are subscribed to all posts in this forum');
                    } else {
                        $(selector).removeClass('True').addClass('False');
                        // Show the topic images
                        $('.subscription.topic').show();
                        alert('You have unsubscribed from all posts in this forum');
                    }
                    SetSubscriptionElements();
                    break;
                case 't': // topic
                    var selector = '.subscription.topic.' + num;
                    if (data.switchedOn) {
                        alert('You are subscribed to all replies made to this topic (' + num + ')');
                        $(selector).removeClass('False').addClass('True');
                    } else {
                        alert('You have unsubscribed to all replies made to this topic (' + num + ')');
                        $(selector).removeClass('True').addClass('False');
                    }
                    SetSubscriptionElements();
                    break;
                default:
                    alert('Default hit in Subscribe() case');
            } // end of switch(level)

//            $(imgID).attr("src", data.image);
//            $(imgID).attr('alt', action);
//            $(imgID).attr('title', action);
        }, "json");
}

function SetSubscriptionElements() {
    // set all icons depending on the True/False flag
    $('.subscription.True > div').removeClass('sprite-icon_subscribe').addClass('sprite-icon_unsubscribe');
    $('.subscription.False > div').removeClass('sprite-icon_unsubscribe').addClass('sprite-icon_subscribe');
    // set board text on span tags - will only happen on Board view
    $('.subscription.board.True > span').html('Unsubscribe from this board');
    $('.subscription.board.False > span').html('Subscribe to this board');
    // set forum text on span tags - will only happen on Forum view
    $('.subscription.forum.True > span').html('Unsubscribe from this forum');
    $('.subscription.forum.False > span').html('Subscribe to this forum');
    // set topic text on span tags - will only happen on Topic view
    $('.subscription.topic.True > span').html('Unsubscribe from this topic');
    $('.subscription.topic.False > span').html('Subscribe to this topic');
    // hide topic subscribe icons on forum view if forum subscribed
    if ($('.subscription.forum.True').size() > 0) {
        $('.subscription.topic').hide();
    }
    // TODO: Still need to hide the category and forum icons if the board is subscribed when first hitting the board.
    // TODO: Need to add tooltips to subscribe buttons
}


// Allows for the locking of the topic when the topic is being displayed. Compare to ForumLockTopic() which locks the
// topic when the list of topics is being displayed at the forum (/Board/Forum/) level.
function TopicLockTopic(topicID) {
    $.post("/Ajax/LockTopic",
    { topicID: topicID },
    function(data) {
        // Change lock icon below:
        if (data.locked) {
            $('#lock' + topicID).removeClass("sprite-icon_folder_locked");
            $('#lock' + topicID).addClass("sprite-icon_folder_unlocked");
            $('#lock' + topicID).attr('Title', 'Unlock Topic');
        } else {
            $('#lock' + topicID).removeClass("sprite-icon_folder_unlocked");
            $('#lock' + topicID).addClass("sprite-icon_folder_locked");
            $('#lock' + topicID).attr('Title', 'Lock Topic');
        }
    }, "json");
}

// Used on the /Board/Forum page to lock a topic
function ForumLockTopic(topicID) {
    $.post("/Ajax/LockTopic",
    { topicID: topicID },
    function(data) {
        // Change lock icon below:
        if (data.locked) {
            $('#lock' + topicID).removeClass("sprite-icon_lock");
            $('#lock' + topicID).addClass("sprite-icon_unlock");
            $('#lock' + topicID).attr('Title', 'Unlock topic');
        } else {
            $('#lock' + topicID).removeClass("sprite-icon_unlock");
            $('#lock' + topicID).addClass("sprite-icon_lock");
            $('#lock' + topicID).attr('Title', 'Lock topic');
        }
    }, "json");
}

// Used on the /Board/Forum page to delete a topic
// Not used on the /Board/Topic page - yet
function DeleteTopic(topicID) {
    if (confirm("Are you sure that you want to delete topic " + topicID + "?")) {
        $.post("/Ajax/DeleteTopic",
        { topicID: topicID },
        function(data) {
            if (data.deleted) {
                $('#TopicRow' + topicID).hide();
            }
        }, "json");
    }
}

function DeleteVisibleTopic(topicID) {
    if (confirm("Are you sure that you want to delete topic " + topicID + "?")) {
        $.post("/Ajax/DeleteTopic",
        { topicID: topicID },
        function(data) {
            if (data.deleted) {
                $('.forumRow').hide();
                $('.topicTable > .forum-font-header:first').after('<h2>Topic has been deleted</h2>');
            }
        }, "json");
    }
}

function DeleteReply(replyID) {
    if (confirm("Are you sure that you want to delete reply " + replyID + "?")) {
        $.post("/Ajax/DeleteReply",
        { replyID: replyID },
        function(data) {
            if (data.deleted) {
                var replySelector = '#Reply' + replyID;
                //$(replySelector).children().hide('slow');
                //$(replySelector).html('<h2>Reply has been deleted</h2>');
                $(replySelector).children().hide('slow', function() {
                    $(replySelector).html('<h2>Reply has been deleted</h2>');
                });
            }
        }, "json");
    }
}

function LockUser(memberID, memberName) {
    if (confirm("Are you sure that you want to lock " + memberName + "'s account? If you lock this member's account they will banned from posting to the forum and will not be able to receive emails from other users.")) {
        $.post("/Ajax/LockUser",
        { memberID: memberID },
        function(data) {
            if (data.locked) {
                $('.User' + memberID).html('Member Locked');
                alert('Member ' + memberName + ' is now locked and will be unable to create new topics or reply to existing topics. It will also not be possible to email this user.');
            } //  if data.locked
        }, "json");   // function(data)
    } // if confirm
} // function LockUser

function TopicSwitchToNarrow() {
    $.cookie('widthFormat', 'wide', 300);
    return true;
}

function TopicSwitchToWide() {
    $.cookie('widthFormat', 'narrow', 300);
    return true;
}
