/* by a-sansara - v 0.5 - https://github.com/pluie-org/bt */var $l=function(){var t=localStorage;return{clear:function(){return t.clear()},get:function(n){return t.getItem(n)},rem:function(n){return t.removeItem(n)},set:function(n,e){return t.setItem(n,e)}}}(),$j=function(){var t=JSON;return{str:function(n){return t.stringify(n)},obj:function(n){return t.parse(n)}}}(),$bt={VERSION:.5,TRACE:!0&&!$.isNone(console),LS_TABS:"bt.tabs",LS_CURTAB:"bt.ctab",LS_CMD:"bt.cmd",CMD_SYNC:"bt.sync",CMD_VAR_SET:"var.set",CMD_VAR_GET:"var.get",CMD_APPEND:"dom.append",CMD_HTML:"dom.html",CMD_RELOAD:"bt.reload",vars:[],init:function(t){this._init(t)},on:function(t){this.log(t)},log:function(t){this.TRACE&&console.log(t)},send:function(t){t.uid=this.id+Math.random(),t.from=this.id,$.isNone(t.to)&&(t.to="*"),t=$j.str(t),$bt.log("sending cmd : "+this.LS_CMD+" : "+t),$l.set(this.LS_CMD,t),$l.rem(this.LS_CMD)},append:function(t,n,e,o,c){this._dom(this.CMD_APPEND,e,t,n,o,c)},html:function(t,n,e,o,c){this._dom(this.CMD_HTML,e,t,n,o,c)},sync:function(t,n,e,o){var c=$.isNone(n)||null==n?document:parent.frames[n].document;this._dom(this.CMD_HTML,n,t,$(t,c).html(),e,o)},reload:function(t,n){$bt.send({name:$bt.CMD_RELOAD,url:t,to:n?n:"*"})},varset:function(t,n){$bt.vars[t]=n,$bt.send({name:$bt.CMD_VAR_SET,data:{k:t,v:n}})},varget:function(t){if($bt.list.length>1){var n=$bt.list[0]==$bt.id?$bt.list[1]:$bt.list[0];$bt.send({name:$bt.CMD_VAR_GET,data:{k:t},to:n})}return $bt.vars[t]},_refresh:function(){$bt.list=$j.obj($l.get($bt.LS_TABS))},_broadcast:function(){$bt.send({name:$bt.CMD_SYNC})},_remove:function(t){t||(t=$bt.id);var n=$bt.list.indexOf(t);n>-1&&$bt.list.splice(n,1)},_init:function(t){$(window).on("beforeunload",$bt._unload),$(window).on("storage",$bt._cmd),$(window).on("focus",$bt._focus),$bt.id=(new Date).getTime();var n=$l.get($bt.LS_TABS);$bt.list=null==n?[]:$j.obj(n),$bt.list.push($bt.id),$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),$bt.log($bt.list),$.isFunc(t)&&t()},_dom:function(t,n,e,o,c,i){$bt.send({name:t,context:n,selector:e,data:o,fn:c,to:i?i:"*"})},_unload:function(){return $bt._refresh(),$bt._remove(),$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),null},_focus:function(){$l.set($bt.LS_CURTAB,$bt.id)},_cmd:function(e){if($.isNone(e.originalEvent)||(e=e.originalEvent),e.key==$bt.LS_CMD){var cmd=$j.obj(e.newValue);if(cmd)if($bt.log("RECEIVING cmd "+cmd.name+" : "),"*"==cmd.to||cmd.to==$bt.id){$bt.log("do "+cmd.name),$bt.log(cmd);try{$.isNone(cmd.context)||($bt.log(cmd.context),cmd.context=window.parent.frames[cmd.context].document,$bt.log(cmd.context))}catch(e){$bt.log("bad context "+cmd.context+" : "+e.message)}switch(cmd.name){case $bt.CMD_SYNC:$bt._refresh(),$bt.log($bt.list);break;case $bt.CMD_APPEND:$(cmd.selector,cmd.context).append(cmd.data);break;case $bt.CMD_HTML:$(cmd.selector,cmd.context).html(cmd.data);break;case $bt.CMD_RELOAD:window.location=$.isNone(cmd.url)?window.location:cmd.url;break;case $bt.CMD_VAR_GET:$bt.varset(cmd.data.k,$bt.vars[cmd.data.k],cmd.from);break;case $bt.CMD_VAR_SET:$bt.vars[cmd.data.k]=cmd.data.v;break;default:$.isFunc($bt.on)&&$bt.on(cmd)}if($.isStr(cmd.fn)&&cmd.fn.length>0){$bt.log(cmd.fn);var f=eval("window."+cmd.fn);$.isFunc(f)&&f()}}else $bt.log("ignoring (not target)")}}};