﻿
var commentEditor = null;
var currentHtmlText = null;
var currentCommentEditor = null;
var gPageSecurityToken = null;
var gRelatedContentID = "";
var gCommentType = "";

// Active Comment variables
var activeGuid = null;
var activeHTML = null;
var originalHTML = null;

function AddComment(curEditor, htmlText, commentText) {

    commentEditor = curEditor;
    currentHtmlText = htmlText;
    FWComments.AddComment(gPageSecurityToken, gRelatedContentID, gCommentType, commentText, onAddCommentCompleted);
}

function onAddCommentCompleted(results) {
    var commentID = results;
    if (commentEditor != null) {
        
        // set our active variables
        activeGuid = commentID;
        originalHTML = currentHtmlText;

        var prefix = '<span class="UserComment" onmouseover="//performShowComment(\'' + commentID + '\');" ' +
                      'onmouseout="//performHideComment();" >';
        var postfix = '</span>';

        var newHtml = prefix + currentHtmlText + postfix;
        activeHTML = newHtml;

        commentEditor.PasteHTML(newHtml);
    }
}

function onContentUpdateCompeted(results) {

}

function getSelectedEditorText(oEditor) {
    // get the active editor document
    var editdoc = oEditor.GetDocument();

    // get the active editor window
    var editwin = oEditor.GetWindow();


    var rng = null, html = "";
    if (document.selection && document.selection.createRange) {
        rng = editdoc.selection.createRange();
        html = rng.htmlText || "";
    } else if (window.getSelection) {
        rng = editwin.getSelection();

        if (rng.rangeCount > 0 && window.XMLSerializer) {
            rng = rng.getRangeAt(0);
            html = new XMLSerializer().serializeToString(rng.cloneContents());
        }
    }

    return html;
}


//
// Primary entry point for adding a new comment.
// this method will create the comment with an empty string, and sets the active comment
// 
// it does a popup.  when saved, it will modify the new one.
// if canceled, it will execute an undo in the html editor and delete the active cms entry.
//
function startAddComment(instanceName, securityToken, relatedContentID, newCommentType, editboxName) {

    // set globals
    gRelatedContentID = relatedContentID;
    gCommentType = newCommentType;
    gPageSecurityToken = securityToken;

    var oEditor = document.getElementById(instanceName);
    if (oEditor != null) {
        var html = getSelectedEditorText(oEditor);
        var commentText = '';

        // empty out any text in the edit box.
        var editBox = document.getElementById(editboxName);
        if (editBox != null) {
            editBox.value = '';
        }
        
        AddComment(oEditor, html, commentText);
    }
}


function performSaveActiveComment(instanceName, editBoxName) {

    commentEditor = null;
    var oEditor = document.getElementById(instanceName);
    if (oEditor != null) {
        commentEditor = oEditor;
    }
        
    var oEditBox = document.getElementById(editBoxName);
    if (oEditBox != null) {
        currentCommentEditor = oEditBox;
        var commentID = activeGuid;
        var newText = oEditBox.value;

        // clear the value from the editbox now that we have retrieved it.
        // so it doesn't hang arround for next time.
        oEditBox.value = "";
        
        FWComments.UpdateComment(gPageSecurityToken, commentID, newText, onSaveComment);
    }
}

function onSaveComment(results) {
    if (commentEditor != null) {
        var fullHTML = commentEditor.getHTML();
        var commentID = activeGuid;
        FWComments.UpdateRelatedContent(gPageSecurityToken, commentID, gRelatedContentID, gCommentType, fullHTML, onContentUpdateCompeted);
    }
}

//
// For an Add operation, we will undo changes to the editor
// and then delete the new cms entry we added.
//
function performCancelAdd(instanceName) {

    ExecUndoCommand(instanceName);

    var commentID = activeGuid;
    FWComments.DeleteComment(gPageSecurityToken, commentID, onCancelAddComment);
}


function onCancelAddComment(results) {

}


function setSecurityToken(newToken) {
    gPageSecurityToken = newToken;
}

function setRelatedContentID(newContentID, newCommentType) {
    gRelatedContentID = newContentID;
    gCommentType = newCommentType;
}

//
// Return the comment ID from the html.
// returns null if no id found.
function findCommentID(html) {
    // now we have the currently selected html, lets find our guid
    var commentID = null;
    var pos = html.indexOf('//performShowComment');
    if (pos != -1) {
        commentID = html.substring(pos + 22, pos + 22 + 36);
    }

    return commentID;
}


function startEditComment(instanceName, editBoxName, securityToken) {

    // set globals
    gRelatedContentID = "";
    gCommentType = "";
    gPageSecurityToken = securityToken;


    var oEditBox = document.getElementById(editBoxName);
    if (oEditBox != null) {
        currentCommentEditor = oEditBox;

        var oEditor = document.getElementById(instanceName);
        if (oEditor != null) {
            var html = getSelectedEditorText(oEditor);
            var commentID = findCommentID(html);
            activeGuid = commentID;
            activeHTML = html;
            originalHTML = null;
            FWComments.GetComment(gPageSecurityToken, commentID, onEditComment);
        }
    }

}

var deleteEditorName = null;
function performDeleteComment(instanceName, securityToken) {

    // set globals
    gRelatedContentID = "";
    gCommentType = "";
    gPageSecurityToken = securityToken;


    var oEditor = document.getElementById(instanceName);
    if (oEditor != null) {
        deleteEditorName = instanceName;
        var html = getSelectedEditorText(oEditor);
        var commentID = findCommentID(html);
        activeGuid = commentID;
        FWComments.DeleteComment(gPageSecurityToken, commentID, onDeleteComment);
    }    
}


function onDeleteComment(results) {
    // once the comment is deleted, remove the formatting
    // from the selected area in the editor.
    ExecRemoveFormatCommand(deleteEditorName);

    var oEditor = document.getElementById(deleteEditorName);
    if (oEditor != null) {
        var fullHTML = oEditor.getHTML();
        var commentID = activeGuid;
        FWComments.UpdateRelatedContent(gPageSecurityToken, commentID, gRelatedContentID, gCommentType, fullHTML, onContentUpdateCompeted);
    }
}

function performShowComment(commentID) {
    FWComments.GetComment(gPageSecurityToken, commentID, onShowComment);
}

function onShowComment(results) {
    var commentText = '<i>Comment has been deleted from server</i>';
    var author = "Unknown";
    if (results != null) {
        commentText = results.CommentText;
        author = results.CreatedBy;
    }

    var outText = '<b>' + author + ':</b><br />' + commentText;
    Tip(outText, BALLOON, true, ABOVE, true);
}

function performHideComment() {
    UnTip();
}

function onEditComment(results) {
    currentCommentEditor.value = results.CommentText;
}

function ExecUndoCommand(instanceName) {
    // get the cute editor instance
    var editor1 = document.getElementById(instanceName);

    var cmd = 'undo';
    var val = '';
    editor1.ExecCommand(cmd, false, val);
}

function ExecRemoveFormatCommand(instanceName) {
    // get the cute editor instance
    var editor1 = document.getElementById(instanceName);

    var cmd = 'removeformat';
    var val = '';
    editor1.ExecCommand(cmd, false, val);
}
