Fetion.Web.Chat.defined("Fetion.Web.Chat.Relay");
Fetion.Web.Chat.Relay.create = function (datas)
{
    //msgDIV.innerHTML = "/" + datas + "/" + Math.random();
    try
    {
        if (datas.constructor == String)
            datas = eval("(" + datas + ")");
        if (datas.constructor != Array)
            datas = [datas];

        var html = "";
        html += "<table style='font-size:12px;margin-top:-10px;'>";
        html += "<tr><td style='padding:5px'><table><tr><td><div></div></td></tr></table></td></tr>";
        html += "<tr><td>";
        html += '<div class="comment" style="padding:0 10px 0 5px">';
        html += "<table cellspacing='0' cellpadding='0' style='width:100%;height:50px;font-size:0'>";
        html += '<tr><td class="frm_tl"></td><td class="frm_tm"></td><td class="frm_tr"></td></tr>';
        html += '<tr><td class="frm_ml"></td><td valign="top" style="background:#FFFFFF"><textarea style="width:100%;height:100%;border:0;overflow:auto;font-size:12px;padding:5px"></textarea></td><td class="frm_mr"></td></tr>';
        html += '<tr><td class="frm_bl"></td><td class="frm_bm"></td><td class="frm_br"></td></tr>';
        html += '</table>';
        html += '<p class="tips">还能输入<span></span>个字</p>';
        html += '</div>';
        html += '<div class="friend_pane">';
        html += '<h1 style="padding-left:5px"><img src="chat/img/tip01.png"></h1>';
        html += '<div class="pop_xc" style="display:none">';
        html += '<div class="box_s">';
        html += '<span class="arrow_t"></span>';
        html += '<div class="box_sl"></div>';
        html += '<div class="box_sm"><span>已选联系人超出限额</span><a class="close_s1" onclick="this.parentNode.parentNode.parentNode.style.display=\'none\';"></a></div>';
        html += '<div class="box_sr"></div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += "</td></tr>";
        html += "<tr><td valign='top'><div class='name_list'><ul></ul></div></td></tr>";
        html += "<tr><td><div class='btn_pane' style='position:absolute;right:0;bottom:0'>";
        html += "<input id='relayBtn' type='submit' class='btn_02' value='转发' disabled/><input id='cancelBtn' type='reset' class='btn_02' value='取消' onclick='window.external.CloseWnd()'/>";
        html += "</div></td></tr>";
        html += "</table>";

        var frm = document.createElement("SPAN");
        frm.innerHTML = html;
        window.relayFrm = frm.childNodes[0];
        var btnPane = relayFrm.rows[relayFrm.rows.length - 1].cells[0].childNodes[0];

        relayFrm.ctrls = relayFrm.rows[0].cells[0].childNodes[0].rows[0].cells[0].childNodes[0];
        var textArea = relayFrm.rows[1].cells[0].childNodes[0].childNodes[0].rows[1].cells[1].childNodes[0];
        textArea.limitDiv = relayFrm.rows[1].cells[0].childNodes[0].childNodes[1].childNodes[1];
        relayFrm.friend = relayFrm.rows[1].cells[0].childNodes[1].childNodes[0];
        relayFrm.friendLimit = relayFrm.rows[1].cells[0].childNodes[1].childNodes[1];
        relayFrm.users = relayFrm.rows[2].cells[0].childNodes[0].childNodes[0];
        relayBtn = btnPane.childNodes[0];

        for (var i = 0; i < datas.length; i++)
        {
            var data = datas[i];
            data.type = data.type.toUpperCase();
            if (data.type == "HEAD")
                continue;
            if (data.type == "AUDIO")
            {
                relayFrm.ctrls.style.marginBottom = "5px";
                textArea.limitDiv.parentNode.style.marginBottom = "30px";
            }
            else if (data.type == "FOCUS_PICTEXT")
            {
                relayFrm.friend.style.marginTop = "-10px";
            }

            data.op = false;
            data.parent = relayFrm.ctrls;
            var ctrl = Fetion.Web.Chat.newCtrl(data);
        }

        textArea.text = "给好友留言";
        textArea.limit = 140;
        textArea.onfocus = function ()
        {
            if (this.value == this.text)
            {
                this.style.color = "";
                this.value = "";
            }
        };
        textArea.onblur = function ()
        {
            if (this.value == "")
            {
                this.style.color = "#777777";
                this.value = this.text;
                this.limitDiv.innerHTML = this.limit;
            }
        };
        textArea.onpropertychange = textArea.oninput = function ()
        {
            if (!this.limitDiv)
                return;
            var value = this.limit - this.value.length;
            if (value < 0)
            {
                this.value = this.value.substring(0, this.limit);
                value = 0;
                //this.limitDiv.style.color = "red";
            }
            this.limitDiv.innerHTML = value;
        };
        relayBtn.textArea = textArea;
        relayBtn.onclick = function ()
        {
            var textArea = this.textArea;
            if (textArea.value == textArea.text)
            {
                window.external.SendMsg("");
                return;
            }
            window.external.SendMsg(textArea.value);
        };

        textArea.onblur();
        textArea.limitDiv.innerHTML = textArea.limit;

        Fetion.Web.Chat.body.appendChild(frm);

        relayBtn.onmouseover = cancelBtn.onmouseover = function ()
        {
            if (this.disabled)
                return;
            this.className = "btn_01";
        };
        relayBtn.onmouseleave = cancelBtn.onmouseleave = function ()
        {
            this.className = "btn_02";
        };
    }
    catch (e)
    {
        //msgDIV.innerHTML = e.message + Math.random();
        setTimeout(function () { Fetion.Web.Chat.Relay.create(datas) });
    }

    return frm;
};
Fetion.Web.Chat.Relay.addUser = function (users)
{
    if (users.constructor == String)
        users = eval("(" + users + ")");
    if (users.constructor != Array)
        users = [users];
    for (var i = 0; i < users.length; i++)
    {
        var user = users[i];
        html = "";
        html += '<li class="long_name" title="' + user.name + '">';
        html += '<div class="per_pho">';
        html += '<img src="' + user.pic + '"/>';
        html += '</div>';
        html += '<div class="user_name">' + user.name.replace(/</g, "&lt;") + '</div>';
        html += '<div class="del" style="display:none"><a class="del_frd"></a></div>';
        html += '</li>';
        var userObj = document.createElement("SPAN");
        userObj.innerHTML = html;
        userObj = userObj.childNodes[0];
        relayFrm.users.appendChild(userObj);
        userObj.id = userObj.name = "user" + user.id;
        var delObj = userObj.childNodes[2];
        userObj.delObj = delObj;
        userObj.onmouseover = function ()
        {
            this.delObj.style.display = "block";
        };
        userObj.onmouseleave = function ()
        {
            this.delObj.style.display = "none";
        };
        userObj.user = delObj.user = user;
        delObj.onclick = function ()
        {
            Fetion.Web.Chat.Relay.delUser(this.user.id, true);
        };
    }
    relayFrm.friend.innerHTML = "转发给以下好友（<span>" + relayFrm.users.childNodes.length + "/5</span>）";
    relayBtn.disabled = false;
    Fetion.Web.Chat.scroll();
};
Fetion.Web.Chat.Relay.delUser = function (userID, external)
{
    var userObj = document.getElementById("user" + userID);
    if (!userObj)
        return;
    var user = userObj.user;
    userObj.parentNode.removeChild(userObj);
    relayFrm.friendLimit.style.display = "none";
    relayFrm.friend.innerHTML = relayFrm.users.childNodes.length == 0 ? "<img src='chat/img/tip01.png'>" : "转发给以下好友（<span>" + relayFrm.users.childNodes.length + "/5</span>）";
    if (relayFrm.users.childNodes.length == 0)
    {
        //relayFrm.users.style.height = "auto";
        relayBtn.disabled = true;
    }
    Fetion.Web.Chat.scroll();

    //调用客户端删除接口
    if (external)
        window.external.DelReceiver(user.id + "", user.type + "");
}
Fetion.Web.Chat.Relay.overload = function ()
{
    relayFrm.friendLimit.style.display = "block";
    setTimeout(function () { relayFrm.friendLimit.style.display = "none" }, 5000);
}