/* by a-sansara - https://github.com/pluie-org/bt */var $bt={TRACE:true&&typeof console!="undefined",LS_TABS:'bt.tabs',LS_CURTAB:'bt.ctab',LS_CMD:'bt.cmd',CMD_SYNC:'bt.sync',CMD_APPEND:'dom.append',CMD_HTML:'dom.html',CMD_RELOAD:'bt.reload',init:function(fn){this._init(fn);},on:function(cmd){this.log(cmd);},log:function(data){if(this.TRACE)console.log(data);},send:function(cmd){cmd.uid=this.id+Math.random();cmd.from=this.id;if(typeof cmd.to=="undefined")cmd.to="*";$l.set(this.LS_CMD,$j.str(cmd));$l.rem(this.LS_CMD);},append:function(selector,data,btid){this._dom(this.CMD_APPEND,selector,data,btid);},html:function(selector,data,btid){this._dom(this.CMD_HTML,selector,data,btid);},sync:function(selector,btid){this._dom(this.CMD_HTML,selector,$v(selector).html(),btid);},reload:function(url,btid){$bt.send({name:$bt.CMD_RELOAD,url:url,to:!btid?'*':btid});},_refresh:function(){$bt.list=$j.obj($l.get($bt.LS_TABS));},_broadcast:function(){$bt.send({name:$bt.CMD_SYNC});},_remove:function(id){if(!id)id=$bt.id;var i=$bt.list.indexOf(id);if(i>-1)$bt.list.splice(i,1);},_init:function(fn){$v(window).on('beforeunload',$bt._unload);$v(window).on('storage',$bt._cmd);$v(window).on('focus',$bt._focus);$bt.id=(new Date).getTime();var t=$l.get($bt.LS_TABS);$bt.list=t==null?[]:$j.obj(t);$bt.list.push($bt.id);$l.set($bt.LS_TABS,$j.str($bt.list));$bt._broadcast();$bt.log($bt.list);if(typeof fn=="function")fn();},_dom:function(n,s,d,id){$bt.send({name:n,selector:s,data:d,to:!id?'*':id});},_unload:function(e){$bt._refresh();$bt._remove();$l.set($bt.LS_TABS,$j.str($bt.list));$bt._broadcast();return null;},_focus:function(e){$l.set($bt.LS_CURTAB,$bt.id);},_cmd:function(e){if(e.key!=$bt.LS_CMD)return;var cmd=$j.obj(e.newValue);if(!cmd)return;if(cmd.to=="*"||cmd.to==$bt.id){$bt.log("do "+cmd.name);$bt.log(cmd);switch(cmd.name){case $bt.CMD_SYNC:$bt._refresh();$bt.log($bt.list);break;case $bt.CMD_APPEND:$v(cmd.selector).append(cmd.data);break;case $bt.CMD_HTML:$v(cmd.selector).html(cmd.data);break;case $bt.CMD_RELOAD:window.location=typeof cmd.url!="undefined"?cmd.url:window.location;break;default:if(typeof $bt.on=="function")$bt.on(cmd);}}}} var $v=function(p){var s=typeof p=="string";var c=s?document.querySelectorAll(p):null;var a=s?[].slice.call(c):[p];delete(s);if(p==localStorage){return{clear:function(){return p.clear();},get:function(k){return p.getItem(k);},rem:function(k){return p.removeItem(k);},set:function(k,v){return p.setItem(k,v);}};} else if(p==JSON){return{str:function(o){return p.stringify(o);},obj:function(s){return p.parse(s);}};} else{this.foreach=function(f){[].forEach.call(c,f);};this.html=function(d){if(arguments.length==0)return a[0].innerHTML;else a[0].innerHTML=d;};this.append=function(d){this.foreach(function(n){n.innerHTML+=d;});};this.on=function(t,f,u){if(c!=null){this.foreach(function(n){n.addEventListener(t,f,u===true);});} else a[0].addEventListener(t,f,u===true);};this.val=function(d){if(arguments.length==0)return a[0].value;else a[0].value=d;};this.attr=function(k,v){if(arguments.length==1)return a[0].getAttribute(k);else a[0].setAttribute(k,v);};this.ready=function(f){document.addEventListener('DOMContentLoaded',f);};return this;}} var $l=$v(localStorage);var $j=$v(JSON);