﻿function insertArticleComment(articleID, parentArticleCommentID, commentTextAreaID, commentSubmitButtonID)
{
    userdo(function() {
        var commentTextArea = $("#" + commentTextAreaID);
        var commentSubmitButton = $("#" + commentSubmitButtonID);
        var comment = commentTextArea.val();
        commentSubmitButton.attr("disabled", true);
        ArticleComments.Insert(
            articleID,
            parentArticleCommentID,
            comment,
            function(result) {
                if (result)
                {
                    window.location.reload( false );
                }
                else
                {
                    alert("Error: Comment was not submitted!");
                }
                commentSubmitButton.attr("disabled", false);
            }
        );
    });
}

function lickComment(commentID, value)
{
    userdo(function() {
        var divID = "c" + commentID;
        var thumbsDiv = $("#" + divID + " div.thumbsDiv");
        var lickCountSpan = $("#" + divID + " span.articleCommentLickCount");
        var oldLickCount = parseInt(lickCountSpan.attr("lickCount"));
        var newLickCount = oldLickCount + value;

        ArticleCommentUserJoins.InsertArticleCommentUserJoin(
            commentID,
            (value > 0 ? "Lick" : "Bury"),
            function(result) {
                thumbsDiv.addClass("voted");
                lickCountSpan.text(
                    ((newLickCount > 0) ? "+ " : "- ")
                    + Math.abs(newLickCount)
                    + " "
                    + ((Math.abs(newLickCount) == 1) ? "lick" : "licks")
                );
            }
        );
    });
}

$(function() {
    var fragmentStartIndex = window.location.toString().lastIndexOf("#");
    if (fragmentStartIndex < 0)
        return;
    var fragment = window.location.toString().substring(fragmentStartIndex + 1);
    var commentObj = $("a." + fragment + "rl");
    if (commentObj == null)
        return;
    commentObj.click();
});