(function(){function F(d){var n=d.lastIndexOf("/");return 0<=n?d.substr(n+1):d}function B(){for(var d="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),n=[],v=0;16>v;v++)n.push(d[0|Math.random()*d.length]);return n.join("")}window.firebase={database:{ServerValue:{TIMESTAMP:"_filebase_timestamp"}}};window.Filebase=function(d,n,v){function w(a,p){this.val=function(){return p};this.key=a;this.child=function(a){return null!==p&&p.hasOwnProperty(a)?new w(a,p[a]):new w(a,null)}}
function G(a,p){this.on=function(g,c,e,m,d){a.onEvent(g,c,e,m,p,!1)};this.once=function(g,c,e,m,d){a.onEvent(g,c,e,m,p,!0)};this.off=function(g,c,e){a.off(g,c,e)}}function E(a,p,g){var c=this,e={},m={},d={},n=[],k=null;c.getInitialData=function(b,a,c,f,h){h={op:"on",path:g,event:"value",once:!0,start_key:h,new_values:!1,key:B(),seq:r++};"undefined"===typeof e.value&&(e.value=[]);e.value.push({callback:function(c){if("child_added"===b)for(var q in c.val()){if(c.val().hasOwnProperty(q)){var h=new w(q,
c.val()[q]);f?a.call(f,h):a(h)}}else a(c)},cancelCallback:c,context:f,key:h.key,once:!0});t(h,null,k)};c.onEvent=function(b,a,l,f,h,d){"undefined"===typeof e[b]&&(e[b]=[]);if("child_added"===b||"value"===b)if(c.getInitialData(b,a,l,f,h),d)return;"undefined"===typeof m[b]&&(m[b]=B(),h={op:"on",path:g,event:b,once:d,start_key:"",new_values:!0,key:m[b],seq:r++},t(h,null,k));e[b].push({callback:a,cancelCallback:l,context:f,key:m[b],once:d})};c.set=function(b,a){b={op:"set",path:g,val:b,seq:r++};t(b,a,
k)};c.child=function(b){if(null===b||0===b.length)return null;"/"===b[0]&&(b=b.substr(1));var a=b.indexOf("/");if(0<=a)return c.child(b.substr(0,a)).child(b.substr(a+1));if(a=d[b])return a;a=new E(c,b,g+"/"+b);return d[b]=a};c.on=function(a,q,l,f,h){c.onEvent(a,q,l,f,"",!1)};c.once=function(a,q,l,f,h){c.onEvent(a,q,l,f,"",!0)};c.off=function(a,c,l){for(var b in e)if(e.hasOwnProperty(b)&&("undefined"===typeof a||a===b)){for(l=0;l<e[b].length;l++)listener=e[b][l],"undefined"!==typeof c&&listener.callback!==
c||e[b].splice(l,1);1>e[b].length&&(l={op:"off",path:g,event:b,key:m[b],seq:r++},t(l),delete m[b])}};c.remove=function(b){var c={op:"remove",path:g,seq:r++};t(c,b);a&&a.removeChild(F(g))};c.root=u;c.key=p;c.parent=function(){return a};c.push=function(a,q){var b=c.child(B());"undefined"===typeof a&&(a=null);b.set(a,q);return b};c.onDisconnect=function(){return{remove:function(a){t({op:"on_disconnect",payload:{op:"remove",path:g},seq:r++},a)},set:function(a,c){t({op:"on_disconnect",payload:{op:"set",
path:g,val:a},seq:r++},c)},cancel:function(a){t({op:"cancel_disconnect",path:g,seq:r++},a)}}};c.startAt=function(a,e){a=new G(c,e);n.push(a);return a};c.transaction=function(a,e,d){k=x=H++;c.once("value",function(b){var h=a(b.val());"undefined"!==typeof h?c.set(h,function(){x=null;C();k=null;e(null,!0,new w(p,h))}):(x=null,C(),k=null)})};c.fireEvent=function(a,c,d,f){var b=e[a];if(b){c=new w(c,f);for(f=0;f<b.length;f++)d===b[f].key&&(b[f].context?b[f].callback.call(context,c):b[f].callback(c),b[f].once&&
(b.splice(f,1),f--));1>b.length&&"undefined"!==typeof m[a]&&delete m[a]}};c.removeChild=function(a){delete d[a]};c.queries=function(){return n}}var k=new WebSocket(d),D=!1,z=[],r=1,y={},x=null,H=1,A=[];k.onmessage=function(a){a=JSON.parse(a.data);if("event"===a.type)u.child(a.path).fireEvent(a.event,a.key,a.listen_key,a.data);else if(a.seq&&y[a.seq]){if("result"===a.type)y[a.seq](a.data);else if("error"===a.type)y[a.seq](Error(a.message));delete y[a.seq]}};k.onopen=function(a){D=!0;"function"===typeof n&&
n();C()};k.onclose=function(a){D=!1;for(a=0;a<A.length;a++)if("function"===typeof A[a])A[a]()};k.onerror=function(a){"function"===typeof v&&v()};var C=function(){for(var a=0;a<z.length;a++)k.send(z[a]);z=[]},t=function(a,d,g){!D||null!==x&&g!==x?z.push(JSON.stringify(a)):k.send(JSON.stringify(a));d&&a.seq&&(y[a.seq]=d)},u=new E(null,null,"","");this.set=u.set;this.child=u.child;this.push=u.push;this.root=u;this.close=function(){1===k.readyState&&k.close()};this.onClose=function(a){A.push(a)}}})();
