﻿function makeCommentHtmlEditor() {

    $('#txtCmt').tinymce({
        // Location of TinyMCE script
        script_url: '/Scripts/tiny_mce/tiny_mce.js',

        // General options
        theme: "simple",
        plugins: "paste,directionality,noneditable,visualchars",

        // Example content CSS (should be your site CSS)
        content_css: "css/content.css",

        // Drop lists for link/image/media/template dialogs
        template_external_list_url: "lists/template_list.js",
        external_link_list_url: "lists/link_list.js",
        external_image_list_url: "lists/image_list.js",
        media_external_list_url: "lists/media_list.js",

        // Replace values for the template plugin
        template_replace_values: {
            username: "Some User",
            staffid: "991234"
        }
    });

}


function showChangePublish(commentID) {
    $("#cpCommentID").val(commentID);
    $("#divChangePublish").dialog({ height: 185 }, { width: 420 }, { modal: true }, { title: "Update comment status" });
}

function showAddComment(parentCommentID,showTitle) {
    if (typeof tinyMCE != "undefined") {
        tinyMCE.execCommand('mceRemoveControl', false, 'txtCmt');
    }

    if (showTitle) 
        $('#divCommentTitle').show();
    else
        $('#divCommentTitle').hide();

    // Reset the fields
    $("#txtCmt").val();
    $("#divCommentImg").html("");
    $("#imgIDfromUpload").val("");
    

    if (parentCommentID > 0)
        $("#ParentCommentID").val(parentCommentID);
    else {
        $("#ParentCommentID").val("null");
    }
    // $("#divNewComment").dialog({ modal: true }, { title: "Enter a comment" }, { resize: "auto" }, { width: "auto" });
    $("#divNewComment").center();
    $("#divNewComment").show('slow');

    makeCommentHtmlEditor();
}

function addCommentComplete(context) {

    hideLoading();
    // Check if the comment saved okay, if not show message.
    var json = context.get_response().get_object();
    if (json.Success) {
        
        //$("#divNewComment").dialog('close');
        $("#divNewComment").hide('slow');

        var newCommentID = json.ItemID;
        var parentCommentID = json.ItemID2;
        $("#cmtTls" + parentCommentID).hide('slow');
        if (parentCommentID != null) {
            $("#cmt" + parentCommentID).show('slow');
            $("#cmt" + parentCommentID).append("<br /><hr /><span class='newCmt'>Your reply:</span><br /><br />" + $("#txtCmt").val() + "<br /><br />" + $("#divCommentImg").html());
            $("#txtCmt").val("");
        }
        else {
            $("#divTopCmt").prepend("<hr /><span class='newCmt'>Your comment:</span><br />" + $("#txtCmt").val() + "<br /><br />" + $("#divCommentImg").html());
            $("#divTopCmt").show("slow");
            $("#txtCmt").val("")
        }
    }
    else {

    }
}

function showReportAbuse(commentID) {
    $("#abuseCmtID").val(commentID);
    $("#divReportAbuse").dialog({ height: 270 }, { width: 420 }, { modal: true }, { title: "Report abuse or spam" });
}

function showDeleteComment(commentID) {
    $("#delCommentID").val(commentID);
    $("#divDeleteCmt").dialog({ height: 140 }, { width: 420 }, { modal: true }, { title: "Delete a comment" });
}

function addCommentBegin() {
    showLoading();
}

function deleteCmtBegin() {
    showLoading();
}

function changePublishBegin() {
    showLoading();
}

function reportAbuseBegin() {
    showLoading();
}




function reportAbuseComplete() {
    hideLoading();
    $("#divReportAbuse").dialog('close');
    alert('Your notification has been emailed to an administrator. The administrator will check this comment and rectify as soon as possible if it is deemed to be abusive.');
}

function deleteCmtComplete(context) {
    hideLoading();
    var json = context.get_response().get_object();
    if (json.Success) {
        location.reload(true);

    }
}

function changePublishComplete(context) {
    hideLoading();
    var json = context.get_response().get_object();
    if (json.Success) {
        $("#divChangePublish").dialog('close');
        if (json.ItemID2 == 1) //Published
        {
            // Hide the not published message.
            $("#divAdminNotPublished" + json.ItemID).hide('slow');
        }
        else {
            // Show the not published message.
            $("#divAdminNotPublished" + json.ItemID).show('slow');
        }
    }
}



function cancelComment() {
    //$("#divNewComment").dialog('close');
    $("#divNewComment").hide('slow');
}

function cancelReportAbuse() {
    $("#divReportAbuse").dialog('close');
}

function cancelDelete() {
    $("#divDeleteCmt").dialog('close');
}

   
