/*
问题:
1、整套好友的功能流程图
2、设计缺少流程 只有大流程 没有那个按钮的细节流程
*/
Namespace.register("U.F"); //好友主命名空间

//#region 初始化好友

//初始化好友函数

/**
*   初始化加载好友
*/
U.F.InitFriend = U.F.CSHFG = function () {
    //如果第三方应用1473,包含了好友窗体。
    if ($("#UD_SYHZ")[0]) {
        var i,
            _selectbottomel,
            _childsel,
            _chatform = $("#UD_SYHZ")[0], //好友窗体
            _userinfoel = $("#UD_SYHZLTI", _chatform)[0]; //多好友分栏

        //初始化出现右边的好友聊天
        U.F.W.FriendDisplayAndNone([{ "display": "none", "left": "auto" },
                    { "display": "block", "top": (US.Height - 530) / 2, "right": "0px"}], true);

        //添加当前使用用户的头像
        $($("img", _userinfoel)[0] || $$("img")).addAttrArray({ "onerror": U.M.ImgError,
            "src": U.M.GetImgU(US.userInfo.UserThumbnailImageHead), "onclick": U.U.DKGEZL
        }).appendTo(_userinfoel);

        //右侧好友最近联系人
        U.F.PrintRightFormFriend(US.Friend[4].length ? US.Friend[4] : US.Friend[1], $("#UD_SYHRC")[0]);

        //我的好友、我的群组等按钮,默认显示我的好友 所以找到我的好友的按钮,并且默认调用onclick
        _selectbottomel = $("#UD_SYHZLTA").Child()[0];
        $(_selectbottomel).Child()[0].onclick();
    }
}

/**
* 初始化右边好友出现
*
* @param  {object} 好友头像处理
* @param  {element} 元素处理
*/
U.F.PrintRightFormFriend = function (friendinfo, el) {
    var i,
        _tempel,
        _temponeel,
        _frag = $$("frag"),
        _childs = $(el).Child();

    //如果是用户登录多次,移除之前残留的信息
    for (i = 1; i < _childs.length; i++) {
        el.removeChild(_childs[i]);
    }

    //如果有好友资料输出
    if (friendinfo.length) {
        //循环打印好友资料
        for (i = 0; (i < friendinfo.length && i < 11); i++) {
            $$("img", { "onerror": U.M.ImgError, "title": friendinfo[i]["FriendsDescript"],
                "className": "UD_SYHRCI", "src": U.M.GetImgU(friendinfo[i].UserThumbnailImageHead),
                "alt": friendinfo[i].FriendsDescript || friendinfo[i].UserNickName,
                "onclick": [[U.M.StopBubble], [U.F.W.PopupFriendsForm, [friendinfo[i]]]]
            }, _frag);
        }
    }
    //暂无好友追随
    else {
        _tempel = $$("div", { "className": "UD_SYHRCT" }, _frag);
        _temponeel = $$("div", { "className": "UD_SYHRCTS" }, _tempel);
        $$("img", { "src": "/img/YSUN.png" }, _temponeel);
        $$("div", { "className": "UD_SYHRCTZ UD_SYI" }, _tempel);
        $$("div", { "className": "UD_SYHRCTX", "innerHTML": "暂时无追随" }, _tempel);
    }
    el.appendChild(_frag);
}

