﻿$(document).ready(function () {


    $("div#lp_dialogs_comments_comment_cms").dialog({
        title: 'Comment CMS',
        autoOpen: false,
        modal: true,
        width: 300,
        resizable: false,
        draggable: false,
        buttons: {
            "Delete": function () {

                var comment_id = $("div#lp_dialogs_comments_comment_cms").data("comment_id");

                wsLinkedPapers.Comment_Delete(
                    comment_id,
                    function (result) {
                        $("div#lp_dialogs_comments_comment_cms").dialog("close");

                        $("#comment_" + comment_id).fadeTo("slow", 0.01, function () {
                            $(this).slideUp(function () {
                                $(this).remove();
                            });
                        });
                    }
                    ,
                    function (result) {
                        switch (result.get_exceptionType()) {
                            default:
                                alert(result.get_exceptionType() + '\n' + result.get_message() + '\n' + result.get_stackTrace());
                                break;
                        }
                    }
                );

            }
            ,
            "Reset": function () {

                var comment_id = $("div#lp_dialogs_comments_comment_cms").data("comment_id");

                wsLinkedPapers.Comment_Flag(
                    comment_id,
                    0,
                    function (result) {
                        $comment = $("#comment_" + comment_id);
                        $comment.children(".status:first").text("0");
                        $("div#lp_dialogs_comments_comment_cms").dialog("close");
                    }
                    ,
                    function (result) {
                        switch (result.get_exceptionType()) {
                            default:
                                alert(result.get_exceptionType() + '\n' + result.get_message() + '\n' + result.get_stackTrace());
                                break;
                        }
                    }
                );

            }
            ,
            "Reject": function () {

                var comment_id = $("div#lp_dialogs_comments_comment_cms").data("comment_id");

                wsLinkedPapers.Comment_Flag(
                    comment_id,
                    -1,
                    function (result) {
                        $comment = $("#comment_" + comment_id);
                        $comment.children(".text:first").text(lp_discussion_comment_rejected_text);
                        $comment.children(".text:first").addClass("rejected");

                        $comment.children(".status:first").text("-1");
                        $("div#lp_dialogs_comments_comment_cms").dialog("close");
                    }
                    ,
                    function (result) {
                        switch (result.get_exceptionType()) {
                            default:
                                alert(result.get_exceptionType() + '\n' + result.get_message() + '\n' + result.get_stackTrace());
                                break;
                        }
                    }
                );

            }
            ,
            "Approve": function () {

                var comment_id = $("div#lp_dialogs_comments_comment_cms").data("comment_id");

                wsLinkedPapers.Comment_Flag(
                    comment_id,
                    1,
                    function (result) {
                        $comment = $("#comment_" + comment_id);
                        $comment.children(".status:first").text("1");
                        $("div#lp_dialogs_comments_comment_cms").dialog("close");
                    }
                    ,
                    function (result) {
                        switch (result.get_exceptionType()) {
                            default:
                                alert(result.get_exceptionType() + '\n' + result.get_message() + '\n' + result.get_stackTrace());
                                break;
                        }
                    }
                );

            }
        }
    });

    /*


    $('#lp_dialogs_note_textarea').keyup(function (event) {

    if ($("div#lp_dialogs_note").data('note') == $("#lp_dialogs_note_textarea").attr('value')) {
    $('[aria-labelledby$=lp_dialogs_note]').find(":button:contains('Save')").addClass('ui-state-disabled').attr("disabled", true);
    }
    else {
    $('[aria-labelledby$=lp_dialogs_note]').find(":button:contains('Save')").removeClass('ui-state-disabled').removeAttr("disabled");
    }
    });

    $("#lp_dialogs_note_textarea").focus(function () {
    if ($("#lp_dialogs_note_textarea").attr('value') == dialog_note_initial_text) {
    $("#lp_dialogs_note_textarea").attr('value', '').removeClass('initial');
    }
    });

    */

});

function lp_comment_cms($comment) {

    comment_id = $comment.children(".comment_id:first").text();

    $("div#lp_dialogs_comments_comment_cms").data("comment_id", comment_id);

    switch ($comment.children(".status:first").text()) {
        case '0':
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Reject')").show();
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Approve')").show();
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Reset')").hide();
            break;
        case '1':
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Reject')").show();
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Approve')").hide();
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Reset')").show();
            break;
        case '-1':
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Reject')").hide();
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Approve')").show();
            $('[aria-labelledby$=lp_dialogs_comments_comment_cms]').find(":button:contains('Reset')").show();
            break;
    }

    $("div#lp_dialogs_comments_comment_cms").dialog("open");
}


