/**
 * Class that does dynamic actions when editing the wish list
 */
WishListComment = function() {

}

WishListComment.staticFunction = function(e) {
   // 
}

maxId = 0;
enableGridAnimateAdd = false;

WishListComment.prototype = {
    wishListController : '',
    baseUrl : '', // location of ${resource:dir('/")}
    wishListItemId : -1,

    /**
     * This method is called after the page load is complete
     * to initialize the dialog, other widgets, and event
     * handlers.
     * @param baseUrl - base URL of web app
     */
    initialize : function(controller, baseUrl) {
        this.wishListController = controller;
        this.baseUrl = baseUrl;

    },

    showAddDialog : function(wishListItemId) {
        this.wishListItemId = wishListItemId;

        $("#dsAddDialog").remove();

        var classThis = this;

        $('<div id="dsAddDialog">' 
          + 'Type your comment below (limit 250 characters).'
          + '<textarea id="dsCommentText" style="width: 360px; height: 100px">'
          + '</textarea>'
          + '<div id="commentValError" style="display:none; margin-top:5px">'
          + '</div>'
        + '</div>')
           .dialog({
            title: '<div class="ds-comment-add-icon"> Add a comment.</div>',
            autoOpen: true,
            modal: true,
            resizable: false,
            width:    400,
            height:   250,
            buttons: {
                "Add": function() {

                        var commentText = $("#dsCommentText").val();

                        if (commentText.trim() == "") {
                           showCommentValidationError("Please type a comment.");
                        }
                        else {
                            if (commentText.length > 250) {
                                commentText = commentText.substr(0,250);
                            }
                            $( this ).dialog( "close" );
                            classThis._handleAdd(commentText);
                        }
                }, 
                "Cancel": function() {
                        $( this ).dialog( "close" );
                }
            }
        });
    },
    /**
     *  Handle adding a comment
     */
    _handleAdd: function(commentText) {
      var classThis = this;
      //mask("Please wait", "Adding comment, please wait...");
      $.ajax({
             url: this.baseUrl + "wishList/addCommentJson",
             async: false,
             context: this,
             dataType: "json",
             data: {
                wish_list_item_id: this.wishListItemId,
                comment_text: commentText
             },
             success: function(data,textStatus,request) {
                if ( isAjaxError(data, textStatus, request) ) {
                    dsLog("Ajax response fail");
                    showAjaxError("Sorry, could not add comment.  Please try again later",
                        {data: data, textStatus: textStatus, request: request});
                }
                else {
                    dsLog("Ajax response success for item " + this.wishListItemId);
                    var spec = "#wishListItem_" + this.wishListItemId;
                    if($(spec).length == 0) {
                        alert("Can't find " + spec);
                    }
                    $(spec).replaceWith(data.html);
                    this.wishListController.initializeShowPage();
                }
             },
             error:function(req, textStatus, errorThrown) {
                dsLog("Ajax response fail");
                showAjaxError("Sorry, could not add comment. Please try again later",
                    { request: req, textStatus: textStatus, errorThrown: errorThrown });
             }
      });
        
    },
    deleteComment : function(commentId) {
       //mask("Please wait", "Deleting comment, please wait...");
       $.ajax({
             url: this.baseUrl + "wishList/deleteCommentJson",
             async: false,
             context: this,
             dataType: "json",
             data: {
                comment_id: commentId
             },
             success: function(data,textStatus,request) {
                if ( isAjaxError(data, textStatus, request) ) {
                    dsLog("Ajax response fail");
                    showAjaxError("Sorry, could not delete comment.  Please try again later",
                        {data: data, textStatus: textStatus, request: request});
                }
                else {
                    dsLog("Add comment - Ajax response success");
                    //location.reload(true);
                    $("#wishListItem_" + data.wishListItemId).replaceWith(data.html);
                    this.wishListController.initializeShowPage();
                }
             },
             error:function(req, textStatus, errorThrown) {
                dsLog("Ajax response fail");
                showAjaxError("Sorry, could not delete comment. Please try again later",
                    { request: req, textStatus: textStatus, errorThrown: errorThrown });
             }
      });
    }
}

showCommentValidationError = function(msg) {
   $("#commentValError").html('<div style="margin-left: 10px; margin-right: 20px;" class="message">' + msg + '</div>');
   $("#commentValError").show();
   $("#commentValError").delay(1000).fadeOut('slow');

}


