if (!window.Fetion)
    window.Fetion = { "Web": { "Chat": {}} };
var ua = navigator.userAgent.toLowerCase();
var isIE = ua.indexOf("msie") > -1;
window.FetionWebChat = Fetion.Web.Chat;
window.fxBrowser = { setBg: function() { }, ScrollToBottom: function() { } };
Fetion.Web.Chat.msgList = [];
Fetion.Web.Chat.clsList = [];
Fetion.Web.Chat.ctrlQueue = [];
Fetion.Web.Chat.clsErrList = {};
Fetion.Web.Chat.JSPath = "chat/js";
Fetion.Web.Chat.defined = function(name) {
    var className = name;
    var nameAry = name.split(".");
    var cls = window;
    for (var i = 0; i < nameAry.length; i++) {
        name = nameAry[i];
        if (!cls[name])
            cls[name] = {};
        cls = cls[name];
    }
    cls.className = className;
    Fetion.Web.Chat[name] = Fetion.Web.Chat[name.toLowerCase()] = Fetion.Web.Chat[name.toUpperCase()] = cls;
    window[className.replace(/\./g, "")] = cls;
    return cls;
};

Fetion.Web.Chat.newMsg = function(ddParent, datas) {
    ddParent.jsonStr = datas;
    if (datas.constructor == String)
        datas = eval("(" + datas + ")");
    if (datas.constructor != Array)
        datas = [datas];

    var head = datas[0];
    ddParent.head = head;
    ddParent.datas = datas;
    this.msgList.push(datas);
    for (var i = 1; i < datas.length; i++) {
        var data = datas[i];
        data.parent = ddParent;
        this.newCtrl(data);
    }
};

Fetion.Web.Chat.newCtrl = function(o) {
    if (o.constructor == String)
        o = { type: o };
    o.src = this.JSPath + "/" + o.type + ".js";
    this.ctrlQueue.push(o);
    this.loadSRC(o);
    return this.createCtrl(o);
};

Fetion.Web.Chat.loadSRC = function(o) {
    if (o.constructor == String)
        o = o.indexOf(".") == "-1" ? { type: o} : { src: o };
    if (!o.src) {
        o.src = this.JSPath + "/" + o.type + ".js";
    }
    if (!window.srcMap)
        window.srcMap = {};
    if (!srcMap[o.src]) {
        srcMap[o.src] = o;
        var src = null;
        if (o.src.indexOf(".js") != -1) {
            src = document.createElement("script");
            src.setAttribute("type", "text/javascript");
            src.setAttribute("src", o.src);
        }
        else if (o.src.indexOf(".css") != -1) {
            src = document.createElement("link");
            src.setAttribute("rel", "stylesheet");
            src.setAttribute("type", "text/css");
            src.setAttribute("href", o.src);
        }
        if (src) {
            var head = document.getElementsByTagName("head")[0];
            head.appendChild(src);
        }
    }
    if (o.src.indexOf(".js") != -1 && !o.type)
        o.type = o.src.substring(o.src.replace(/\\/g, "/").lastIndexOf("/") + 1, o.src.lastIndexOf(".")).toLowerCase();
};

Fetion.Web.Chat.createCtrl = function(o) {
    var baseCls = "cardfrm";
    var cls = this.ctrlQueue[0] || baseCls.indexOf(o.type) != -1 ? Fetion.Web.Chat[o.type] : null;
    var obj = null;
    try {
        obj = cls.create(o.sub || o);
    }
    catch (e) {
        setTimeout(function() { Fetion.Web.Chat.createCtrl(o) });
        return null;
    }
    if (this.ctrlQueue[0])
        this.ctrlQueue.shift();
    this.clsErrList[o.type] = null;

    if (!o.id)
        o.id = this.newID(o.type);
    obj.cls = cls;
    obj.data = o;
    obj.id = Fetion.Web.Chat.formatID(o.id);
    obj.setAttribute("name", obj.id);
    obj.type = o.type;
    obj.setAttribute("type", o.type);
    obj.lang = '{"type":"' + o.type + '"}';
    if (o.style)
        this.setStyle(obj, o.style);

    Fetion.Web.Chat.scroll();

    obj.onmousedown = function() {
        if ("IMG,CE,OUT_CE,CE_AUDIO".indexOf(this.type.toUpperCase()) != -1) {
            window.selObj = this;
            window.imgSrc = this.data.path;
            window.selMsgCtrl = this;
        } else {
            window.selMsgCtrl = null;
        }
    }

    if (false && cls.update)
        setTimeout(function() { cls.update(obj, obj.data); }, 1000);
    return obj
};

