﻿/* Blog & Fan Boards */
$Postmanager = {
    initialize: function() {
        $("#taComment").wysiwyg({
            controls: {
                strikeThrough : { visible : true },
                underline     : { visible : true },
              
                separator00 : { visible : true },

                justifyLeft: { visible: false },
                justifyCenter: { visible: false },
                justifyRight: { visible: false },
                justifyFull: { visible: false },

                separator01: { visible: false },

                indent: { visible: true },
                outdent: { visible: true },

                separator02: { visible: true },

                subscript: { visible: false },
                superscript: { visible: false },

                separator03: { visible: false },

                undo: { visible: false },
                redo: { visible: false },

                separator04: { visible: false },

                insertOrderedList: { visible: true },
                insertUnorderedList: { visible: true },
                insertHorizontalRule: { visible: true },

                h1mozilla: { visible: false },
                h2mozilla: { visible: false },
                h3mozilla: { visible: false },
                h4mozilla: { visible: false },
                h5mozilla: { visible: false },
                h6mozilla: { visible: false },

                h1: { visible: false },
                h2: { visible: false },
                h3: { visible: false },
                h4: { visible: false },
                h5: { visible: false },
                h6: { visible: false },

                separator05: { visible: false },
                separator06: { visible: false },
                separator07: { visible: false },
                separator08: { visible: false },
                separator09: { visible: false },
                
                cut: { visible: false },
                copy: { visible: false },
                paste: { visible: false },
                
                createLink: { visible: false },
                insertImage: { visible: false },
                removeFormat: { visible: false },
            }
        });

        $("#btnMoreComments").click(function() {
            if (BlogCommentsInfo != null) {
                $("#imgLoadingComments").show();
                $("#btnMoreComments").hide();
                target = (BlogCommentsInfo.isFanBoards) ? "fan-boards" : "blog";
                $.ajax({
                    url: "/" + target + "/load-more-comments/" + BlogCommentsInfo.blogPostId + "/" + BlogCommentsInfo.pageNumber,
                    type: 'POST',
                    data: { postId: BlogCommentsInfo.blogPostId, pageNumber: BlogCommentsInfo.pageNumber },
                    dataType: 'json',
                    success: function(data) {
                        postOn = false;
                        $.each(data, function() {
                            html = "";
                            if (BlogCommentsInfo.isFanBoards) {
                                html = $Postmanager.buildPostHtml(this, postOn);
                                postOn = (postOn) ? false : true;
                            } else {
                                html = $Postmanager.buildCommentHtml(this);
                            }
                            $("#CommentsContainer_" + BlogCommentsInfo.blogPostId).append(html);
                        });
                        BlogCommentsInfo.pageNumber++;
                        $("#imgLoadingComments").hide();
                        if (BlogCommentsInfo.pageNumber < BlogCommentsInfo.pageCount) {
                            $("#btnMoreComments").show();
                        }
                    }
                });
            }
            return false;
        });

        $("#btnSubmit").click(function() {
            $("#btnSubmit").hide();
            $("#litErrorMessage").hide();
            if (PostInfo != null) {
                targetId = PostInfo.postId;
                safeCommentText = escape($("#taComment").val());
                $.ajax({
                    url: "/Comment/Post/",
                    data: { folioItemID: targetId, folioItemUrl: "", commentText: safeCommentText },
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if (data.Code != null && data.Code != undefined && data.Code != "0") {
                            //display error message
                            $("#litErrorMessage").append(data.Error);
                            $("#litErrorMessage").show();
                        } else {
                            //refresh the current url
                            window.location.href = PostInfo.currentUrl;
                        }
                    }
                });
            }
        });
    },
    buildCommentHtml: function(cObj) {
        retval = "<li itemid=" + cObj.CommentId + "><img src=" + cObj.PosterImageURL + " width='45px' /><div class='comment'>" + cObj.Poster + " - " + cObj.TimeStamp + "<br />" + cObj.Text + "</div><div class='clear'></div></li>";
        return retval;
    },
    buildPostHtml: function(cObj, isOn) {
        onText = (isOn) ? "on" : "";
        retval = "<div class='box orange box-reply' itemid='" + cObj.CommentId + "'><div class='reply-container " + onText + "'><div class='info'>";
        retval += "<span>Reply:</span> Posted by <a href='#'><span>" + cObj.Poster + "</span></a> on " + cObj.TimeStamp + "</div>";
        retval += "<div class='image left'><img src='" + cObj.PosterImageURL + "' /></div><div class='reply-body right'>" + cObj.Text + "</div><div class='clear'></div></div></div>";

        return retval;
    }
}
$(document).ready(function() {
    $Postmanager.initialize();
});
