define('jForumXhrDefer', ['require','jquery'], function (require, $) {
    'use strict';

    var jForumXhrDefer = (function () {
        var da = {};
        
        //回复加载后的def
        da.gatherMood = function(xhr, template){
            var postsIdStr = $('.dynamic-posts-record').find('.posts-body').map(function(){
                return this.id.replace('posts-', '');
            }).get().join(",");
            if(postsIdStr.length == 0){
                return;
            }
            var requestURI = '/posts/mood/list/jsonp';
            $.ajax({
                url: requestURI,
                jsonp: 'callback',
                dataType: 'jsonp',
                data: 'ids='+postsIdStr,
                success: function( response ) {
                    try{
                        template.drawMoodResult(response);
                    }catch(e){ }
                }
            });
        };
        //回复点赞计数更新
        da.updateMoodCounter = function(jqEle){
            var cc = jqEle.find('span');
            var upC = 1;
            if(cc.length == 1){
                upC += parseInt(cc.html());
            }else{
                cc = $('<span></span>').appendTo(jqEle);
            }
            cc.html(upC);
        };
        //消息收件箱中条目的未读
        da.unreadInboxMessageSize = function(xhr){
            var requestURI = '/message/size';
            var params = $('#member-message-records').find('.badge').map(function(){
                return this.id.replace('notice_', '');
            }).get().join(",");
            xhr.jsonGet(requestURI+'?sender='+params, {
                jsonCallback : function( data ){
                    if($.isEmptyObject(data)){
                            return $('.badge').hide();
                    }
                    $.each(data, function(index, item) {
                            $('#notice_'+item.id).addClass('badge-info').html(item.title);
                    });
                    //没有的
                    $('.badge').not( ".badge-info").hide();
                }
            });
        };
        //版块中的两个收藏按钮
        da.updateBoardFavoriteCounter = function(jqEle){
            var cp = jqEle.parent();
            var cc = cp.find('.favoriteCounter').html();
            $('.favoriteCounter').html(parseInt(cc) + 1);
            $('a[data-action=star-board]').addClass('disabled').html(jqEle.html().replace("收藏","已收藏"));
        };
        //话题内容页中点击收藏后的回调函数
        da.updateFavoriteCounter = function(jqEle){
            var cp = jqEle.parent();
            var cc = cp.find('.favoriteCounter').html();
            cp.find('.favoriteCounter').html(parseInt(cc) + 1);
            //更新
            jqEle.addClass('disabled').html(jqEle.html().replace("收藏","已收藏"));
        };
        //话题内容页中点赞后的回调函数
        da.updateLikeCounter = function(jqEle){
            var cp = jqEle.parent();
            var cc = cp.find('.likeCounter').html();
            cp.find('.likeCounter').html(parseInt(cc) + 1);
            //更新
            jqEle.addClass('disabled').html(jqEle.html().replace("赞","已赞"));
        };
        //删除标签后的回调
        da.delTagHandler = function(jqEle){
            console.log('remove tag callback');
            jqEle.remove();
        };
        //添加标签后的回调
        da.newTagHandler = function(json){
            if(json.level === 'acc'){
                //插入到标签列中
                var T = '<a href="javascript:;" class="action-cmd tag-item-anchor" data-handler="%APP%/topic/tag/delete" data-query="id:%id%" data-function="delTagHandler">%names%</a>';
                    T = T.replace('%APP%', BASE).replace('%id%', json.input).replace('%names%', json.names);
                $(T).insertBefore('#tag-action-add');
                return;
            }
            if(json.level ==='err'){
                //提示错误
                util.errorMessage(json.message);
                return;
            }
        };
        //用户中心的移除收藏话题的回调
        da.removeHomeCallbackFun = function(jqEle){
            jqEle.parents('.home-record-item').slideUp(1000, 'linear', function(){
                $(this).remove();
            });
        };
        return da;
    })();
    return jForumXhrDefer;
});