﻿/* ===== comments ====== */

/*
* move the add-comment div to below the selected comment when reply is clicked
* need to reset if the comment id changes
*/
function commentReply(id) {
    callback = function(response) {
        if (response) {
            if ($('#comment-' + id).is(':visible')) {
                $('#reply-link-' + id).html('cancel');
            } else {
                $('#reply-link-' + id).html('reply');
            }

            $('#reply-id').attr('value', id);
            $('#comment-' + id + ' div > textarea').width(0);
            $('#comment-' + id + ' div > textarea').width($('#comment-' + id).width() - 85 + 'px');
            $('#comment-' + id).append($('#add-reply').toggle());
        } else {
            alert('you need to be logged in to post a comment or reply!');
        }
    }

    errorCallback = function(response) {
        alert('error making ajax request');
    }

    Proxy.invoke("IsAuthenticated", {}, callback, errorCallback, false);

}

var fetchedReplies = new Array();

function commentShowReplies(id, r) {

    /* not replies then do nothing */
    if (r == 0) { return };
    /* check if we have already fetched */
    for (i = 0; i < fetchedReplies.length; i++) {
        if (id == fetchedReplies[i]) {
            $('#comment-' + id + ' > ul').toggle();
            return;
         }
    }

    var temp = $("#comment-" + id + " > div.show-replies").html()
    $("#comment-" + id + " > div.show-replies").html("loading replies...");

    callback = function(response) {
        $('#comment-' + id).append(response);
        $("#comment-" + id + " > div.show-replies").html(temp);
        fetchedReplies.push(id);
    }

    errorCallback = function(response) {
        alert(response);
    }

    Proxy.invoke("CommentsByParentID", { 'id': "" + id + "" }, callback, errorCallback, false);
}