Fetion.Web.Chat.newID = function(name) {
    while (true) {
        var tmpName = name + "_" + parseInt(Math.random() * 1000);
        var obj = window[tmpName];
        if (!obj)
            return tmpName;
    }
};


Fetion.Web.Chat.updateCtrl = function(datas) {
    if (datas.constructor == String)
        datas = eval("(" + datas + ")");
    if (datas.constructor != Array)
        datas = [datas];
    for (var i = 0; i < datas.length; i++) {
        var data = datas[i];
        var id = Fetion.Web.Chat.formatID(data.id);
        var ctrls = document.getElementsByName(id);
        if (!data.type && ctrls[0])
            data.type = ctrls[0].type;
        var cls = Fetion.Web.Chat[data.type];
        if (cls && cls.update) {
            var ctrls = document.getElementsByName(id);
            var length = ctrls.length;
            for (var i = 0; i < length; i++) {
                var ctrl = ctrls[i];
                if (!ctrl.data)
                    ctrl.data = {};
                for (var attr in data)
                    ctrl.data[attr] = data[attr];
                cls.update(ctrls[i], ctrl.data);
            }
        }
    }
}

Fetion.Web.Chat.setStyle = function(ctrl, style) {
    if (!style)
        return;
    var styles = style.replace(/,/g, ";").replace(/ /g, "").split(";");
    for (var i = 0; i < styles.length; i++) {
        var style = styles[i];
        if (style == "")
            continue;
        style = style.split(":");
        try {
            var styleName = style[0];
            if (styleName.indexOf("-") != -1) {
                var names = styleName.split("-");
                styleName = names[0] + names[1].substring(0, 1).toUpperCase() + names[1].substring(1);
            }
            ctrl.style[styleName] = style[1];
        }
        catch (e) {
            //alert(style + "\n" + e.message);
        }
    }
};

Fetion.Web.Chat.formatDatetime = function(time, format) {
    if (!time)
        return "00";
    time = parseInt(time);

    if (!format)
        format = "hh:mm:ss";

    var fmtAry = ["ss", "mm", "hh"];
    var argAry = [60, 60, 24];
    for (var i = 0; i < fmtAry.length; i++) {
        var fmt = fmtAry[i];
        var arg = argAry[i];
        format = format.replace(fmt, (time % arg + 100 + "").substring(1, 3));
        time = time / arg;
    }
    format = format.replace(/00:/g, "");
    if (format.length == 2)
        format += "'";
    return format;
};

Fetion.Web.Chat.formatID = function(id) {
    return ("_" + id).replace(/\W/, "_");
};

Fetion.Web.Chat.split = function(str, len) {
    if (str.length < len)
        return str;
    len = len * 2 - 3;
    var result = "";
    for (var i = 0; i < str.length && len > 0; i++) {
        var tmp = str.substring(i, i + 1);
        if (tmp.replace(/[^\x00-\xff]/g, "").length == 0)
            len -= 1;
        len -= 1;
        result += tmp;
    }
    result += "...";
    return result;
};

Fetion.Web.Chat.scrollTime = function(scrollTop) {
    //    if (window.searchMsgID != undefined && window.searchMsgID != null) {
    //        var obj = document.getElementById("dd_" + window.searchMsgID);
    //        if (obj) {
    //            var rect = obj.parentNode.getBoundingClientRect();
    //            scrollTop = rect.top;
    //        }
    //    }
    //    if (!scrollTop)
    //        scrollTop = $('.his_ct_m')[0].scrollHeight;
    //    $('.his_ct_m').scrollTop(scrollTop);
    var scrollTop1 = 0;
    if (window.searchMsgID != undefined && window.searchMsgID != null) {
        var obj = document.getElementById("dd_" + window.searchMsgID);
        if (obj) {
            //var rect = obj.parentNode.getBoundingClientRect();
            scrollTop1 = obj.parentNode.offsetTop - 50;
        }
    }
    for (var i = 0; i < window.scrollHeightList.length; i++) {
        if (window.scrollHeightList[i] == scrollTop1) {
            return;
        }
    }
    window.scrollHeightList.push(scrollTop1);
	setTimeout(function() { $('.his_ct_m').scrollTop($('.his_ct_m')[0].scrollHeight); }, 100);
    
}

Fetion.Web.Chat.scroll = function(scrollTop) {
	Fetion.Web.Chat.scrollTime(scrollTop);
    //setTimeout(new function() { Fetion.Web.Chat.scrollTime(scrollTop) }, 1000);
}


