var UR=UR||{};UR.client={init:function(a,v,d,j){var m=true,h,t=UR.util.newGUID(),e,p,l="web-"+UR.util.myGUID();var f,c,o,q,g,k,u,i,b,s;f=function(x,y,w){x.Source=l;$.ajax({type:"POST",url:"/client/request",data:JSON.stringify(x),headers:{"UR-Connection-ID":h}}).done(y||function(z){console.debug("Request success, request: \n"+JSON.stringify(z))}).fail(w||function(B,C,z){var A={status:C,headers:this.headers,error:z.message,data:x};console.debug(A)})};var n=0;longPollEvents=function(x,w){if(m){setTimeout(function(){longPollEvents(x,w)},300);return}$.ajax({type:"GET",url:"/client/event",headers:{"UR-Connection-ID":h}}).done(x||function(y){console.debug("Request success, data: \n"+JSON.stringify(y))}).fail(w||function(A,B,y){n++;if(n>5){n=0;o()}var z={name:"Request failed",status:B,headers:this.headers,error:y.message};console.debug(z)}).always(function(){longPollEvents(x,w)})};s=function(w){if(w.Remotes){UR.store.saveRemotes(w.Remotes);model.remotes=UR.store.getRemoteList();model.active_remotes=_.where(model.remotes,{Hidden:false}).length}if(w.Layouts){UR.store.updateLayouts(w.Layouts,function(y,x){fetchRemoteLayout(y,x)})}m=false;a()};b=function(y){var z=UR.store.getRemoteHash();var w=UR.store.getLayoutsHash();var x={Action:UR.enums.DataPacketAction.Sync,Request:UR.enums.DataPacketAction.Sync,Remotes:z,Layouts:w};f(x,s)};i=function(x){tip("Synching remotes");var w={Action:UR.enums.DataPacketAction.Hash,Request:UR.enums.DataPacketAction.Hash};f(w,b)};u=function(w){if(w.Security===UR.enums.DataSecurity.Invalid){UR.store.resetCredentials();disconnect();j()}if(w.Security===UR.enums.DataSecurity.Disabled){UR.store.saveCredentials(p);i()}if(w.Security===UR.enums.DataSecurity.Enable){alert("Sorry, encryption is not supported in this client")}};g=function(w){e=(w||{}).Password;var x={Capabilities:{Actions:true,Sync:true,Grid:true,Fast:false,Loading:true,Encryption2:true},Action:UR.enums.DataPacketAction.Authenticate,Request:UR.enums.DataPacketAction.Authenticate};p=UR.store.getSavedCredentials();if(w.Security===UR.enums.DataSecurity.Users){k=function(){x.Password=UR.util.sha256(e+p.username+":"+p.password+t);f(x,u)};if(p===false){d(true)}else{k()}}if(w.Security===UR.enums.DataSecurity.Anonymous){k=function(){x.Password=UR.util.sha256(e+p.password+t);f(x,u)};if(p===false){d(false)}else{k()}}if(w.Security==UR.enums.DataSecurity.Disabled){f(x,u)}};q=function(){var w={Action:UR.enums.DataPacketAction.Handshake,Request:UR.enums.DataPacketAction.Handshake,Version:UR.enums.HandshakeVersionCode,Password:t,Platform:"web"};f(w,g)};c=function(){$.get("/client/connect").done(function(w){id=(w.id||"").trim();h=id;console.debug("Client connected ("+id+")");q()}).fail(function(y,z,w){var x={name:"Connect failed",status:z,headers:this.headers,error:w};console.debug(x);setTimeout(function(){o()},3000)})};disconnect=function(w){$.ajax({type:"GET",url:"/client/disconnect",headers:{"UR-Connection-ID":h}}).done(function(x){console.debug("Client stopped")}).fail(function(y,z,x){console.log("Failed to stop connection")})};o=function(){v();m=true;c()};fetchRemoteLayout=function(y,x){var w={Action:UR.enums.DataPacketAction.Layout,Request:UR.enums.DataPacketAction.Layout,ID:y,Layout:{Hash:0}};f(w,function(z){x(z.Layout)})};getRemoteLayout=function(y,x){var w=UR.store.getLayout(y);if(w){x(w)}else{fetchRemoteLayout(y,x)}};loadRemote=function(y,x,z){var w={ID:y.ID,Action:UR.enums.DataPacketAction.Load,Request:UR.enums.DataPacketAction.Load,Layout:_.pick(x,"Hash")};f(w,function(A){z(A)})};unloadRemote=function(x,y){var w={ID:x.ID,Action:UR.enums.DataPacketAction.Unload,Request:UR.enums.DataPacketAction.Unload};f(w,function(z){y(z)})};fetchState=function(x,y){var w={ID:x.ID,Action:UR.enums.DataPacketAction.State,Request:UR.enums.DataPacketAction.State};f(w,function(z){y(z)})};sendRemoteAction=function(y,x,z){var w={ID:y.ID,Action:UR.enums.DataPacketAction.Action,Request:UR.enums.DataPacketAction.Action,Run:x};f(w,function(A){z(A)})};sendKeepAlive=function(){if(m){return}var w={KeepAlive:true};f(w,function(x){console.debug("KeepAlive sent!")})};var r;startListen=function(w){longPollEvents(w);r=setInterval(function(){sendKeepAlive()},60*1000)};c();return{startRemote:function(y,w,x){getRemoteLayout(y.ID,function(z){w(z);console.debug("Remote "+y.ID+" loading");loadRemote(y,z,function(A){console.debug("Remote "+y.ID+" loaded");x(A);fetchState(y,function(B){console.debug("Remote "+y.ID+" focused")})})})},stopRemote:function(w){console.debug("Remote "+w.ID+" unloading remote");unloadRemote(w,function(x){console.debug("Remote "+w.ID+" unloaded")})},sendAction:function(x,w){if(w===undefined){return}sendRemoteAction(x,w,function(y){console.debug("Action "+JSON.stringify(w)+" sent!")})},listenToEvents:function(w){startListen(w)},onAuthentication:function(w,x){p={username:w,password:x};k()},stop:function(w){m=true;clearInterval(r);disconnect()},restart:function(){m=true;c()}}}};