bizqqwpa.define("util.domain",function(){var domain={},dm=document.domain;try{domain.url=location.href}catch(e){domain.url=""}domain.topdomain=function(){var reg1=/\.(?:(?:edu|gov|com|org|net)\.cn|co\.nz)$/,reg2=/^[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d$/,slicepos=reg1.test(dm)?-3:reg2.test(dm)?0:-2;return dm.split(".").slice(slicepos).join(".")}();domain.domain=function(){var reg=/(?::[\/]{2}|:[\\]{3})([a-za-z0-9_\.]+)/;try{var ret=reg.exec(domain.url);return ret?ret[1]||dm:dm}catch(e){return dm}}();return domain});bizqqwpa.define("wpa.wpamgr","globalsettings,lang.each,util.proxy,util.cookie,util.titleflash,util.report,util.serialize,util.domain,wpa.wpa",function(require){var globalsettings=require("globalsettings"),each=require("each"),proxy=require("proxy"),titleflash=require("titleflash"),report=require("report"),serialize=require("serialize"),domain=require("domain"),cookie=require("cookie"),wpa=require("wpa");var report_possibility=40;var passrandom=function(poss){var r=poss||10;if(math.random()*100<=r){return true}return false};var reportwpa=function(url,opts){if(passrandom(report_possibility)){report(url+"?"+serialize(opts))}};var displayreport=function(params){var url="http://prom.b.qq.com/wpadisplay/r.gif?",optsarr=["wty","type","nameaccount","kfuin","ws","aty","a","title","wording","wording2"],reportopts={version:globalsettings.version};each(optsarr,function(){reportopts[this]=typeof params[this]==="undefined"?"":params[this]});report(url+serialize(reportopts))};return{newwpa:function(params){var nameaccount=params.nameaccount;if(!nameaccount){return}displayreport(params);reportwpa("http://report.b.qq.com/crmreport/accesslog",{fuid:cookie.get("pgv_pvi"),fkfuin:params.kfuin,fna:params.nameaccount,frurl:window.document.referrer});return new wpa(params)},invite:function(params,di){var mgr=this,defaultinvite=function(){titleflash.on("【您有新信息】");params.insert=function(node){var body=document.getelementsbytagname("body")[0];body.insertbefore(node,body.firstchild)};mgr.newwpa(params)};if(!di){defaultinvite();return}try{var chat=proxy({params:params},wpa.prototype.launchchat);window[di]&&window[di](chat,params["msg"])}catch(e){defaultinvite()}}}});bizqqwpa.define("wpa.visitor","lang.browser,util.log,util.speedreport,util.getjsonp,util.domain,util.pubsub,wpa.filter,wpa.ta,wpa.invite,wpa.wpamgr,wpa.ta,wpa.kfuin",function(require){var invite=require("invite"),domain=require("domain"),filter=require("filter"),getjsonp=require("getjsonp"),pubsub=require("pubsub"),log=require("log"),speedreport=require("speedreport"),ta=require("ta"),kfuincahe=require("kfuin"),browser=require("browser");var crm_block_on_serverside=1;var get_config_url="http://visitor.crm2.qq.com/cgi/visitorcgi/ajax/wpa_first_heart_beat.php";return function(config){var nameaccount=config.nameaccount;if(!nameaccount||nameaccount==="undefined"){return}if(invite.isloaded(nameaccount)||!filter.ta){return}var uid,cfg,launch=function(uid,cfg){if(!uid||!cfg){return}if(cfg.block===crm_block_on_serverside){return}if(browser&&browser.ua){var spiderreg=/spider|bot|^\s*$/;if(spiderreg.test(browser.ua)){return}}if(filter.crm){cfg.di=config.di;cfg.kfuin=config.kfuin;log(nameaccount+" try launch slave");invite.load(nameaccount,uid,cfg)}};pubsub.one("ta.loaded",function(data){uid=data;launch(uid,cfg)});pubsub.one("invite.first",function(data){cfg=data;launch(uid,cfg)});ta(nameaccount,domain.topdomain,function(uid){pubsub.pub("ta.loaded",uid)});var opts={nameaccount:nameaccount,dm:domain.topdomain,title:document.title,url:location.href.split("://")[1].split("?")[0]};getjsonp(get_config_url,opts,function(cfg){pubsub.pub("invite.first",cfg)});config.kfuin&&kfuincahe.set(nameaccount,config.kfuin)}});bizqqwpa.define("wpa.kfuin","util.getjsonp",function(require){var getjsonp=require("getjsonp");var get_kfuin_url="http://wpl.b.qq.com/cgi/conv.php";var kfuins={};return{get:function(nameaccount,callback){callback=callback||function(){};if(!nameaccount||kfuins[nameaccount]){callback(kfuins[nameaccount]);return}var opts={num:nameaccount};getjsonp(get_kfuin_url,opts,function(rs){if(!rs||rs.r!==0){callback();return}var kfuin=kfuins[nameaccount]=rs.data.kfuin;callback(kfuin)})},set:function(nameaccount,kfuin){kfuins[nameaccount]=kfuin}}});bizqqwpa.define("util.proxy",function(){return function(ns,fn){return function(){return fn.apply(ns,arguments)}}});bizqqwpa.define("util.titleflash","util.taskmgr",function(require){var title_flash_gap=120;var taskmgr=require("taskmgr");var doc=document,title=doc.title,task=taskmgr.newtask(function(){var t=doc.title;doc.title=t.substr(1,t.length)+t.substr(0,1)},title_flash_gap);return titleflash={on:function(msg){doc.title=msg+(""+doc.title);task.run()},off:function(){task.pause();doc.title=title}}});bizqqwpa.define("util.cookie",function(){var doc=document;return{set:function(name,value,domain,path,expires){if(expires){expires=new date(+new date+expires)}var tempcookie=name+"="+escape(value)+(expires?"; expires="+expires.togmtstring():"")+(path?"; path="+path:"")+(domain?"; domain="+domain:"");if(tempcookie.length<4096){doc.cookie=tempcookie}},get:function(name){var carr=doc.cookie.match(new regexp("(^| )"+name+"=([^;]*)(;|$)"));if(carr!=null){return unescape(carr[2])}return null},del:function(name,domain,path){if(this.get(name)){doc.cookie=name+"="+(path?"; path="+path:"")+(domain?"; domain="+domain:"")+";expires=thu, 01-jan-1970 00:00:01 gmt"}},find:function(pattern){return doc.cookie.match(pattern).split(",")}}});bizqqwpa.define("wpa.wpa","globalsettings,lang.browser,util.cookie,lang.typeenhance,util.serialize,util.proxy,util.pad,util.bits,util.getscript,util.getjsonp,util.domain,util.events,util.onload,util.offset,util.report,util.log,util.speedreport,wpa.selectpanel,util.oniframeloaded,util.guid,wpa.getqqversion,wpa.viewhelper,wpa.views,wpa.ta,wpa.kfuin,wpa.sid",function(require){var globalsettings=require("globalsettings"),proxy=require("proxy"),typeenhance=require("typeenhance"),pad=require("pad"),bits=require("bits"),getscript=require("getscript"),getjsonp=require("getjsonp"),domain=require("domain"),events=require("events"),onload=require("onload"),serialize=require("serialize"),browser=require("browser"),offset=require("offset"),report=require("report"),log=require("log"),speedreport=require("speedreport"),guid=require("guid"),selectpanel=require("selectpanel"),oniframeloaded=require("oniframeloaded"),getqqversion=require("getqqversion"),viewhelper=require("viewhelper"),views=require("views"),ta=require("ta"),kfuincache=require("kfuin"),cookie=require("cookie"),sidcache=require("sid");var global=window;var wpa_type_ta_invite_only="0",wpa_type_normal="1",wpa_type_link="2",wpa_type_custom="3",session_version_ta="4",wpa_style_type_invite="20",appointed_type_auto="0",appointed_type_kfext="1",appointed_type_group="2",wpa_float_type_fixed="0",wpa_float_type_scroll="1",wpa_float_position_x_left="0",wpa_float_position_x_center="1",wpa_float_position_x_right="2",wpa_float_position_y_top="0",wpa_float_position_y_center="1",wpa_float_position_y_bottom="2",is_invite_wpa_false="0",is_invite_wpa_true="1",chat_type_aio=1,chat_type_anonymous=2;var speed_report_display=.1,report_possibility=40;var passrandom=function(poss){var r=poss||10;if(math.random()*100<=r){return true}return false};var reportwpa=function(url,opts){if(passrandom(report_possibility)){report(url+"?"+serialize(opts))}};var wpa=function(params){this.params=params;this.insert=params.insert;this.wty=params.wty;var self=this,nameaccount=this.nameaccount=params.nameaccount,kfuin=this.kfuin=params.kfuin;switch(this.wty){case wpa_type_normal:this.render();break;case wpa_type_custom:this.custom()}!this.kfuin&&kfuincache.get(nameaccount,function(data){data&&(self.kfuin=data)});!this.sid&&sidcache.get(nameaccount,function(data){data&&(self.sid=data)});report("http://prom.b.qq.com/se/r.gif?na="+nameaccount+"&ref="+encodeuricomponent(document.referrer))};wpa.prototype={render:function(){var params=this.params;var width,height,type=parseint(params["type"]),typestr,isfloat=false;switch(type){case 1:typestr="a01";width=92;height=22;break;case 2:typestr="a02";width=77;height=22;break;case 10:typestr="b01";width=93;height=151;isfloat=true;break;case 11:typestr="b02";width=327;height=172;isfloat=true;break;case 12:typestr="b03";width=121;height=277;isfloat=true;break;case 20:typestr="i01";width=327;height=172;isfloat=true;break;case 30:typestr="d01";width=params["txw"];height=params["txh"];isfloat=true;break}this.type=typestr;this.width=width;this.height=height;this.createwpa();if(type>9&&type<14||type===20||type===30){this.initfloatwpa()}},createwpa:function(){var speedrpt=speedreport("7818","21","1");var wpa=this,type=this.type,width=this.width,height=this.height,view=views[type];var iframe;try{iframe=document.createelement('')}catch(e){iframe=document.createelement("iframe");iframe.width=width;iframe.height=height;iframe.setattribute("scrolling","no");iframe.setattribute("frameborder",0);iframe.setattribute("allowtransparency",true);iframe.setattribute("src","about:blank")}this.node=iframe;this.insert(iframe);if(browser.msie){try{var accesstest=iframe.contentwindow.document}catch(e){iframe.src="javascript:void((function(){document.open();document.domain='"+document.domain+"';document.close()})())"}}var loaded=function(){var iwin=iframe.contentwindow,idoc=iframe.contentdocument||iwin.document;idoc.open();idoc.write(['','',"
",'',browser.msie&&iframe.src!=="about:blank"?"":"","","",view.templ,"",""].join(""));idoc.close();var helper=new viewhelper(idoc,wpa);view.init(idoc,helper);var point=1;speedrpt.addpoint(point).send(speed_report_display)};oniframeloaded(iframe,loaded)},initfloatwpa:function(){var doc=window.document,params=this.params,type=parseint(params["type"]),node=this.node,width=this.width,height=this.height;node.style.csstext=["display:none;","position:absolute;",type===20?"z-index:2147483647!important;":"z-index:2147483646!important;"].join(" ");onload(initiframeload);function initiframeload(){var isty=node.style,isie=browser.msie,ver=parseint(browser.version,10),isquirk=doc.compatmode==="backcompat";isty.position=parseint(params["fsty"],10)==0?"fixed":"absolute";if(parseint(params["fposx"])==0){isty.left=8+"px";isty.right="auto";isty.marginleft=0}else if(parseint(params["fposx"])==1){isty.left="50%";isty.right="auto";isty.marginleft="-"+parseint(width/2)+"px"}else{isty.left="auto";isty.right=8+"px";isty.marginleft=0}if(parseint(params["fposy"])==0){isty.top=8+"px";isty.bottom="auto";isty.margintop=0}else if(parseint(params["fposy"])==1){isty.top="50%";isty.bottom="auto";isty.margintop="-"+parseint(height/2)+"px"}else{isty.top="auto";isty.bottom=8+"px";isty.margintop=0}if(isie&ver<7||isquirk){isty.position="absolute";if(parseint(params["fsty"])==0){isty.margintop=0;var itop;if(parseint(params["fposy"])==0){itop=8}else if(parseint(params["fposy"])==1){itop=(offset.getclientheight(doc)-height)/2}else{itop=offset.getclientheight(doc)-height-8}setinterval(function(){isty.top=offset.getscrolltop(doc)+itop+"px"},128)}}isty.display="block"}},custom:function(){events.addevent(this.params.node,"click",proxy(this,this.launchchat))},remove:function(){var node=this.node;node.parentnode.removechild(node)},launchchat:function(callback){var wpa=this,sptreport=speedreport("7818","21","2"),chattype=this.params.chattype;reportwpa("http://report.b.qq.com/crmreport/clicklog",{fuid:cookie.get("pgv_pvi"),fkfuin:wpa.params.kfuin,fna:wpa.params.nameaccount,frurl:global.document.referrer});if(browser.isios||browser.isandroid){this.launchmobilechat();return}if(chattype==chat_type_anonymous){wpa.launchanonymouschat(callback);return}if(chattype==chat_type_aio){wpa.launchaiochat(callback);return}!browser.msie&&wpa.launchaiochat(callback);getqqversion(function(version){sptreport.addpoint(7).send();if(version){browser.msie&&wpa.launchaiochat(callback);return}new selectpanel({onaiochat:function(){wpa.launchaiochat(callback)},onanonychat:function(){wpa.launchanonymouschat(callback)}})})},launchmobilechat:function(){var nameaccount=this.params.nameaccount;kfuincache.get(nameaccount,function(kfuin){if(navigator.useragent.indexof("micromessenger")>-1&&domain.domain.indexof("qq.com")===-1){location.href="http://wpd.b.qq.com/page/info.php?nameaccount="+nameaccount;return}launch(schema(),fallback);function schema(){var dm=domain.domain,type="crm";if(browser.isqq&&browser.qqversion==="4.5"){type="wpa"}return browser.isandroid&&browser.chrome&&parseint(browser.version,10)>25?"intent://im/chat?chat_type="+type+"&uin="+kfuin+"&version=1&src_type=web&web_src="+location.protocol+"://"+dm+"#intent;scheme=mqqwpa;action=android.intent.action.view;end":"mqqwpa://im/chat?chat_type="+type+"&uin="+kfuin+"&version=1&src_type=web&web_src="+location.protocol+"://"+dm}function launch(schema,fail){var start=+new date;location.href=schema;settimeout(function(){var gap=+new date-start;if(gap<1e3){fail&&fail()}},800)}function fallback(){location.href="http://wpd.b.qq.com/page/info.php?nameaccount="+nameaccount}})},launchaiochat:function(){var iframe=document.createelement("iframe"),body=document.getelementsbytagname("body")[0];iframe.style.display="none";body.insertbefore(iframe,body.firstchild);return function(callback){var params=this.params,kfuin=this.kfuin,opts={na:params.nameaccount,kfuin:kfuin,aty:params.aty,a:params.a,sid:this.sid,uid:ta.uid,url:domain.url,title:document.title,dm:domain.topdomain,clksrc:params.clksrc||"",ext:params.ext||""},guid=guid();var sptreport=speedreport("7818","21","2");getjsonp("http://wpd.b.qq.com/cgi/get_sign.php",opts,function(rs){if(!rs||rs.r!==0||!rs.data){return}iframe.src=rs.data.sign;var isloaded=false,loaded=function(){if(isloaded){return}isloaded=true;var clickid=rs.data.clkid;sptreport.addpoint(5).send();report("http://promreport.crm2.qq.com/wpaclick/r.gif?ty=1&kfuin="+kfuin+"&version="+globalsettings.version+"&browser="+encodeuricomponent(navigator.useragent)+"&bfrom=1&appointtype="+params.aty+"&appoint="+params.a+"&clkid="+clickid+"&guid="+guid);global.taclick&&global.taclick(clickid,"clickid");typeenhance.isfunction(callback)&&settimeout(function(){callback(params)},1e3)};oniframeloaded(iframe,loaded);if(browser.msie){var fallback=function(){settimeout(function(){if(isloaded){return}/interactive/.test(iframe.readystate)?loaded():fallback()},500)};fallback()}});report("http://promreport.crm2.qq.com/wpaclickorg/r.gif?kfuin="+kfuin+"&version="+globalsettings.version+"&browser="+encodeuricomponent(navigator.useragent)+"&bfrom=1&appointtype="+params.aty+"&appoint="+params.a+"&guid="+guid)}}(),launchanonymouschat:function(callback){var params=this.params,sptreport=speedreport("7818","21","2"),url="http://wpd.b.qq.com/page/webchat.html?nameaccount="+this.nameaccount,opener=window.open(url,"_blank","height=516, width=598,toolbar=no,scrollbars=no,menubar=no,status=no,location=no");typeenhance.isfunction(callback)&&callback(params);report("http://promreport.crm2.qq.com/wpaclick/r.gif?ty=2&kfuin="+this.kfuin+"&version="+globalsettings.version+"&browser="+encodeuricomponent(navigator.useragent)+"&bfrom=1&appointtype="+params.aty+"&appoint="+params.a);opener.onload=function(){sptreport.addpoint(6).send()}}};return wpa});bizqqwpa.define("util.getjsonp","util.getscript,util.serialize",function(require){var getscript=require("getscript"),serialize=require("serialize");var count=0;return function(url,options,callback){var fnname="jsonp_callback_"+ ++count+"_"+math.round(math.random()*100),script;options.cb=fnname;url+=url.indexof("?")===-1?"?":"&";url+=serialize(options);script=getscript(url);window[fnname]=function(json){callback(json);settimeout(function(){window[fnname]=null;script.parentnode.removechild(script)},1)}}});bizqqwpa.define("wpa.filter","util.domain",function(require){var ta_blacklist="",crm_blacklist="qq.com,pengyou.com,qzoneapp.com,nipic.com,docin.com,51zxw.net,2155.com,xd.com,yto.net.cn,c-c.com,27.cn,05wan.com,alivv.cn,gogo.com,doctorjob.com.cn,emoney.cn,m4.cn,chinaktv.net,yk988.com,bangkaow.com,wsxsp.com,55tools.com,youxi518.com",crm_whitelist="b.qq.com,sales.b.qq.com,guilin.house.qq.com,ta.qq.com,hn.qq.com,nantong.house.qq.com";var domain=require("domain");return{ta:function(){var dm=domain.topdomain,ipreg=/^[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d$/,localreg=/^localhost$/,previewpagereg=/^wpa\.b\.qq\.com/;return ta_blacklist.indexof(dm)===-1&&!ipreg.test(dm)&&!localreg.test(dm)&&!previewpagereg.test(domain.domain)}(),crm:function(){try{var reg=new regexp("(^|,)"+domain.domain);if(reg.test(crm_whitelist)){return true}var dm=domain.topdomain,dmreg=new regexp("(^|,)"+dm),ipreg=/^[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d\.[12]?\d?\d$/,localreg=/^localhost$/;return!dmreg.test(crm_blacklist)&&!ipreg.test(dm)&&!localreg.test(dm)}catch(e){}}()}});bizqqwpa.define("wpa.ta","util.getscript,util.serialize,util.cookie",function(require){var url="http://tajs.qq.com/crmqq.php",uid_cookie_name="pgv_pvi";var getscript=require("getscript"),serialize=require("serialize"),cookie=require("cookie");var loaded=false;var ta=function(nameaccount,domain,callback){var istriggered=false;if(ta.uid){callback(ta.uid);istriggered=true}if(!loaded){var options={uid:nameaccount,dm:domain},url=url+"?"+serialize(options,"=","&");getscript(url,function(){loaded=true;if(istriggered){return}ta.uid=cookie.get(uid_cookie_name);if(ta.uid){callback(ta.uid)}else{settimeout(arguments.callee,30)}})}};ta.uid=cookie.get(uid_cookie_name)||"";return ta});bizqqwpa.define("wpa.invite","util.log,util.getjsonp,util.proxy,util.domain,util.blockstorage,util.taskmgr,wpa.wpamgr",function(require){var master_monitor_gap=2e3,invite_monitor_gap=1e3,master_heatbeat_gap=2e3,slave_heartbeat_gap=2e3,server_monitor_gap_min=5e3,server_monitor_gap_max=15e3,server_monitor_sleepcheck_gap=36e5,server_monitor_sleeping_gap=1e3;var invite_signal="is",invite_kfext="ik",invite_msg="msg",master_heartbeats="mh",master_id="mid",slave_ids="slid";var invite_signal_uninvited="0",invite_signal_invite="1",invite_signal_invited="2",invite_kfext_auto="0",master_heartbeats_error="-1",data_seperator="|";var heartbeat_url="http://visitor.crm2.qq.com/cgi/visitorcgi/ajax/wpa_heart_beat.php",confirm_auto_invite_url="http://visitor.crm2.qq.com/cgi/visitorcgi/ajax/auto_invite.php";var result_success=0,invite_state_uninvited="0",invite_state_invite="1",invite_state_invited="2",auto_invite_true=1;var wpa_type_ta_invite_only="0",wpa_type_normal="1",wpa_type_link="2",session_version_ta="4",wpa_style_type_invite="20",appointed_type_auto="0",appointed_type_kfext="1",appointed_type_group="2",appointed_type_auto_invite="4",appointed_type_invite="5";var wpa_float_type_fixed="0",wpa_float_position_y_center="1",wpa_float_position_x_center="1",is_invite_wpa_false="0",is_invite_wpa_true="1";var log=require("log"),getjsonp=require("getjsonp"),proxy=require("proxy"),domain=require("domain"),blockstorage=require("blockstorage"),taskmgr=require("taskmgr"),wpamgr=require("wpamgr");var slave=function(nameaccount,uid,cfg){this.nameaccount=nameaccount;this.uid=uid;this.config=cfg;this.genid();this.storage=blockstorage(nameaccount);this.monitors={master:taskmgr.newtask(proxy(this,this.mastermonitor),master_monitor_gap).run(),invite:taskmgr.newtask(proxy(this,this.invitemonitor),invite_monitor_gap).run()};this.heartbeat=taskmgr.newtask(proxy(this,this.heartbeatprocess),slave_heartbeat_gap).run();this.setactive();window.onfocus=proxy(this,this.setactive);log("slave "+this.id+" launched!")};slave.prototype={genid:function(){this.id="slid_"+ +new date%1e3+"_"+math.round(math.random()*100)},mastermonitor:function(){if(masters[this.nameaccount]){return}log("monitoring mater state");var lastmasterheartbeat=this.storage.get(master_heartbeats)||0,gap=+new date-parseint(lastmasterheartbeat);log("gap of master is "+gap);if(gap>3*master_heatbeat_gap){this.recovermaster()}},recovermaster:function(){masters[this.nameaccount]=new master(this.nameaccount,this.uid,this.config);log("recover master by slave "+this.id)},invitemonitor:function(){if(this.isinvited()){this.kill()}else if(this.isinviting()){if(this.isactive()){this.invite()}}log("slave "+this.id+" monitoring invite state")},kill:function(){this.monitors.invite.drop();this.heartbeat.drop();var storage=this.storage,keys=[this.id];for(var i=0,key;key=keys[i++];){storage.del(key)}log("slave "+this.id+" killed")},invite:function(){var kfext=this.storage.get(invite_kfext);var params={wty:wpa_type_normal,nameaccount:this.nameaccount,kfuin:this.config.kfuin,type:wpa_style_type_invite,aty:kfext?kfext===invite_kfext_auto?appointed_type_auto_invite:appointed_type_invite:appointed_type_auto_invite,a:kfext||"",iv:is_invite_wpa_true,fsty:wpa_float_type_fixed,fposx:wpa_float_position_x_center,fposy:wpa_float_position_y_center,sv:session_version_ta,uid:this.uid,dm:domain.topdomain,msg:this.storage.get(invite_msg)};wpamgr.invite(params,this.config.di);this.storage.set(invite_signal,invite_signal_invited);log("invited by slave "+this.id)},heartbeatprocess:function(){var storage=this.storage,ids=storage.get(slave_ids);if(!ids){storage.set(slave_ids,this.id+"|")}else if(ids.indexof(this.id+"|")===-1){storage.set(slave_ids,this.id+"|"+ids)}storage.set(this.id,+new date)},setactive:function(){var storage=this.storage,ids=storage.get(slave_ids)||"",sign=this.id+data_seperator;if(ids.indexof(this.id)>-1){ids=ids.replace(sign,"")}ids+=sign;storage.set(slave_ids,ids)},isactive:function(){var slaves=this.storage.get(slave_ids);if(!slaves){return false}return slaves.substr(0,slaves.length-1).split(data_seperator).pop()===this.id},isinvited:function(){return this.storage.get(invite_signal)===invite_signal_invited},isinviting:function(){return this.storage.get(invite_signal)===invite_signal_invite}};var masters={};var master=function(nameaccount,uid,cfg){this.nameaccount=nameaccount;this.uid=uid;this.config=cfg;this.storage=blockstorage(nameaccount);this.genid();this.sleep=false;this.heartbeaturl=cfg.hbdomain||heartbeat_url;this.storage.set(master_id,this.id);this.heartbeatprocess();this.heartbeat=taskmgr.newtask(proxy(this,this.heartbeatprocess),master_heatbeat_gap).run();this.initwithconfig();log("master launched!")};master.prototype={genid:function(){this.id=+new date%1e3+"_"+math.round(math.random()*100)},setinvitestate:function(signal,kfext,msg){if(signal===invite_signal_invite){this.storage.set(invite_kfext,kfext);this.storage.set(invite_msg,msg)}this.storage.set(invite_signal,signal)},isinvited:function(){var invited=this.storage.get(invite_signal)===invite_signal_invited;if(invited){this.recycle();this.isinvited=function(){return true}}return invited},initwithconfig:function(){var cfg=this.config;if(cfg.r!==result_success){this.storage.set(master_heartbeats,master_heartbeats_error);return}if(cfg.isauto===auto_invite_true){this.storage.set(invite_msg,cfg.automsg);this.autoinvitetimer=settimeout(proxy(this,function(){this.autoinvite()}),cfg.autotime*1e3)}this.monitors={slave:taskmgr.newtask(proxy(this,this.slavemonitor),slave_heartbeat_gap).run(),server:taskmgr.newtask(proxy(this,this.servermonitor),server_monitor_gap_min).run(),sleep:taskmgr.newtask(proxy(this,this.sleepmonitor),server_monitor_sleepcheck_gap).run()};log("master inited with config")},autoinvite:function(){if(this.isinvited()){return}var opt={nameaccount:this.nameaccount,uid:this.uid};var servermonitor=this.monitors.server;servermonitor.pause();getjsonp(confirm_auto_invite_url,opt,proxy(this,function(rs){if(rs.r!==result_success){servermonitor.run();return}if(!this.isinvited()){this.setinvitestate(invite_signal_invite,invite_kfext_auto,this.storage.get(invite_msg));taskmgr.once(function(){servermonitor.run()},5e3).run()}}))},ajustservermonitorgap:function(time){this.monitors.server.setgap(math.min(math.max(server_monitor_gap_min,time),server_monitor_gap_max))},servermonitor:function(){var invitesignal=this.storage.get(invite_signal);if(this.sleep){return}var opt={nameaccount:this.nameaccount,uid:this.uid};if(invitesignal===invite_signal_invite){opt["invitestate"]=invite_state_invite}if(invitesignal===invite_signal_invited){opt["invitestate"]=invite_state_invited}getjsonp(this.heartbeaturl,opt,proxy(this,function(rs){if(rs.r!==result_success){return}if(rs.gap){this.ajustservermonitorgap(rs.gap*1e3)}if(rs.invitestate===invite_state_uninvited){return}if(rs.invitestate===invite_state_invite){this.setinvitestate(invite_signal_invite,rs.kfext,rs.invitemsg);return}if(rs.invitestate===invite_state_invited){this.setinvitestate(invite_signal_invited)}}))},slavemonitor:function(){if(this.isinvited()){this.monitors.slave.drop()}var storage=this.storage,slaves=storage.get(slave_ids);if(!slaves){return}slaves=slaves.split(data_seperator);var aliveslaves="",time=+new date,lastslaveheartbeat,slave,gap;for(var i=0;slave=slaves[i++];){log("monitoring slave "+slave+" state");lastslaveheartbeat=storage.get(slave)||0;gap=time-parseint(lastslaveheartbeat);log("gap of slave "+slave+" is "+gap);if(gap>3*slave_heartbeat_gap){storage.del(slave);log("clear slave "+slave+" in storage")}else{aliveslaves+=slave+data_seperator}}storage.set(slave_ids,aliveslaves)},sleepmonitor:function(){var slaves=this.storage.get(slave_ids)||"",activeslave=slaves.substr(0,slaves.length-1).split(data_seperator).pop();if(this.sleep){if(this.activeslave!==activeslave){this.activeslave=activeslave;this.sleep=false;this.monitors.sleep.setgap(server_monitor_sleepcheck_gap)}}else{if(this.activeslave===activeslave){this.sleep=true;this.monitors.sleep.setgap(server_monitor_sleeping_gap)}else{this.activeslave=activeslave}}},kill:function(){masters[this.nameaccount]=undefined;if(this.monitors){this.monitors.server.drop();this.monitors.slave.drop();this.heartbeat.drop();cleartimeout(this.autoinvitetimer)}log("master killed")},recycle:function(){var storage=this.storage,keys=[invite_kfext,invite_msg];for(var i=0,key;key=keys[i++];){storage.del(key)}log("storage recycled")},heartbeatprocess:function(){var storage=this.storage;if(storage.get(master_id)!==this.id){this.kill();return false}this.storage.set(master_heartbeats,+new date)}};var slaves={};return{load:function(nameaccount,uid,cfg){if(this.isloaded(nameaccount)){log(nameaccount+" slave already running");return}var slave=new slave(nameaccount,uid,cfg);slaves[nameaccount]?slaves[nameaccount].push(slave):slaves[nameaccount]=[slave]},isloaded:function(nameaccount){return typeof slaves[nameaccount]!=="undefined"}}});bizqqwpa.define("util.taskmgr","util.proxy",function(require){var task_run="run",task_pause="pause",task_drop="drop",loop_time=50;var proxy=require("proxy");var tm=function(){this.circle=[];this.pos=0;setinterval(proxy(this,this.loop),16)};tm.prototype={newtask:function(fn,gap){var t=new task(fn,gap);this.circle.push(t);return t},once:function(fn,gap){return this.newtask(function(){fn.apply(this);this.drop()},gap)},loop:function(){var c=this.circle,pos=this.pos,count=c.length,start=+new date,looptime=loop_time,t=c[pos];while(count>0&&+new date-start