(function(){
	var companyId=23498;
	var groupId = null;
	var  getCookie = function(name){
		var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
		if(arr=document.cookie.match(reg)) return unescape(arr[2]);
		else return null;
	};
    var setCookie = function(name, value, t) {
        t || ( t = 24 * 60 *48 * 30 );
        t = t *( 60 * 1000);
        var exp = new Date();
        exp.setTime(exp.getTime() + t);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    };	
    var attachE = function(t, e, f) {
        if (t.addEventListener) t.addEventListener(e, f, false);
        else if (t.attachEvent) t.attachEvent('on' + e, f);
        else
            t['on'+e]=f;
    };
    var genID=function(){
    	var rs = '';
		var dic=['a','b','c','d','0','1','2','3','4','5','6','7','8','9']
    	for(var i=0;i<32;i++){
    		rs += dic[(Math.floor(Math.random()*10000))%14];
    	}
    	return rs
    };  
	var sameSite = function() {
        var reg = /^(https?:\/\/[^\/]*)\/.*$/i;
        return function(loc) {
            if (loc == null) return false;
            reg.exec(loc);
            var s1 = RegExp.$1;
            reg.exec(document.location.href);
            var s2 = RegExp.$1;
            return s1 == s2;
        }
    }();    	  
	function shortURL(l){ if(l==null)return '';l=encodeURIComponent(l);if(l.length > 500) l=l.substring(0,500);return l;}
	
	var dvi='DOYOO_VISITOR_ID',dui='DOYOO_USER_ID',dvr='DOYOO_VISITOR_REFER',dvc='DOYOO_VISIT_COUNTER',
		uId=getCookie(dui),vId=getCookie(dvi),refer=getCookie(dvr)||document.referrer,loc=document.location.href,
		param= (groupId?('g='+groupId):''),vc = getCookie(dvc)||0;
		url= "http://chat.doyoo.net/WebModule/chat/p.do?c="+companyId +'&'+ param,
		bound = false;
	
	var upvid=function(){
		setCookie(dvi,vId,2);
		if(refer) setCookie(dvr,refer,2);
		setCookie(dvc,vc,2);
	}		
	var bindEnv=function(){
		if( !vId ){
			 vId = genID();
			 setCookie(dvi,vId,2);
			 vc = parseInt(vc);
			 vc ++;
			 setCookie(dvc,vc,2);
		}
		if( !uId){
			uId = vId;
			setCookie(dui,uId);
		}
		if( refer && !sameSite(refer)){
			setCookie(dvr,refer,2);
		}else{
			refer = null;
			setCookie(dvr,refer,-2);
		}
		bound = true;
		setInterval(upvid,50*1000);		
		
	};	
	window.openLooyuChat=function(t){
		if(typeof doyoo != 'undefined' && doyoo){
			return doyoo.util.openChat(t||param);
		}else{
			if(!bound) bindEnv();
			var ourl = url+'&u='+uId+'&v='+vId;
			if( refer) ourl +='&refer='+shortURL(refer);
			if( loc ) ourl +='&loc='+shortURL(loc);
			if(typeof reseveKey != 'undefined' && reseveKey)
				ourl +='&r='+encodeURIComponent(reseveKey);
			ourl+='&ct='+vc;
			ourl+='&'+ (t||param);
			var winParam = "height=460,width=690,directories=no," + "location=no,menubar=no," + "resizeable=no,status=no,toolbar=no,top=100,left=200";			
		    window.open(ourl,"", winParam);			
		}
	}
	
})();
