﻿var curPageItemsIdx = 1;
var commentListOrigHeight = 0;

$(document).ready(function() {
    $("#blogPostsNavBtn").addClass("on");
    $("#blogPostMessage").hide();

    // Set up comment link
    // Figure out the id of the current content
    var id = $.jqURL.get("id");
    if (id != null) {
        if (id.indexOf("#") > -1) {
            id = id.split("#")[0];
        }
        $("#postAComment").attr("href", "javascript:commentBlogPost(" + id + ");");
        $("#printLink").show();
        $("#printLink a").attr("href", "BlogPostPrint.aspx?id=" + id);
    }

    // Load blog post comments
    loadComments(id);
});

function showBlogPostMessage(id, messageText, fn) {
    $("#blogPostMessage > p").text(messageText);
    $("#blogPostMessage").hide().fadeIn("fast", function() {
        if (fn != null) { fn(); }
        setTimeout(function() {
            $("#blogPostMessage").fadeOut(1500, function() {
                $("#blogPostMessage > p").html("&nbsp;");
            });
        }, 1500);
    });
}

function commentBlogPost(id) {
    // Make sure the comments list is sufficiently large
    if ($("#blogCommentListOuter").height() < 504) {
        commentListOrigHeight = $("#blogCommentListOuter").height();
        $("#blogCommentList").animate({ height: "504px" });
    }

    // Show comments box
    $("#lightBoxBG").fadeIn("fast", function() {
        $("#blogCommentPostContainer").fadeIn();
    });
}

function loadComments(id, pageNo) {
    if (pageNo == null) {
        pageNo = 1;
    }
    curPageItemsIdx = pageNo;
    $("#commentPositionIndicatorContainer").fadeOut();
    $("#commentPaginationContainer").fadeOut();
    $("#blogCommentList").fadeOut();
    $("#blogCommentListOverlay").fadeIn("fast", function() {
        var url = "/ajaxcontent/BlogCommentList.aspx?id=" + id + "&n=3&p=" + pageNo + " #commentList";
        $("#blogCommentList").load(url, function() {
            // Turn off the bottom border of the last item in the list
            $(".commentRow:last").addClass("lastComment");

            // Load the other parts of the page that were retrieved by the above AJAX call:
            // Position indicator (showing m-n of p)
            // Pagination container (1 2 3 NEXT >)
            $("#commentPositionIndicatorContainer").html("").append($("#blogCommentList #positionIndicator"));
            $("#commentPaginationContainer").html("").append($("#blogCommentList #paginationContainer"));
            $("#blogCommentListOverlay").fadeOut("fast");
            $("#blogCommentList").fadeIn();
            $("#commentPositionIndicatorContainer").fadeIn();
            $("#commentPaginationContainer").fadeIn();
        });
    });
}

function showComments(pageNo) {
    var id = $.jqURL.get("id");
    if (id != null) {
        loadComments(id, pageNo);
    }
}

function showNextCommentPage() {
    var totalNumPages = $("#commentPaginationContainer a").length - 1; // The -1 is to remove the NEXT link from the count
    var nextPage = (curPageItemsIdx % totalNumPages) + 1;
    showComments(nextPage);
}

function submitComment() {
    var commentName = $("#txtCommentName").val();
    if (commentName == "" || commentName == null) {
        alert("Name is required.");
        return;
    }
    var email = $("#txtEmailAddress").val();
    if (email == "" || email == null) {
        alert("E-mail Address is required.");
        return;
    }
    var url = $("#txtURL").val();
    var comment = $("#txtCommentBody").val();
    if (comment == "" || comment == null) {
        alert("Comment is required.");
        return;
    }

    var id = $.jqURL.get("id");
    if (id.indexOf("#") > 1) {
        id = id.split("#")[0];
    }
    if (id != null) {
        while (comment.indexOf("\n\n") != -1) {
            comment = comment.replace("\n\n", "</p><p>");
        }
        $.ajax({
            url: "/PostBlogComment.aspx",
            type: "POST",
            data: {
                id: id,
                username: commentName,
                comment: comment,
                email: email,
                url: url
            },
            dataType: "text",
            cache: false,
            beforeSend: function() {
                // Disable the input controls
                $("#blogCommentPostContainer input").attr("disabled", "disabled");
                $("#blogCommentPostContainer textarea").attr("disabled", "disabled");
            },
            success: function(text) {
                showCommentConfirmation();
            },
            error: function(xhr) {
                alert("Error submitting comment:\n" + xhr.responseText);
            },
            complete: function() {
                $("#blogCommentPostContainer input").removeAttr("disabled");
                $("#blogCommentPostContainer textarea").removeAttr("disabled");
            }
        });
    }
}

function showCommentConfirmation() {
    $("#blogCommentPostContainer").fadeOut();
    $("#blogCommentConfirmationBox").fadeIn(function() {
        setTimeout(function() {
            $("#blogCommentConfirmationBox").fadeOut(function() {
                fadeOutCommentLightbox();
                resetCommentListHeight();
                location.reload(true);
            });
        }, 2000);
    });
}

function cancelComment(cb) {
    $("#blogCommentPostContainer").fadeOut(function() {
        fadeOutCommentLightbox(cb);
        resetCommentListHeight();
    });
}

function resetCommentListHeight() {
    if (commentListOrigHeight > 0) {
        $("#blogCommentList").animate({ height: commentListOrigHeight + "px" });
        commentListOrigHeight = 0;
    }
}

function fadeOutCommentLightbox(cb) {
    $("#lightBoxBG").fadeOut("fast", function() {
        $("#txtCommentName").val("");
        $("#txtCommentBody").val("");
        if (cb != null) { cb(); }
    });
}