﻿

if (typeof Comments == "undefined" || !Comments) 
{
    /**
     * The Comments global namespace object.  If Comments is already defined, the
     * existing Comments object will not be overwritten so that defined
     * namespaces are preserved.
     * @class Comments
     * @static
     */
    var Comments = {}; 
}

Comments.RegisterNamespace = function() 
{ 
    var a = arguments;
    var o = null;
    var i;
    var j;
    var d; 

    for (i=0; i<a.length; i=i+1) 
    {
        d = a[i].split(".");
        o = Comments;

        // Comments is implied, so it is ignored if it is included
        for (j = (d[0] == "Comments") ? 1 : 0; j < d.length; j = j + 1) 
        { 
            o[d[j]]=o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o; 
};

Comments.RegisterNamespace("Comments.Net");

/** 
* Initialize the comments
*/
Comments.Net.Script = function() 
{
    this.commentTextBoxId = null;       // The comment textbox id
    this.normalizedEntityId = null;     // The normalized entity id
    this.commentRequiredText = null;    // The comment required text
    this.commentDeleteUrl = null;       // The delete comment url
    this.commentDeleteText = null;      // The delete comment text
    this.commentCancelText = null;      // The cancel comment text

    var me = this;
    
    /** 
    * Initialize the comments 
    * @param {String} normalizedEntityId    The normalized entity id
    * @param {String} commentRequiredText   The comment required text 
    * @param {String} commentDeleteUrl      The delete comment url 
    * @param {String} commentDeleteText     The comment delete text 
    * @param {String} commentCancelText     The comment cancel text 
    */
    this.Init = function(normalizedEntityId, commentRequiredText, commentDeleteUrl, commentDeleteText, commentCancelText) 
    {
        me.commentTextBoxId = "commentText" + normalizedEntityId;
        me.normalizedEntityId = normalizedEntityId;
        me.commentRequiredText = commentRequiredText;
        me.commentDeleteUrl = commentDeleteUrl;
        me.commentDeleteText = commentDeleteText;
        me.commentCancelText = commentCancelText;
        
        //Autogrow textbox
        $("#" + me.commentTextBoxId).autogrow();

        //Show all comments
        $("#commentsList" + me.normalizedEntityId + " div.commentsList div.viewAllComments a").click(function() 
        {
            $(this).parent("div.viewAllComments").parent(".commentsList").children("div.comment-user-hidden").slideDown("fast");
            $(this).parent("div.viewAllComments").hide();
            
            $("#" + $(this).attr("rel")).attr("value", "true");
            return false;
        });

        //Form validation
        $("#AddCommentForm" + me.normalizedEntityId).validate();

        $("#" + me.commentTextBoxId).rules
        (
            "add",
            {
                required: true,
                messages:
                {
                    required: me.commentRequiredText
                }
            }
        );
        
        //Expand comment
        $("#AddCommentForm" + me.normalizedEntityId + " a.expand-comment-box").click
        (
            function() 
            {
                $("#AddCommentForm" + me.normalizedEntityId + " div.comment-area").fadeIn("slow");
                $(this).fadeOut("slow");
            }
        );
        
        //Hide comment area
        $("#AddCommentForm" + me.normalizedEntityId + " div.comment-area").hide();

        //Delete failed dialog
        $("#commentDeleteDialog" + me.normalizedEntityId).dialog
        (
            {
                resizable: false,
                height: 150,
                modal: true,
                autoOpen: false,
                open: function() 
                {
                    $('.ui-dialog-buttonpane').find('button:contains("Delete")').html('<span style="float:left;" class="ui-icon ui-icon-trash"></span>' + me.commentDeleteText);
                    $('.ui-dialog-buttonpane').find('button:contains("Cancel")').html(me.commentCancelText);
                    //$('.ui-dialog-buttonpane').find('button:contains("Delete")').prepend('<span style="float:left;" class="ui-icon ui-icon-trash"></span>');
                }
            }
        );

        //Delete dialog
        $("#commentDeleteFailedDialog" + me.normalizedEntityId).dialog
        (
            {
                resizable: false,
                height: 150,
                modal: true,
                autoOpen: false
            }
        );

        //Delete comment scripts
        $("#commentsList" + me.normalizedEntityId + " div.commentsList div.comment-user div.delete-comment a.delete-comment, #commentsList" + me.normalizedEntityId + " div.commentsList div.comment-user-hidden div.delete-comment a.delete-comment").click
        (
            function() 
            {
                var commentId = $(this).attr("rel");
                var commentContainer = $(this).parent("div.delete-comment").parent("div.comment-user, div.comment-user-hidden");
                var dialogContainer = $("#commentDeleteDialog" + me.normalizedEntityId);
                dialogContainer.dialog
                (
                    "option",
                    "buttons",
                    {
                        'Delete': function() 
                        {
                            //Delete the comment
                            $.ajax
                            ({
                                type: 'POST',
                                url: me.commentDeleteUrl,
                                data:
                                {
                                    commentId: commentId
                                },
                                dataType: "json",
                                success: function(result) 
                                {
                                    if (result.Success) 
                                    {
                                        commentContainer.fadeOut("slow", function() {
                                            commentContainer.remove();
                                        });
                                    }
                                    else 
                                    {
                                        //Comment was not deleted
                                        $("#commentDeleteFailedDialog" + me.normalizedEntityId).dialog('open');
                                    }
                                },
                                error: function(request, textStatus, errorThrown) 
                                {
                                    alert(request.status + " - " + textStatus + " - " + errorThrown);
                                }
                            });
                            
                            $(this).dialog('close');
                        },
                        Cancel: function() 
                        {
                            //Do not do anything
                            $(this).dialog('close');
                        }
                    }
                );
                dialogContainer.dialog('open');
            }
        );
    }
}

//Ajax validation
function CommentsFormAjaxValidation(formId) 
{
    //Show loader
    AjaxUpdateProgressShow();
    var valid = $("#" + formId).valid();

    if (!valid) 
    {
        //Hide loader
        AjaxUpdateProgressHide();
    }

    return valid;
}

//Reinitializes scripts for every comments module over the page
function CommentsInitAllScripts(commentRequiredText, commentDeleteUrl, commentDeleteText, commentCancelText) 
{
    $("div.comment-holder").each
    (
        function() 
        { 
            var normalizedEntityId = $(this).children("input.comment-hidden-entity").val();
            new Comments.Net.Script().Init(normalizedEntityId, commentRequiredText, commentDeleteUrl, commentDeleteText, commentCancelText);
        }
    );
}

//Reinitializes scripts for a comment module over the page
function CommentsInitScripts(normalizedEntityId, commentRequiredText, commentDeleteUrl, commentDeleteText, commentCancelText) 
{
    new Comments.Net.Script().Init(normalizedEntityId, commentRequiredText, commentDeleteUrl);
}
