﻿/// <reference path="~/Content/js/lib/jquery/jquery.min.js" />

var numCommentsRendered = 0;

$(document).ready(function() {
    
    if (numComments > 0) {
        renderComments(safeJsonParse(firstPageComments));
    }
    else {
        updateCommentListInfo();
    }

});

function showMoreComments() {
    addComments(numCommentsRendered, itemsPerPage, ".comment:last");    
}

function showAllComments() {
    var numUnseenComments = numComments - numCommentsRendered;
    addComments(numCommentsRendered, numUnseenComments, "");
}

function safeJsonParse(jsonStr)
{
    var parsedObj;
    var safeJsonStr = jsonStr.replace(/</g, "&lt;");
    safeJsonStr = safeJsonStr.replace(/>/g, "&gt;");    
    
    try
    {
        parsedObj = JSON.parse(safeJsonStr);
    }
    catch (Error)
    {
        parsedObj = null;
    }
    return parsedObj;
}

function addComments(startIndex, numComments, scrollTarget) 
{
    showLoadingMsg(true);
    $.post(siteRoot + "/" + communityId + "/Ajax/GetComments.aspx",
        { archiveId: presentation_id, startIndex: startIndex, numComments: numComments },
        function(data) {
            comments = safeJsonParse(data);
            if (comments != null)
                renderComments(comments);
            showLoadingMsg(false);
            if (scrollTarget != "")
                $.scrollTo($(scrollTarget));
        });    
}

function renderComments(comments) {

    for (var i = 0; i < comments.length; i++)
        renderComment(comments[i]);    

    numCommentsRendered += comments.length;
    updateCommentListInfo();
}

function renderComment(commentObj) 
{
    var newComment = $("#CommentTemplate").clone();
    newComment.appendTo("#CommentsList");

    var usernameSpan = newComment.find(".comment-username-text");

    usernameSpan.html(commentObj.displayName);
    usernameSpan.attr("title", commentObj.fullDisplayName);
    newComment.find(".comment-timeago-text").html(commentObj.timeAgoMade);
    newComment.find(".comment-body-text").html(commentObj.text);
    newComment.find("input[name='commentId']").val(commentObj.id);

    if (commentObj.timeSec != 0)
        newComment.find(".linked-time-function").click(function() {
            goToLinkedTime(commentObj.timeSec);            
        });
    else
        newComment.find(".linked-time-function").hide();
    
    if (isAdmin)
        newComment.find(".delete-function").click(function() {
            deleteComment(commentObj.id);
        });
    else
        newComment.find(".delete-function").hide();
    
    newComment.show();
   
}

function updateCommentListInfo()
{
    if (numComments > 0) {
        $("#NoCommentsMsg").hide();
        $("#CommentsShowing").show();
    } else {
        $("#NoCommentsMsg").show();
        $("#CommentsShowing").hide();
    }
        
    $("#CommentsShowing").html(StringFormat("Showing {0} of {1} comments",
        numCommentsRendered, numComments));
    
    if (numCommentsRendered >= numComments)
        hideMoreCommentsBtns();
       
}

function showLoadingMsg(isLoading) 
{
    if (isLoading) {
        $("#CommentsLoadingMsg").show();
    } else {
        $("#CommentsLoadingMsg").hide();
    }
}

function hideMoreCommentsBtns()
{
    $("#AllCommentsBtn").hide();
    $("#MoreCommentsBtn").hide();
}   

function postComment(formData) {
        
    if (formData.commentText.value == "")
        return;

    var timeSec;
    if (formData.linkToTime.checked) {        
        timeSec = getCurrentPlayPos();      
    }
    else {
        timeSec = 0;
    }

    var commentPostObj = { 'comment.text': formData.commentText.value,
        'comment.archive.id': presentation_id,
        'comment.user.id': formData.userId.value,
        'comment.timesec': timeSec
    };

    $("#NoCommentsMsg").hide();    

    $.post(siteRoot + "/" + communityId + "/Ajax/PostComment.aspx", commentPostObj,
        function(data) {
            numComments += 1;
            var numShowingBeforePost = numCommentsRendered;            
            $("#CommentsList").html("");
            numCommentsRendered = 0;
            addComments(0, numShowingBeforePost + 1, ".comment:first");
            $("#PostCommentTextBox").val('');
            $("#LinkTimeCheckbox").attr('checked', false);            
        }
    );        
    
}

function getCurrentPlayPos()
{
    var playPos = 0;
    try {
        playPos = getMyApp("presentationUISwf").getPosition();
    }
    catch (error) { }
    return playPos;    
}

function setCurrentPlayPos(newTime)
{
    try {
        getMyApp("presentationUISwf").setPosition(newTime);
    }
    catch (error) { }
}

function getPlayPosLink(time, innerHtml) 
{
    return StringFormat("<a href=\"#\" onclick=\"setCurrentPlayPos({0})\">{1}</a>", 
        time, innerHtml);
}

function goToLinkedTime(timeSec) 
{    
    setCurrentPlayPos(timeSec);
    $.scrollTo(0);
}

function deleteComment(commentId)
{    

    var targetComment = $(StringFormat(".comment:has(input[name='commentId'][value='{0}'])", commentId));

    targetComment.toggleClass("comment-selected");
    if (!confirm(deleteConfirmMsg)) {
        targetComment.toggleClass("comment-selected");
        return;
    }

    $.post(siteRoot + "/" + communityId + "/Ajax/DeleteComment.aspx", { commentId: commentId },
        function() { });                
     
    targetComment.hide("normal", function() {
        $(this).remove();
    });

    numComments -= 1;
    numCommentsRendered -= 1;
    
    updateCommentListInfo()    
}