function lp_citation_comment_add($citation) {

    $discussion = $citation.children(".footer:first").children(".discussion:first");
    $comments = $discussion.children(".comments:first");
    $add = $discussion.children(".add:first");
    $saving = $discussion.children(".saving:first");
    $ok = $add.children(".ok:first");
    $text = $add.children(".text:first");

    var pmid = $citation.children(".pmid:first").text();
    var text = $text.val();

    $text.attr("disabled", true).addClass("ui-state-disabled");
    $ok.attr("disabled", true).addClass("ui-state-disabled");

    //$saving.show();

    wsLinkedPapers.Citation_Comment_Add(
        pmid,
        text,
        function (result) {

            $(".linkedpaper_" + pmid).removeClass('is_not_in_bookshelf').addClass('is_in_bookshelf');

            $comment = $comments.append("<div class='comment new' id='comment_" + result.comment_id + "'></div>").children(":last");
            $comment.append("<div class='comment_id'>" + result.comment_id + "</div>");
            $comment.append("<div class='status'>" + result.status + "</div>");
            $comment.append("<a class='cms' href='Javascript: void(0);' onclick='lp_comment_cms($(this).parent());'>cms</a>");
            $comment.append("<div class='text'>" + result.text.replace('\n', '<br />') + "</div>");

            $created = $comment.append("<div class='created'></div>").children(":last");

            $created.append(" <span class='created_on'>" + result.created_on_user_friendly + "</span>");
            $created.append(", ");
            $created.append("<span class='created_by_username'>" + result.created_by_username + "</span>");

            height = $comment.height();

            $comment.css('height', '0px');

            $comment.animate({ height: height }, 500, function () {
                $comment.fadeTo("slow", 1, function () {
                    $comment.removeClass("new");
                    if ($.browser.msie) this.style.removeAttribute('filter');
                });
            });

            // reset the add form
            $text.val("");
            $text.attr("disabled", false).removeClass("ui-state-disabled");
            $ok.attr("disabled", false).removeClass("ui-state-disabled");

            // hide the fipo message. even if it is possibly already hidden
            $add.children(".fipo:first").hide();

            $citation.addClass("has_comments");
            lp_bookshelf_recalculate_label_citations();
        },
        function (result) {
            switch (result.get_exceptionType()) {
                case "System.Exception":
                    switch (result.get_message()) {
                    }
                    break;
                default:
                    alert(result.get_exceptionType() + '\n' + result.get_message() + '\n' + result.get_stackTrace());
                    break;
            }
        }
    );
}

function lp_citation_comments_load($citation) {

    $discussion = $citation.children(".footer").children(".discussion:first");
    $loading = $discussion.children(".loading:first");
    $comments = $discussion.children(".comments:first");
    $login = $discussion.children(".login:first");
    $add = $discussion.children(".add:first");

    if ($discussion.height() == 0)
    { // load the comments

        $login = $discussion.children(".login:first");
        $fipo = $add.children(".fipo:first");
        $text = $add.children(".text:first");
        $ok = $add.children(".ok:first");

        // get the pmid of this citation
        var pmid = $citation.children(".pmid:first").text();

        // show the loading div
        $loading.show();

        $saving = $discussion.children(".saving:first");

        $discussion.slideDown("fast", function () {

            wsLinkedPapers.Citation_Comment_List(
            pmid,
            function (result) {

                $loading.hide();

                if (result.length == 0) {
                    $add.children(".fipo:first").show();
                }
                else {
                    $comments.show();

                    for (var t = 0; t < result.length; t++) {
                        $comment = $comments.append("<div class='comment' id='comment_" + result[t].comment_id + "'></div>").children(":last");
                        $comment.append("<div class='comment_id'>" + result[t].comment_id + "</div>");
                        $comment.append("<div class='status'>" + result[t].status + "</div>");
                        $comment.append("<a class='cms' href='Javascript: void(0);' onclick='lp_comment_cms($(this).parent());'>cms</a>");

                        if (result[t].status == -1) {
                            $comment.append("<div class='text rejected'>" + lp_discussion_comment_rejected_text + "</div>");
                        }
                        else {
                            $comment.append("<div class='text'>" + result[t].text + "</div>");
                        }

                        $created = $comment.append("<div class='created'> </div>").children(":last");

                        $created.append("<span class='created_by_username'>" + result[t].created_by_username + "</span>");
                        $created.append(", ");
                        $created.append("<span class='created_on'>" + result[t].created_on_user_friendly + "</span>");

                        $comment.append("<div class='rejected_text'>rejected</div>");
                    }
                }

                if ($("body").hasClass('logged_in')) {
                    $add.show();

                    $add.children(".created").children(".created_by_username:first").text($('#ctl00_hlLoggedUsername').text());
                    $add.children(".created").children(".created_on:first").text("as we speak");

                    // disable the ok button
                    $ok.attr("disabled", true).addClass("ui-state-disabled");

                    // make sure the ok button gets enabled when there's something in the textarea
                    $text.keyup(function () {

                        // we must redeclare $ok. it's always the last defined one otherwise
                        $ok = $(this).parent().children(".ok:first");

                        if ($(this).val() == "") {
                            $ok.attr("disabled", true).addClass("ui-state-disabled");
                        }
                        else {
                            $ok.attr("disabled", false).removeClass("ui-state-disabled");
                        }
                    });
                }
                else
                {
                    $login.show();
                }


            },
            function (result) {
                alert(result.get_exceptionType() + '\n' + result.get_message() + '\n' + result.get_stackTrace());
            }
        );

        });

    }
    else
    { // hide the comments
        $discussion.slideUp(function () {
            $add.hide();
            $comments.children().remove();
        });
    }


}

var lp_discussion_comment_rejected_text = "Comment removed due to user agreement violation";

