﻿(function (id, ln) {
    //一些全局变量
    var _odd = function () { return new _odd.fn.init(arguments); }, _webid, _ln, _ishow, _host, _guid, $ = function (id) { return document.getElementById(id) }, spanArr = [], ie = navigator.appVersion.indexOf("MSIE") != -1, bodyobj = document.documentElement || document.body, addscript = function (sUrl) { var _script = document.createElement('script'); _script.setAttribute('charset', 'utf-8'); _script.setAttribute('type', 'text/javascript'); _script.setAttribute('src', sUrl); document.getElementsByTagName('head')[0].appendChild(_script); };

    _odd.fn = _odd.prototype = {
        init: function (arguments) {
            //begin 初始化
            _host = arguments[0];
            _webid = arguments[1];
            _ln = arguments[2] || "zh-cn";
            _ishow = arguments[3] == undefined ? 0 : arguments[2];
            this.uid = arguments[4] || 0;
            this.emt = 0;
            this.reffer = escape(document.referrer);
            this.title = escape(document.title);
            this.cookie = this.getcookie();
            _guid = this.cookie["EzhenDDVisitorGuid"];
            //end

            this.stat(); //发送访问请求
            this.show(); //显示统计和漂浮及邀请
            return this;
        },
        getcookie: function () {
            if (document.cookie.indexOf("EzhenDDVisitorGuid=") == -1) {
                //alert("nocookie");
                var c = [], guid = "";
                //生成guid,是否会出现异常情况?
                for (var i = 1; i <= 32; i++) {
                    var n = Math.floor(Math.random() * 16.0).toString(16);
                    guid += n;
                    if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                        guid += "-";
                }
                c.push("EzhenDDVisitorGuid=", escape(guid), "; expires=", new Date((new Date()).getTime() + 315360000000).toGMTString(), ";path=/;");
                document.cookie = c.join('');
            }
            //alert(document.cookie);
            var _c = [], aCookie = document.cookie.split("; ");
            _c.push("({");
            for (var i = 0; i < aCookie.length; i++) {
                var aCrumb = aCookie[i].split("=");
                if (aCrumb[0].length > 0) {
                    if (aCrumb[0] == "MemberUser" || aCrumb[0] == "EzhenDDVisitorGuid") {
                        _c.push(aCrumb[0], ':"', aCrumb[1], '"');
                        _c.push(",")
                    }
                }
            }
            _c.pop();
            _c.push("})");
            return eval(_c.join(''));
        },
        getguid: function () {
            //alert(this.cookie["EzhenDDVisitorGuid"]);
            if (this.cookie["EzhenDDVisitorGuid"]) {
                var c = [], guid = "guid";
                c.push("EzhenDDVisitorGuid=", escape(value), "; expires=", new Date((new Date()).getTime() + 315360000000).toGMTString(), ";path=/;");
                document.cookie = c.join('');
                return guid;
            }
            else { return this.cookie["EzhenDDVisitorGuid"]; }
        },
        stat: function () {
            var isp = 0, suid = 0, _src = [];
            if ($("ProductSimpleIntro")) { isp = 1; }
            if (this.cookie["MemberUser"]) { suid = parseInt(this.cookie["MemberUser"].split("|")[2]); }
            _src.push(_host, "jscall.aspx?type=1&vguid=", _guid, "&webid=", _webid, "&emt=", this.emt, "&uid=", this.uid, "&isp=", isp, "&suid=", suid, "&title=", this.title, "&referer=", this.reffer, "&time=", new Date());

            var image = new Image();
            image.src = _src.join('');
        },
        show: function () {
            //页面加载完成才执行,显示统计等操作
            if (document.readyState === "complete") {
                return this.Ready();
            }
            if (document.addEventListener) {
                //alert("firefox");
                window.addEventListener("load", this.Ready, false);
            } else if (document.attachEvent) {
                //alert("ie");
                document.attachEvent("onreadystatechange", this.Ready);
            }
        },
        Ready: function () {//页面加载完成后执行
            //删除事件监听
            if (document.readyState === "complete") {
                if (document.addEventListener) { document.removeEventListener("Ready", this.Ready, false); } else if (document.attachEvent) { document.detachEvent("onreadystatechange"); }

                _odd.fn.showemt.call(this); //产品页面显示图标

                if (typeof CN != "undefined") { _odd.fn.showstat.call(this); } //显示访问统计
                if (_ishow) { _odd.fn.showfloat.call(this); } //显示漂浮图片

            }
        },
        showstat: function () {//显示访问统计
            if (typeof StatType == "undefined") { StatType = 1; }
            var url = _host + "jscall.aspx?type=2&f=showstattext&webid=" + _webid + "&s=" + StatType + "&ln=" + _ln;
            addscript.call(this, url);
        },
        showemt: function () {
            var spanlist = document.getElementsByTagName("span"), weblist = [];
            for (var i = 0; i < spanlist.length; i++) {
                var s = spanlist[i];
                if (s.id.substring(0, 4) === "ODD_") {
                    var spanName = s.getAttribute("name"), _parm = "&emt=1&ln=" + _ln;
                    if (spanName) {
                        var _parm = "&emt=2&pid=" + spanName + "&hu=" + document.URL + "&ln=" + _ln
                    }
                    var obj = {}; obj.span = s; obj.parm = _parm; //obj.type = s.spanType || 0; 
                    spanArr.push(obj);
                    weblist.push(s.id.substring(4));
                }
            }
            if (weblist.length > 0) {
                var url = _host + "jscall.aspx?type=3&f=setinnerhtml&webidlist=" + weblist.join(',');
                addscript.call(this, url); //setInnerHTML
            }
        },
        showfloat: function () {//显示漂浮图片
            var url = _host + "showfloat.aspx?webid=" + _webid + "&vguid=" + _guid + "&ln=" + _ln;

            addscript.call(this, _host + "JavaScripts/FloatDialogForm.js");
            addscript.call(this, url);
        },
        showstattext: function (result) {    //显示统计： 访问 - 257162, 产品 - 333, 新闻 - 42, 文章 - 87
            var span = $(CN); span.title = ""; span.innerHTML = result || "";
        },
        setinnerhtml: function (result) {    //显示图标：行业网站搜索页面、产品详细页面、联系我们页面
            var nodeList = eval("([" + result + "])"), len = nodeList.length, _text = "", _textarr;
            if (len == 0 || len != spanArr.length) { return; }
            for (var i = 0; i < spanArr.length; i++) {
                var tablestr = [], _span = spanArr[i].span, _node = nodeList[i], img;
                _textarr = _span.innerHTML || _span.title || "与我聊天;给我留言"; //todo firefox下存在空字符问题需要处理
                var _cm = _textarr.split(';'), _ourl = [];
                if (_node.OnLineState < 3) {
                    img = "online.gif"; _text = _cm[0];
                    _ourl.push(_host, "chatroom.aspx?webid=", _node.WebId, "&userid=", _node.UserId, "&vguid=", _guid, "&ao=1&emt=1&ln=", _ln);
                }
                else {
                    img = "offline.gif"; _text = _cm[1];
                    _ourl.push(_host, "messageboard.aspx?webid=", _node.WebId, "&userid=", _node.UserId, "&vguid=", _guid, "&ao=1", spanArr[i].parm);
                    //产品页面参数已经一样了
                }
               
                tablestr.push("<table style='width:20px;heigth:20px;'><tbody><tr><td>");
                tablestr.push("<img id=img_", i, " style='width:16px;' src=", _host, "Images/StyleImages/EmbedDD/", img, "></img>");
                tablestr.push("</td><td style='font-size:9pt;cursor:pointer' noWrap='true' valign ='bottom'>");
                tablestr.push("<span id=href_", i, " style='width:1%' onclick=\"openwin('", _ourl.join(''), "')\" >", _text, "</span>");
                tablestr.push("</td></tr></tbody></table>");

                _span.innerHTML = tablestr.join("");
                _span.width = "20px";
                _span.height = "20px";
                _span.title = "";
            }
        }


    }
    _odd.fn.init.prototype = _odd.fn;


    //打开聊天新窗口窗口
    var win;
    function openwin(_url) { NewWindowInCenter(_url, "", 586, 439, "no"); }
    function NewWindowInCenter(windowUrl, windowName, windowWidth, windowHeight, isScroll) {
        var windowLeft = document.documentElement.clientWidth / 2 - windowWidth / 2;
        var windowTop = document.documentElement.clientHeight / 2 - windowHeight / 2;
        var windowSetting = "left=" + windowLeft + ",top=" + windowTop + ",width=" + windowWidth + ",height=" + windowHeight + ",scrollbars=" + isScroll + ",location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
        if (win) { win = window.open(windowUrl, windowName, windowSetting); }
        else { try { win.focus(); } catch (ex) { win = window.open(windowUrl, windowName, windowSetting); } }
    }

    window.odd = _odd;
    window.showstattext = _odd.fn.showstattext;
    window.setinnerhtml = _odd.fn.setinnerhtml;
    window.openwin = openwin;
})(window);
