﻿(function ($) {

    $.fn.wall = function (options) {

        // Options + defaults
        var defaults = {}; // defaults are defined on the server in wall.ascx.vb
        var config = $.extend(defaults, options);
        this.each(function () {

            var self = this;
            var $self = $(this);
            var $commentListPlaceholder = $('div.CommentPlaceholder', self);
            var $showMore = $("a.ShowMore", $self);
            var lastCommentId = config.lastCommentID;
            var totalComments = config.totalComments;
            var commentsDisplayed = config.commentsDisplayed;

            $self.bind("dataBind", dataBind);

            enableBehaviors();

            function enableBehaviors() {
                // Slide down newly entered items
                $self.find(".SlideDown").slideDown()
                // Wire up editing on comments
                $self.find(".Comment").editableComment({
                    containerId: self.id,
                    deleteAction: "deletecomment",
                    updateAction: "updatecomment",
                    itemName: "comment"
                })
                // Wire up dummy comment form 
                $self.find('div.DummyCommentForm').unbind("click focusin").bind("click focusin", function (e) {
                    var $dummyCommentForm = $(this);
                    var $commentInputWrapper = $dummyCommentForm.parents('div.CommentContainer').find('div.CommentInputWrapper');
                    $dummyCommentForm.hide();
                    $commentInputWrapper.show();
                    $commentInputWrapper.find('textarea')
                                .elastic()
                                .focus();
                });
                // Wire up comment submit buttons
                $self.find("a.SubmitComment").unbind("click").bind("click", insertComment);
                // Wire up show more button
                $showMore.unbind("click").bind("click", showMore);
                $self.enableBehavior();
                $r.enableWatermarks();
            }

            function dataBind(e, newId) {
                $.ajax({
                    url: $r.cleanHref(window.location),
                    type: "POST",
                    data: {
                        containerId: self.id,
                        action: "select",
                        isReadOnly: config.isReadOnly
                    },
                    success: function (msg) {
                        //debugger;
                        var result = JSON.parse(msg);
                        lastCommentId = result.LastCommentID;
                        totalComments = result.TotalComments;
                        commentsDisplayed = result.CommentsDisplayed;
                        if (totalComments > commentsDisplayed) $showMore.removeClass("Hidden");
                        $commentListPlaceholder.html(result.HTML);
                        enableBehaviors();
                        $r.hideProgress();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $r.showError(XMLHttpRequest, errorThrown);
                        $r.hideProgress();
                    }
                });
            } // dataBind

            function showMore(e) {
                var $this = $(this);
                var $showMoreSpinner = $(".ShowMoreSpinner", self);
                $showMoreSpinner.show();
                $.ajax({
                    url: $r.cleanHref(window.location),
                    type: "POST",
                    data: {
                        containerId: self.id,
                        action: "showmore",
                        lastCommentId: lastCommentId,
                        totalComments: totalComments,
                        commentsDisplayed: commentsDisplayed,
                        isReadOnly: config.isReadOnly
                    },
                    success: function (msg) {
                        var result = JSON.parse(msg);
                        commentsDisplayed = result.CommentsDisplayed;
                        $commentListPlaceholder.append(result.HTML);
                        enableBehaviors();
                        lastCommentId = result.LastCommentID;
                        if (lastCommentId == 0) $this.addClass("Hidden");
                        $r.hideProgress();
                        $showMoreSpinner.hide();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $r.showError(XMLHttpRequest, errorThrown);
                        $r.hideProgress();
                    }
                });
                e.preventDefault();
            } // showMore

            function insertComment(e) {
                var $el = $(this);
                var $parent = $el.parents("div.Comment");
                var parentCommentID = $parent.attr("keyValue") || 0;
                var $input = $el.parents("div.CommentInputWrapper").find(".CommentInput");
                var comment = $input.val();
                if (comment != $input.attr("title") && comment.length > 0) {
                    $.ajax({
                        url: $r.cleanHref(window.location),
                        type: "POST",
                        data: {
                            containerId: self.id,
                            action: "insertComment",
                            parentCommentID: parentCommentID,
                            comment: comment
                        },
                        success: function (msg) {
                            if (!isNaN(parseInt(msg))) {
                                var newId = msg;
                                $r.clearWaitButton($el[0]);
                                $input.val("");
                                $self.trigger("dataBind", [newId]);
                                $r.hideProgress();
                            } else {
                                $r.updateStatus('warning', msg)
                                $r.hideProgress();
                            };
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $r.showError(XMLHttpRequest, errorThrown);
                            $r.hideProgress();
                        }
                    });
                } else {
                    $self.trigger("dataBind"); // Easy way to clear progress, waitbuttons
                }
                e.preventDefault();
            }

        }); // each

    }; // $.fn.wall 

})(jQuery);


