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

}

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

maxId = 0;
enableGridAnimateAdd = false;

WishListWillGift.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;
        this.initilizeEventHandlers();
    },
    initilizeEventHandlers : function() {

        $(".dsGiveGiftButton").unbind('click');
        $(".dsGiveGiftButton").bind('click', { classThis: this }, function(event) {
            var classThis = event.data.classThis;

            var id = $(this).attr("id");
            var items = id.split("_",2);
            var wishListId = items[1];

            classThis.wishListItemId = wishListId;
            classThis.showAddDialog(wishListId);
        });
    },

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

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

        var classThis = this;
        classThis._handleAdd();

        if (false) {
        $('<div id="dsAddDialog">' 
          + '<div>Want to give this gift?  Great!'
          + ' <br/>'
          + ' DearSanta.com&reg; allows other people who this wish list '
          + " see that you've planned on giving this gift.  Don't worry "
          + " though, the person you're giving it to won't be able to "
          + " see any of the secret gift giving information."
          + ' <br/>'
          + " Also, remember it's still up to you to buy and wrap the " 
          + ' gift! '
          + ' <br/>'
          + ' If you want to indicate you want to give this gift, click '
          + ' the <b>Give</b> button below.  Otherwise, click <b>Cancel</b>'
          + '</div>')
           .dialog({
            title: '<div class="ds-gift-icon"> Give a gift.</div>',
            autoOpen: true,
            modal: true,
            resizable: false,
            width:    450,
            height:   330,
            buttons: {
                "Give": function() {
                        $( this ).dialog( "close" );
                        classThis._handleAdd();
                }, 
                "Cancel": function() {
                        $( this ).dialog( "close" );
                }
            }
        });
        }
    },
    /**
     *  Handle adding a gift
     */
    _handleAdd: function() {
      var classThis = this;

      $.ajax({
             url: this.baseUrl + "wishList/addWillGiftJson",
             context: this,
             async: false,
             dataType: "json",
             data: {
                wish_list_item_id: this.wishListItemId
             },
             success: function(data,textStatus,request) {
                if ( isAjaxError(data, textStatus, request) ) {
                    dsLog("Ajax response fail");
                    showAjaxError("Sorry, could not add.  Please try again later",
                        {data: data, textStatus: textStatus, request: request});
                }
                else {
                    dsLog("Ajax response success");
                    //location.reload(true);
                    $("#wishListItem_" + this.wishListItemId).replaceWith(data.html);
                    this.wishListController.initializeShowPage();
                }
             },
             error:function(req, textStatus, errorThrown) {
                dsLog("Ajax response fail");
                showAjaxError("Sorry, could not addcomment. Please try again later",
                    { request: req, textStatus: textStatus, errorThrown: errorThrown });
             }
      });
        
    }
}