/**
* 用户列表好友打印
*
* @param  {element} 元素11
*/
U.F.PrintFriendGroupOrFriend = function (el) {

    var i,
        _tempel,
        _temponeel,
        _classinfo,
        _friendinfo = US.Friend[0],
        _frag = $$("frag");
    el.innerText = "";
    //如果用户有好友则打印
    if (US.Friend[1].length) {
        //循环打印好友分组
        for (i = 0; i < _friendinfo.length; i++) {
            _classinfo = _$(US.Friend[1]).Select({ "FriendsGroupID": _friendinfo[i].FriendsGroupID }); //根据好友分组获取好友
            //打印好友分组
            _tempel = $$("div", { id: "g" + _friendinfo[i].FriendsGroupID, "TID": _friendinfo[i].FriendsGroupID, "className": "UD_SYHZLZO" }, _frag);
            $$("div", { "className": "UD_SYHZLZOT", "innerHTML": _friendinfo[i].FriendsGroupName + "(" + _classinfo.length + ")",
                "onclick": [[U.F.OpenOrCloseFriendClass, [_tempel]]], "oncontextmenu": [[U.F.R.FriendClassRightClickMenu, [US.Friend[0][i], "this"]]]
            }, _tempel);
            _temponeel = $$("div", { "className": "UD_SYHZLZOC" }, _tempel);
            //打印好友
            U.F.PrintFriendOrGroup(_temponeel, _classinfo);
        }
    }
    //无好友打印提示
    else {
        U.F.PrintHasNoFriend(_frag);
    }
    el.appendChild(_frag);
}

/**
* 好友和群样式输出
*
* @param  {element} 元素
* @param  {array} 好友数组
*/
U.F.PrintFriendOrGroup = function (el, friendorgroupinfo, isinsert) {
    var i,
        _isfront,
        _friendel,
        _friendoneel,
        _friendtwoel,
        _onlinefrag = $$("frag"),
        _offlinefrag = $$("frag");

    //循环所有的好友或者群,打印到左边好友或群面板中
    for (i = 0; i < friendorgroupinfo.length; i++) {
        _friendel = $$("div", { "className": "UD_SYHZLZOCO", "onclick": [[U.F.W.PopupFriendsForm, [friendorgroupinfo[i], "this"]]], "TID": friendorgroupinfo[i].GroupManageID || friendorgroupinfo[i].UserId,
            "oncontextmenu": friendorgroupinfo[i].UserId ? [[U.F.R.FriendsRightClickMenu, ["this", friendorgroupinfo[i]]]] : ""
        });
        _friendoneel = $$("div", { "className": "UD_SYHZLZOCOI" }, _friendel);

        //如果是群的处理,群的头像输出
        if (friendorgroupinfo[i].GroupManageID) {
            U.F.PrintGroupHeadImage(_$(US.Friend[2]).Select({ "GroupManageID": friendorgroupinfo[i].GroupManageID })[0], _friendoneel, 4, [35, 35]);
        }
        //好友头像输出
        else {
            $$("img", { "onerror": U.M.ImgError, "title": "点击查看", "src": U.M.GetImgU(friendorgroupinfo[i].UserThumbnailImageHead) }, _friendoneel);
        }
        _friendtwoel = $$("div", { "className": "UD_SYHZLZOCOR UTDF" }, _friendel);
        $$("div", { "className": "UD_SYHZLZOCORN UTDF", "innerHTML": friendorgroupinfo[i].FriendsDescript || friendorgroupinfo[i].UserNickName || friendorgroupinfo[i].GroupManageName }, _friendtwoel);
        $$("div", { "className": "UD_SYHZLZOCORT UTDF", "innerHTML": friendorgroupinfo[i].UserIndividualitysignature || friendorgroupinfo[i].GroupManageRemark || "" }, _friendtwoel);
        //如果用户在线那么插入到最前面
        if (friendorgroupinfo[i].IsLogin == 1) {
            _onlinefrag.appendChild(_friendel);
        }
        //否则插入到后面
        else {
            _offlinefrag.appendChild(_friendel);
        }
    }
    el.insertBefore(_onlinefrag, el.firstChild); //在线用户插入到最前面
    //如果需要插入到最前面,那么则不管在不在线的用户都插入到最前面
    if (isinsert) {
        el.insertBefore(_offlinefrag, el.firstChild); //用户插入到最前面
    }
    else {
        el.appendChild(_offlinefrag); //非在线用户插入到最后米娜
    }

}

/**
* 打开分组展开好友
*
* @param  {element} 展开好友的区域
*/
U.F.OpenOrCloseFriendClass = function (el) {
    var _firendel = $(el).Child()[1]; //获取好友分组对应的好友元素
    //如果现在隐藏了则打开
    if (_firendel.style.display == "none") {
        _firendel.style.display = "block";
    }
    //如果是显示则隐藏
    else {
        _firendel.style.display = "none";
    }
}

