(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[3];
			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(); //显示统计和漂浮及邀请
			ztInquery();//zt询价事件绑定
			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 = []
				, spanId = ''
				//标记是否处理过
				, oddSpanMarkDone = '';
			for (var i = 0; i < spanlist.length; i++) {
				var s = spanlist[i];
				spanId = s.id || '';
				if (spanId.substring(0, 4) === "ODD_") {
					//排除已处理过的数据
					oddSpanMarkDone = s.getAttribute('data-done');
					//不存在该属性
					if (oddSpanMarkDone != '1') {
						//标记处理过
						s.setAttribute('data-done', '1');
						//并且不包含子元素
						var spanName = s.getAttribute("name")
							, _parm = ""
							, productname = s.getAttribute("data-pname");
						if (spanName) {
							_parm += "$_$pid=" + spanName ;
						}
						if (productname) {
							_parm += "$_$emt=1$_$pname=" + encodeURI(productname);
						}
						s.setAttribute('data-parm', _parm);
						var obj = {};
						obj.span = s;
						obj.parm = _parm;
						//obj.type = s.spanType || 0;
						spanArr.push(obj);
						weblist.push(spanId.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;
			var url = _host + "webdd.aspx?webid=" + _webid + "&vguid=" + _guid + "&ln=" + _ln + "&url=" + document.location.href;
			addscript.call(this, _host + "JavaScripts/FloatDialogForm.js");
			addscript.call(this, url);
		},
		showstattext: function (result) {    //显示统计: 访问 - 257162, 产品 - 333, 新闻 - 42, 文章 - 87
			try {
				var span = $(CN); span.title = ""; span.innerHTML = result || "";
			} catch (e) {

			}
		},
		setinnerhtml: function (result) {
			/*
			setinnerhtml('{"IsEnable":"0","OnLineState":"4","UserId":"314164","WebId":"251247"}');
			result 参数案例:
				sEnable	OnLineState	UserId	WebId
				1		1			598630	518600
			*/
			//显示图标:行业网站搜索页面、产品详细页面、联系我们页面
			var nodeList = eval("([" + result + "])"), len = nodeList.length, _text = "", _textarr;
			if (len == 0) { return; }

			var WebIdList = nodeList[0].WebId.split(',');
			var UserIdList = nodeList[0].UserId.split(',');
			var OnLineStateList = nodeList[0].OnLineState.split(',');

			var _span,_spanId;
			var spanlist2 = document.getElementsByTagName("span")
			//if (WebIdList.length != slength) {
			//	console.log(WebIdList.lengt+' != ' + slength);
			//	return;
			//}
			var i = -1;
			for (var j = 0; j < spanlist2.length; j++) {
				_span = spanlist2[j]
				_spanId = _span.id || '';
				//筛选叮当的dom
				if (_spanId.substring(0, 4) != "ODD_") {
					continue;
				}
				//要求处理过的dom
				if (_span.getAttribute('data-done') != '1') {
					continue;
				}
				i = -1;
				//获取下标
				for (var n = 0; n < WebIdList.length; n++) {
					if (WebIdList[n] == _spanId.substring(4)) {
						i = n;
						break;
					}
				}
				//i = WebIdList.indexOf(_spanId.substring(4));  IE8不支持
				if (i == -1) {
					//返回结果中不存在该webId
					continue;
				}
				var tablestr = [],
					img='',
					dataParm = '';
				_textarr = _span.innerHTML || _span.title || "与我聊天;给我留言"; //todo firefox下存在空字符问题需要处理
				if (_textarr && _textarr.indexOf('table') != -1) {
					_textarr = "与我聊天;给我留言";
				}
				var _cm = _textarr.split(';'), _ourl = [];
				if (OnLineStateList[i] < 3) {
					//在线 聊天室
					img = "online.gif"; _text = _cm[0];
					//_ourl.push(_host, "WebddChatRoom.aspx?webid=", WebIdList[i], "&userid=", UserIdList[i], "&vguid=", _guid, "&ao=1&emt=1&ln=", _ln, "&url=",document.location.href);
					_ourl.push(Odd_getRedirectUrl(WebIdList[i], UserIdList[i], -1, '$_$ao=1$_$emt=1'));
				}
				else {
					//产品页面参数已经一样了
					img = "offline.gif"; _text = _cm[1];
					dataParm = _span.getAttribute('data-parm');
					//_ourl.push(_host, "WebddMessageBoard.aspx?webid=", WebIdList[i], "&userid=", UserIdList[i], "&vguid=", _guid, "&ao=1", dataParm);
					_ourl.push(Odd_getRedirectUrl(WebIdList[i], UserIdList[i], -2, ('$_$ao=1' + dataParm)));
				}
				tablestr.push("<table style='width:20px;heigth:20px;' cellpadding=0 cellspacing=0><tbody><tr><td style='vertical-align:middle'>");
				tablestr.push("<img id=img_", i, " style='width:16px;display:block' src=", _host, "Images/StyleImages/EmbedDD/", img, "></img>");
				tablestr.push("</td><td style='font-size:9pt;cursor:pointer;' noWrap='true'>");
				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, "", 632, 528, "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); } }
	}
	//302调整统计点击
	function Odd_getRedirectUrl(webId, id, t,parm) {
		//id:联系人ID 或UserCenter中的叮当ID
		//t.typeId 小于零代表叮当
		//v:联系人的value值
		var _rediurl = new Array();
		_rediurl.push(_host);
		_rediurl.push('Controls/WebddContact.ashx');
		_rediurl.push('?i=' + webId);
		_rediurl.push('&t='+t);
		_rediurl.push('&id='+id);
		_rediurl.push('&l=' +  _ln);
		_rediurl.push('&vg=' +  _guid);
		_rediurl.push('&h=' + document.location.href);
		_rediurl.push('&parm=' + parm);
		return _rediurl.join('');
	}
	//lpd 20150615
	function ztInquery() {
		//zt模板询价功能
		if (jQuery) {
			jQuery('.mod-box-onlineAs').unbind().click(function () {
				var urlstr4 = Odd_getRedirectUrl(_webid, 0, -3, '');
				if (jQuery.DialogV2015Site){
					jQuery.DialogV2015Site({
						width: 630,
						height: 525,
						btnClose: true,
						title: '',
						footerBtn: '',
						type: 'html',
						content: '<iframe id=content_iframe marginWidth=0 marginHeight=0 src="' + urlstr4 + '" frameBorder=0 width=610 height=525></iframe>',
						callback: function (data, selfwarp) {
							//var $selfwarp = $(selfwarp);
						}
					});
				}
			});
		}
	}
	window.odd = _odd;
	window.showstattext = _odd.fn.showstattext;
	window.setinnerhtml = _odd.fn.setinnerhtml;
	window.openwin = openwin;
})(window);