Namespace.register("U.MD.F.S");
U.MD.F.S.SearchPostion; //搜索的时候判断位置
//#region 本地好友搜索处理
/**
* 搜索好友
*
* @param  {object} 用户信息
* @param  {element} 打印显示的元素
* @param  {object} 回调函数 
* @param  {object} 搜索的类型 
*                   F   好友
*                   G   群
*                   ""  好友和群
*/
U.MD.F.S.searchFriendsOrGroupForm = function (inputs, el, cb, typename) {
    var _searchfriendinfo = [], //搜索好友的数组
        _searchvalue = inputs.value, //获取搜索的内容
        _isserch = U.MD.F.S.searchFriendsKeyboardHandle(el, inputs) //是否允许搜索
    ;
    //判断用户键盘输入的内容,是否需要进行搜索
    if (_isserch) {
        //获取搜索好友的值
        if (!typename || typename == "F") {
            _searchfriendinfo = _searchfriendinfo.concat(U.Json.like(US.friend.friends, { "FriendsDescript": _searchvalue, "UserNickName": _searchvalue, "UserName": _searchvalue })); //获取用户好友
        }
        //获取搜索群的值
        if (!typename || typename == "G") {
            _searchfriendinfo = _searchfriendinfo.concat(U.Json.like(US.friend.group, { "GroupManageName": _searchvalue })); //获取好友群
        }
        //显示清空
        el.style.display = "block";
        el.innerText = "";
        //搜索好友的打印
        U.MD.F.S.printSearchFriends(_searchfriendinfo, el, cb);
    };
}
/**
* 搜索键盘的处理
*
* @param  {object} 用户信息
*/
U.MD.F.S.searchFriendsKeyboardHandle = function (el, inputs) {
    var _keycode;
    if (event) {
        _keycode = event.keyCode
    } else {
        _keycode = 0
    } //获取键盘的值
    _childs = $(el).Child(), //搜索的内容子元素
        _searchvalue = inputs.value, //搜索的值
        _position = U.MD.F.S.SearchPostion, //搜索的位置
        _searchel = _childs[_position] //搜索得到的元素
    ;
    //判断搜索的值是否存在,不存在则隐藏
    if (_searchvalue == "" || _searchvalue == inputs.defaultValue) {
        el.style.display = "none";
    }
    //如果搜索的内容存在,同时用户执行了回车处理
    else if (_position > -1 && _keycode == 13 || _keycode == 10 && _childs[_position].onmousedown) {
        _childs[_position].ondblclick();
    }
    //如果不是上下键,那么允许用户继续搜索
    else if (_keycode != 38 && _keycode != 40) {
        U.MD.F.S.SearchPostion = -1; //搜索选择的位置设置为-1
        return true;
    }
    return false;
}
/**
* 打印搜索好友
*
* @param  {object} 用户信息
*/
U.MD.F.S.printSearchFriends = function (friendinfo, el, cb) {
    var i,
            _name,
            _tempel,
            _temponeel,
            _temptwoel,
            _frag = $$("frag"),
            _childs = $(el).Child();
    //没有搜索到好友的提示
    if (!friendinfo[0]) {
        $$("div", { "className": "U_MD_F_S_Local_NoData", "innerText": "很抱歉,没有找到您搜索的好友" }, el);
    }
    else {
        //循环打印搜索的好友
        for (i = 0; i < friendinfo.length; i++) {
            (function (i) {
                //搜索的名字
                _name = friendinfo[i].GroupManageName || friendinfo[i].FriendsDescript || friendinfo[i].UserNickName || friendinfo[i].UserName;
                //打印的样式
                _tempel = $$("div", {
                    "title": "点击查看",
                    "className": "U_MD_F_S_Local_Result",
                    "onclick": function () {
                        U.MD.F.S.switchSearchFriendsOrGroup(el, i);
                    },
                    "ondblclick": function () {
                        U.MD.F.W.popupFriendsForm(friendinfo[i]);
                        U.MD.F.S.hideSearchFriendsOrGroupForm(el);
                    }
                }, _frag);
                _temptwoel = $$("div", { "className": "U_MD_F_H_ZLZOCOI", "title": _name }, _tempel);
                if (friendinfo[i].GroupManageID) {
                    U.MD.F.printGroupHeadImage(friendinfo[i], _temptwoel, 4, [35, 35]);
                }
                else {
                    $$("img",
                {
                    "onerror": U.MD.C.imgError,
                    "className": "U_MD_HomeSSZJDAO",
                    "src": U.MD.C.getHeadImage(friendinfo[i].UserThumbnailImageHead)
                },
                 _temptwoel);
                }
                $$("span", { "className": "U_MD_F_S_Local_UserName", "innerHTML": _name }, _tempel);
            })(i);
        }
        el.appendChild(_frag);
    }
    $('body').unbind('keyup', U.MD.F.S.SwitchFunction);
    U.MD.F.S.SwitchFunction = U.UF.C.closure(U.MD.F.S.switchSearchFriendsOrGroup, [el]);
    $('body').bind('keyup', U.MD.F.S.SwitchFunction);
    U.MD.F.S.SearchPostion = -1; //搜索选择的位置设置为-1
    return _childs;
}
/**
* 上下切换搜索的好友或者群
*
* @param  {element} 切换元素
* @param  {number} 当前所在的位置
*/
U.MD.F.S.switchSearchFriendsOrGroup = function (el, switchposition) {
    var _switchel,
        _top,
        _scrolltop,
        _isswitch = null,
        _keycode = event.keyCode, //键盘码
        _childs = $(el).Child(), //获取所有搜索的值
        _postion = U.MD.F.S.SearchPostion, //当前选择好友或者去的位置
        _nowswitchel = _childs[switchposition || U.MD.F.S.SearchPostion] //之前聚焦显示的元素
    ;
    if (_keycode == 38) {
        _isswitch = -1
    } else if (_keycode == 40) {
        _isswitch = 1
    }
    //判断是否需要重新选择
    if (_isswitch || (switchposition != null && switchposition != _postion)) {
        //如果需要指定切换到那个位置
        if (switchposition != null) {
            U.MD.F.S.SearchPostion = switchposition;
        }
        //上下切换的处理
        else {
            U.MD.F.S.SearchPostion += _isswitch; //加上上下的处理  如果键盘向上则-1 向下+1
        }
        //如果位置大于最后一个,则位置设置为最后一个
        if (U.MD.F.S.SearchPostion > _childs.length - 1) {
            U.MD.F.S.SearchPostion = 0; //设置位置
        } else if (U.MD.F.S.SearchPostion < 0) {
            U.MD.F.S.SearchPostion = _childs.length - 1;
        }
        switchposition = U.MD.F.S.SearchPostion;
        _switchel = _childs[switchposition]; //判断选择好友或者群有没有元素
        //选择的位置
        if (_switchel) {
            $('.U_MD_F_S_Local_Result_Hover').removeClass('U_MD_F_S_Local_Result_Hover')
            _switchel.className = "U_MD_F_S_Local_Result U_MD_F_S_Local_Result_Hover"; //取消上一个
            //下面计算需要显示的元素是否在滚动条可见范围,如果不在,那么滚动滚动条设置
            _top = _switchel.offsetTop;
            _scrolltop = el.scrollTop;
            if (_top > el.offsetHeight + _scrolltop - 40) {
                el.scrollTop = _top - el.offsetHeight;
            }
            if (_top < el.scrollTop) {
                el.scrollTop = _top; //滚动条的位置
            }
        }
    }
}
/**
* 隐藏
*
* @param  {element} 切换元素
*/
U.MD.F.S.hideSearchFriendsOrGroupForm = function (el) {
    $("body")[0].focus();
    el.style.display = "none";
    $('body').unbind('keyup', U.MD.F.S.SwitchFunction);
    $(el).prev().Child()[0].value = "联系人、好友讨论组";
}
//#endregion
//#region 窗体搜索
/**
* 弹出群搜索
*
*/
U.MD.F.S.popupsSearchGroups = function () {
    //弹出群搜索的窗体
    var _form = new U.UF.UI.form('
群文档搜索查看区域
',
                                 "",
                                 {
                                     "resize": U.MD.F.S.searchGroupsFormSize,
                                     "id": "U_MD_F_S",
                                     "style": {
                                         "width": "71.5%",
                                         "height": "70%"
                                     }
                                 },
                                 null,
                                 {
                                     "style": {
                                         "padding": "10px 0"
                                     }
                                 });
    var _formel = _form.form; //获取整个窗体
    $(_formel).find("input")[0].focus(); //窗体头部的输入框聚焦
    $('#U_MD_F_S_TDR_GF')[0].onclick = function () {
        U.MD.F.S.searchAjax(1, 1, _formel.children[2].children[0], $('#U_MD_F_S_GF_Search')[0])
    }
    U.MD.F.S.searchGroupsFormSize(_formel); //窗体的大小设置
    U.MD.F.S.searchAjax($(_form.middle).Child()[1], 1, _formel.children[2].children[0], $('#U_MD_F_S_GF_Search')[0]); //初始化数据
    return _formel;
}
/**
* 弹出Ftp搜索
* 
*/
U.MD.F.S.popupsSearchFtp = function () {
    var _form = new U.UF.UI.form('FTP共享区域查看
',
                                 "",
                                 {
                                     "resize": U.MD.F.S.searchGroupsFormSize,
                                     "id": "U_MD_F_F",
                                     "style":
                                        {
                                            "width": "70%",
                                            "height": "70%"
                                        }
                                 },
                                 null,
                                 {
                                     "style": {
                                         "padding": "10px 0"
                                     }
                                 });
    var _formel = _form.form; //获取整个窗体
    $('#U_MD_F_S_TDR')[0].onclick = function () {
        U.MD.F.S.searchAjax(1, 0, $("#U_MD_F_F")[0].children[2].children[0], $("#U_MD_F_S_FTP_Search")[0])
    }
    $(_formel).find("input")[0].focus(); //窗体头部的输入框聚焦
    U.MD.F.S.searchGroupsFormSize(_formel); //窗体的大小设置
    U.MD.F.S.searchAjax($(_form.middle).Child()[1], 0, _formel.children[2].children[0], $('#U_MD_F_S_FTP_Search')[0]); //初始化数据
    return _formel;
}
/**
* 搜索内容大小变化
*
*/
U.MD.F.S.searchGroupsFormSize = function (contentel) {
    var _parentel = $(contentel).Parent(3);
    contentel.style.width = Math.floor(_parentel.offsetWidth / 225) * 225 + "px";
    $(contentel).Parent().style.height = _parentel.offsetHeight - 125 + "px";
}
/**
* 搜索开始
*
*/
U.MD.F.S.searchAjax = function (page, isgroupserch, contentel, inputel) {
    var _searchvalue = inputel.value == inputel.defaultValue ? "" : inputel.value;
    //判断是否
    if (_searchvalue != "") {
        U.MD.F.S.searchGroupOrFriend(page, isgroupserch, contentel, inputel);
    }
    //热门查看
    else {
        U.MD.F.S.getHotFriendOrGroup(1, isgroupserch, contentel);
    }
}
/**
* 搜索开始
*
*/
U.MD.F.S.searchGroupOrFriend = function (page, isgroupserch, contentel, inputel) {
    var _searchvalue = inputel.value == inputel.defaultValue ? "" : inputel.value, //输入的内容
        _end = page * 80, //结束的位置
        _start = _end - 79, //开始的位置
        _loadingel = page > 1 ? $(contentel).parentElement(2).Child()[2] : $(contentel).parentElement()[0]
    ;
    //群搜索的ajax处理
    if (isgroupserch) {
        //后台搜索群
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "SearchGroupInfo", _searchvalue, _start, _end],
                    U.MD.F.S.ajaxSearchAjax, [_loadingel, page, _searchvalue, contentel, isgroupserch]);
    }
    //ftp搜索的ajax处理
    else {
        //后台搜索好友的ftp
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "SearchFriendInfo", US.userInfo.userid || US.EMPTYGUID, _searchvalue, _start, _end],
                    U.MD.F.S.ajaxSearchAjax, [_loadingel, page, _searchvalue, contentel, isgroupserch]);
    }
    //滚动加载设置
    U.UF.EV.scrollLoad($(contentel).Parent(), function () {
        U.MD.F.S.searchAjax(page + 1, isgroupserch, contentel, inputel);
    });
}
/**
* 获取热门的群或者好友
*
*/
U.MD.F.S.getHotFriendOrGroup = function (page, isgroup, el) {
    var i,
        _loadingel = page > 1 ? $(el).parentElement(2).Child()[2] : $(el).parentElement()[0],
        _end = page * 80,
        _start = _end - 79
    ;
    //滚动加载设置
    if (page == 1) {
        //设置滚动加载函数
        U.UF.EV.scrollLoad($(el).Parent(), function () {
            U.MD.F.S.getHotFriendOrGroup(page + 1, isgroup, el); //加载热门好友和群
        });
    }
    //群的加载处理
    if (isgroup) {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "RecommendGroups", _start, _end],
                    U.MD.F.S.asynGetHotFriendOrGroup, [_loadingel, el, isgroup]);
    }
    //好友ftp加载处理
    else {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "GetFtpHotUser", _start, _end],
                    U.MD.F.S.asynGetHotFriendOrGroup, [_loadingel, el, isgroup]);
    }
}
/**
* 热门群或者好友获取异步
*
*/
U.MD.F.S.asynGetHotFriendOrGroup = function (r) {
    var _context = r.context,
        _el = _context[1], //打印区域的元素
        _isgroup = _context[2]; //是否是群的处理
    r = r.value;
    //打印内容
    U.MD.F.S.printSearchContent(r, _el, _isgroup);
    //如果搜索的内容已经不足的时候,那么就取消滚动加载函数
    if (r.length < 80) {
        $(_el).Parent().onscroll = null;
    }
}
/**
* 搜索异步
*
*/
U.MD.F.S.ajaxSearchAjax = function (r) {
    //参数处理
    var _context = r.context,
        _page = _context[1], //页码
        _searchvalue = _context[2], //搜索内容
        _el = _context[3],
        _isgroup = _context[4]; //是否是群搜索
    r = r.value;
    //如果是第一页
    if (_page == 1) {
        _el.innerHTML = ""; //清空之前的数据
        //如果获取的值不存在,那么就直接打印搜索是否为空
        if (!r || !r.length) {
            U.MD.F.S.printNullSearch(_el); //打印空搜索
        }
    }
    //如果搜索有内容,那么打印内容
    if (r && r.length) {
        U.MD.F.S.printSearchContent(r, _el, _isgroup); //打印搜索营部
    }
}
/**
* 打印搜索内容
*
*/
U.MD.F.S.printSearchContent = function (friendsinfo, el, isgroup) {
    var i, _el, _tempel, _tempela, _tempelb, _tempelc, _arr,
        _userid = US.userInfo.userid || "",
        _frag = $$("frag");
    if (isgroup) {
        _arr = ["GroupManageName", "+加群"];
    }
    else {
        _arr = ["UserNickName", "+好友"];
    }
    //循环打印好友数据
    for (i = 0; i < friendsinfo.length; i++) {
        _el = $$("div", { "className": "U_MD_F_S_Pop_Content_G U_MD_D_Radius_5",
            "onclick": U.UF.C.closure(function (friendinfo) {
                if (isgroup) {
                    //      U.MD.U.V.GroupInfo(friendinfo);
                }
                else {
                    U.MD.D.I.openApplication("disk", { "userid": friendinfo.UserId, "directoryid": US.FTPFOLDERID });
                }
            }, [friendsinfo[i]])
        }, _frag);
        _tempel = $$("div", { "className": "U_MD_F_S_Pop_Content_G_T" }, _el);
        _tempela = $$("div", { "className": "U_MD_F_S_Pop_Content_G_T_L" }, _tempel);
        $$("img", { "onerror": U.MD.C.imgError,
            "onerror": U.MD.C.imgError,
            "src": isgroup ? "" : U.MD.C.getHeadImage(friendsinfo[i]["UserThumbnailImageHead"]),
            "title": friendsinfo[i][_arr[0]],
            "onclick": U.UF.C.closure(function (friendinfo) {
                U.UF.EV.stopBubble();
                //群处理
                if (isgroup) {
                    //   U.MD.U.V.GroupInfo(friendinfo);
                }
                //好友处理
                else {
                    U.MD.U.V.ViewOtherUserInfo(friendinfo.UserId);
                }
            }, [friendsinfo[i]])
        }, _tempela);
        _tempelb = $$("div", { "className": "U_MD_F_S_Pop_Content_G_T_R" }, _tempel);
        $$("div", { "className": "U_MD_F_S_Pop_Content_G_T_R_T U_MD_D_Text_Abbreviation", "innerHTML": friendsinfo[i][_arr[0]] }, _tempelb);
        $$("div", { "className": "U_MD_F_S_Pop_Content_G_T_R_C U_MD_D_Text_Abbreviation", "innerHTML": (isgroup ? "目前人数:" : "目前好友:") + (friendsinfo[i].FCount || 0) }, _tempelb);
        $$("div", { "className": "U_MD_F_S_Pop_Content_G_T_R_B U_MD_D_Text_Abbreviation", "innerHTML": "ftp文件:" + friendsinfo[i].DCount || 0 }, _tempelb);
        _tempelc = $$("div", { "className": "U_MD_F_S_Pop_Content_G_B" }, _el);
        $$("div", { "className": "U_MD_F_S_Pop_Content_G_B_A U_MD_D_Radius_5", "innerHTML": _arr[1],
            "onclick": U.UF.C.closure(function (friendinfo) {
                U.UF.EV.stopBubble();
                if (isgroup) {
                    U.MD.F.J.addUsersToGroup(friendinfo, function () {
                        U.MD.D.I.openApplication("friend", { "userid": _userid, "directoryid": friendinfo.GroupManageID }, friendinfo);
                    });
                }
                else {
                    U.MD.F.M.addFriend(friendinfo);
                }
            }, [friendsinfo[i]])
        }, _tempelc);
    }
    el.appendChild(_frag);
}
/**
* 空查询
*
*/
U.MD.F.S.printNullSearch = function (el) {
    $$("div", { "className": "U_MD_F_S_CXT", "innerHTML": "暂无搜索消息, 请稍候搜索。" }, el);
}
//#endregion
//#region 新搜索
/**
* 搜索好友
*
* @param  {object} 用户信息
* @param  {element} 打印显示的元素
* @param  {object} 回调函数
* @param  {object} 搜索的类型
* @param  {number} defaultindex 0好友 1群组 2ftp
*/
U.MD.F.S.popSearchFriendsOrGroupForm = function (defaultindex) {
    var _searchel = $("#U_MD_F_S_Pop")[0],
        _contentel = $($(_searchel).Child()[1]).Child()[0],
        _inputel = $("input", _searchel)[0]
    ;
    _searchel.style.display = "block";
    //弹出群搜索的窗体
    var _form = new U.UF.UI.form("查找好友/群",
                                 _searchel,
                                 {
                                     "resize": function () { U.MD.F.S.searchGroupsFormSize(_contentel); },
                                     "id": "U_MD_F_S",
                                     "style": {
                                         "width": "70%",
                                         "height": "70%"
                                     }
                                 }, null);
    var _formel = _form.form; //获取整个窗体
    _inputel.focus(); //窗体头部的输入框聚焦
    U.MD.F.S.searchGroupsFormSize(_contentel); //窗体的大小设置
    U.MD.F.S.searchFriendsOrGroupFormClick($("span", _searchel)[defaultindex]); //默认选择查找用户
    return _formel;
}
/**
* 搜索好友
*
* @param  {object} 用户信息
*/
U.MD.F.S.clickSearch = function (el) {
    var i,
        _changeel,
        _topel = $(el).Parent(2),
        _childel = $($(_topel).Child()[0]).Child();
    ;
    for (i = 0; i < _childel.length; i++) {
        _changeel = $(_childel[i]).Child()[0];
        if (_changeel.className == "U_MD_F_S_Pop_Head_Nav_fonto") {
            U.MD.F.S.searchFriendsOrGroupFormClick(_changeel);
            return;
        }
    }
}
/**
* 搜索好友
*
* @param  {object} 用户信息
*/
U.MD.F.S.searchFriendsOrGroupFormClick = function (el) {
    var i, j,
        _changeel,
        _parentel = $(el).Parent(2),
        _childel = $(_parentel).Child(),
        _topel = $(_parentel).Parent(2),
        _contentel = $($(_topel).Child()[1]).Child()[0],
        _inputel = $("input", _topel)[0]
    ;
    //循环处理样式
    for (i = 0; i < _childel.length; i++) {
        _changeel = $(_childel[i]).Child()[0];
        //如果是点击的对象
        if (_changeel == el) {
            el.className = "U_MD_F_S_Pop_Head_Nav_fonto";
            j = i;
        }
        //点击的对象
        else {
            _changeel.className = "";
        }
    }
    _contentel.innerText = "";
    U.MD.F.S.searchAjax(1, j == 1, _contentel, _inputel); //初始化数据
}
//#endregion