/**
* 打印空好友
*
* @param  {element} 打印的区域
*/
U.F.PrintHasNoFriend = function (el) {
    var _tempel,
        _temponeel
    ;
    _tempel = $$("div", { "className": "UD_SYHZLZT" }, el);
    _temponeel = $$("div", { "className": "UD_SYHZLZTS" }, _tempel);
    $$("img", { "src": "/img/YSUN.png" }, _temponeel);
    $$("div", { "className": "UD_SYHZLZTZ UD_SYI" }, _tempel);
    $$("div", { "className": "UD_SYHZLZTX", "innerHTML": "暂时没有添加追随!!" }, _tempel);
}

//#endregion

//#region 好友功能

/**
* 打印空好友
*
* @param  {string} 切换类型
* @param  {element} 点钱点击的区域
* @param  {element} 变化的样式
*/
U.F.SwitchFriendOrGroup = function (typename, el, classname) {
    var i,
        _chatform = $("#UD_SYHZLZ")[0], //打印内容区域
        _childsel = $(el).parentg(2).Child(), //所有参与切换的元素
        _classarray = ["UD_SYHZLTO", "UD_SYHZLTT", "UD_SYHZLTS", "UD_SYHZLTF"]; //切换样式数组

    //切换按钮的样式,把样式改成用户默认按钮的样式
    for (i = 0; i < _childsel.length; i++) {
        $(_childsel[i]).Child()[0].className = _classarray[i] + " UD_SYI UD_SYHZLTOK";
    }
    //设置选中的按钮样式
    el.className = classname + " UD_SYI UD_SYHZLTOK";

    //点击好友添加按钮
    if (typename == "A") {
        U.F.M.CreateFriendGroupPopup(); //调用创建群弹框
    }
    else {
        //群成员或者最近联系人输出
        if (typename == "G" || typename == "H") {
            _chatform.innerText = ""; //清空展示区域的内容
            U.F.PrintFriendOrGroup(_chatform); //打印
        }
        //打印好友,由于好友有好友分组,所以不能直接调用U.F.PrintFriendOrGroup输出
        else {
            U.F.PrintFriendGroupOrFriend(_chatform); //打印
        }
    }
}

/**
* 获取群里头像集
*
* @param  {object} 群信息
* @param  {element} 添加指定的元素
* @param  {element} 变化的样式
* @param  {element} 最大长度
*/
U.F.PrintGroupHeadImage = function (groupinfo, el, num, size) {
    var i, j,
        _groupnameel,
        _imageel,
        _groupuserinfo = _$(US.Friend[3]).Select({ "GroupManageID": groupinfo.GroupManageID }, null, (num || 9)), //获取当前群
        _horizontal = _groupuserinfo.length < 3 ? _groupuserinfo.length : (_groupuserinfo.length > 6 ? 3 : 2), //每排几个
        _vertical = (_groupuserinfo.length > 4) ? 3 : (_groupuserinfo.length > 1) ? 2 : 1; //一共多少行
    size = size || [el.offsetHeight || 40, el.offsetWidth || 40]; //最大长宽
    _groupnameel = $$("div", { "className": "UD_SYSSZFZAIO", "title": groupinfo.GroupManageName }); //群名
    //打印所有群成员头像 横排打印
    for (i = 0; i < _horizontal; i++) {
        _imageel = $$("div", { "style": { "width": "100%", "height": Math.floor(size[0] / _horizontal) + "px"} }, _groupnameel);
        //竖排打印
        for (j = i * _vertical; j < i * _vertical + _vertical && j < _groupuserinfo.length; j++) {
            $$("img", {
                "onerror": U.M.ImgError,
                "src": U.M.GetImgU(_groupuserinfo[j].UserThumbnailImageHead),
                "style": { "width": Math.floor(size[1] / _vertical) + "px",
                    "height": Math.floor(size[0] / _vertical) + "px"
                }
            }, _imageel);
        }
    };
    //追加
    el.appendChild(_groupnameel);
}

//#endregion