/*!
Copyright (C) 2012 Jari-Pekka Voutilainen

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/
typeof Lively3D=="undefined"&&(Lively3D={});var Lively3D=function(a){var b="",c="canvas";a.GLGE={document:null,renderer:null};var d=[],e=0,f={Id:"mainscene",BindCanvasEvents:function(){console.log("binding room events")},RenderingFunction:function(){},GLGEObject:"DefaultSceneObject.children[0]",GLGEGroup:"DefaultSceneObject",Open:function(a,b){a.GetWindowObject().setLookat(null),a.GetWindowObject().setRotZ(0).setRotX(0).setRotY(Math.PI),a.GetWindowObject().setScale(3,3,3),a.GetWindowObject().setLoc(a.GetSceneObject(0).getLocX(),a.GetSceneObject(0).getLocY(),a.GetSceneObject(0).getLocZ())},Close:function(a,b){a.GetSceneObject(0).setLoc(a.GetWindowObject().getLocX(),a.GetWindowObject().getLocY(),a.GetWindowObject().getLocZ())}};a.GLGE.draggableObject=null,a.GLGE.hoverObject=null,a.GLGE.clickedObject=null,a.PERMISSIONS={DRAG:"drag",OPEN:"open",CLOSE:"close",MAXIMIZE:"maximize"};var g=null,h=null,i=[];a.Init=function(i){var j=$("<h1>Enter Username</h1>Username: <input type='text' name='username' id='username'/><h3 onclick='Lively3D.UI.EnterUsername()'>Ok</h3>");a.UI.ShowHTML(j,!0),i?b=i:b=c,this.GLGE.document=new GLGE.Document;var i=document.getElementById(b);this.GLGE.document.onLoad=function(){function g(){requestAnimFrame(g,i),c=parseInt((new Date).getTime()),d[e].GetModel().RenderingFunction(c,b),a.GLGE.renderer.render(),b=c}a.GLGE.renderer=new GLGE.Renderer(i,null,{alpha:!0,depth:!0,stencil:!0,antialias:!1,premultipliedAlpha:!0,preserveDrawingBuffer:!0}),d.push((new a.Scene).SetScene(a.GLGE.document.getElement(f.Id)).SetModel(f)),a.GLGE.renderer.setScene(d[e].GetScene());var b=0,c;g(),a.GLGE.document.onLoad=o},this.GLGE.document.load("lively3d.xml"),g=new GLGE.KeyInput,h=new GLGE.MouseInput(i),m(i),$(document).keydown(function(b){a.GLGE.clickedObject!=null&&a.GLGE.clickedObject.fireEvent("keydown",b)}),$(document).keyup(function(b){a.GLGE.clickedObject!=null&&a.GLGE.clickedObject.fireEvent("keyup",b)}),$(document).keypress(function(b){a.GLGE.clickedObject!=null&&a.GLGE.clickedObject.fireEvent("keypress",b)})},a.AddApplication=function(b,c,f){var g=new a.Application;i.push(g),g.SetName(b).SetApplicationCode(c).SetInitializationCode(f);var h=f(c),k=h.GetCanvas();g.SetStart(h.StartApp);var l=new GLGE.TextureCanvas;l.setCanvas(k);var m=(new GLGE.MaterialLayer).setMapinput(GLGE.UV1).setMapto(GLGE.M_COLOR).setTexture(l),n=(new GLGE.Material).addTexture(l).addMaterialLayer(m).setSpecular(0);j(n,h.EventListeners);var o=(new GLGE.Texture).setSrc("images/app.png");m=(new GLGE.MaterialLayer).setMapinput(GLGE.UV1).setMapto(GLGE.M_COLOR).setTexture(o);var p=(new GLGE.Material).addTexture(o).addMaterialLayer(m).setSpecular(0),q=new GLGE.Object;q.setMesh(this.GLGE.document.getElement("ProgTitleBar"));var r=document.createElement("canvas");r.setAttribute("style","display:none"),r.height=64,r.width=512;var s=r.getContext("2d");s.fillStyle="#fff",s.fillRect(0,0,512,164),s.fillStyle="#333",s.font="bold 50px arial",s.fillText(b,5,50);var t=new GLGE.TextureCanvas;t.setCanvas(r);var u=(new GLGE.MaterialLayer).setMapinput(GLGE.UV1).setMapto(GLGE.M_COLOR).setTexture(t),v=(new GLGE.Material).addTexture(t).addMaterialLayer(u).setSpecular(0);q.setMaterial(v);var w=new GLGE.Object;w.setMesh(this.GLGE.document.getElement("ProgCloseButton")).setMaterial(this.GLGE.document.getElement("closebutton"));var x=new GLGE.Object;x.setMesh(this.GLGE.document.getElement("ProgWindow")),x.setMaterial(n);var y=new GLGE.Group;y.addChild(q).addChild(w).addChild(x),g.SetWindowObject(y);var z=new GLGE.Object;z.setLoc(0,0,0),z.setId("app"),z.setMesh(this.GLGE.document.getElement("cube2")),z.setMaterial(p);var A=new GLGE.Group;A.addChild(z),A.addEventListener("mouseover",function(){g.GetCurrentSceneObject().addChild(g.hoverText)}),A.addEventListener("mouseout",function(){g.GetCurrentSceneObject().removeChild(g.hoverText)});var B={group:A};g.AddSceneObject(B),h.GetState&&g.SetSave(h.GetState),h.SetState&&g.SetLoad(h.SetState),h.Open&&g.SetAppOpen(h.Open),h.Close&&g.SetAppClose(h.Close),g.hoverText=(new GLGE.Text).setText(g.GetName()).setLoc(g.GetCurrentSceneObject().getLocX()+5,g.GetCurrentSceneObject().getLocY(),g.GetCurrentSceneObject().getLocZ()).setId("hovertext").setSize(50).setColor("lightblue").setFont("arial").setLookat(d[0].GetScene().camera),g.AddPermission(this.PERMISSIONS.DRAG,q),g.AddPermission(this.PERMISSIONS.DRAG,z),g.AddPermission(this.PERMISSIONS.CLOSE,w),g.AddPermission(this.PERMISSIONS.MAXIMIZE,q);for(var C=1;C<d.length;++C){var D=d[C].GetModel().CreateApplication(n);g.AddSceneObject(D)}return d[0].GetScene().addObject(A),h.SetLivelyApp(g),g.SetCurrentSceneObject(e),g};var j=function(a,b){a!=null&&b!=null&&$.each(b,function(b,c){a.addEventListener(b,c)})},k=!1;a.Open=function(b){for(var c in d)d.hasOwnProperty(c)&&d[c].GetScene().removeChild(b.GetSceneObject(c));d[e].GetScene().addChild(b.GetWindowObject()),d[e].GetModel().Open(b,d[e].GetScene()),b.ToggleWindowObject(),this.GLGE.clickedObject=b.GetWindowMaterial(),b.Open(),a.GLGE.hoverObject=null},a.Close=function(b){b.GetWindowObject().setScale(1,1,1),b.isMaximized()&&a.Minimize(b),b.ToggleWindowObject(),this.GLGE.clickedObject=null,b.Close();for(var c in d)d.hasOwnProperty(c)&&d[c].GetScene().addChild(b.GetSceneObject(c));d[e].GetScene().removeChild(b.GetWindowObject()),d[e].GetModel().Close(b,d[e].GetScene())},a.Maximize=function(a){var b=d[e].GetScene(),c=b.makeRay(window.innerWidth/2,window.innerHeight/2),f=[];f=[c.origin[0]-c.coord[0]*11,c.origin[1]-c.coord[1]*11,c.origin[2]-c.coord[2]*11],a.OldLocation=[a.GetWindowObject().getLocX(),a.GetWindowObject().getLocY(),a.GetWindowObject().getLocZ()],a.GetWindowObject().setLoc(f[0],f[1],f[2]).setScale(1,1,1),a.Maximize(),this.GLGE.clickedObject=a.GetWindowMaterial()},a.Minimize=function(a){a.GetWindowObject().setLoc(a.OldLocation[0],a.OldLocation[1],a.OldLocation[2]).setScale(3,3,3),a.Minimize(),this.GLGE.clickedObject=a.GetWindowMaterial()},a.GetApplication=function(a){for(var b in i)if(i.hasOwnProperty(b)&&i[b].GetCurrentSceneObject()===a)return i[b];return null},a.FireEvent=function(a,b,c,d,e){if(c,d){var f;f=d.getMaterial();var g=f.getLayers()[0].getTexture();if(g.getCanvas){var h;if(a!="mousewheel"){var i=g.getCanvas(),j=[c[0]*i.width,(1-c[1])*i.height];h={coord:j,canvas:i}}else h=e;f.fireEvent(a,h)}}else b.GetCurrentSceneObject().fireEvent(a)},a.PickLocation=function(a,b){var c=l(a),f=b.pageX-c.x,g=b.pageY-c.y,h=d[e].GetScene().pick(f,g);return h};var l=function(a){var b=curtop=0;if(a.offsetParent){do b+=a.offsetLeft,curtop+=a.offsetTop;while(a=a.offsetParent);return{x:b,y:curtop}}};a.GetCurrentSceneIndex=function(){return e},a.FileOperations={},a.FileOperations.getScript=function(a,b){$.get("getFile.php",{name:a,path:b},function(a){var b=document.createElement("script");b.type="text/javascript",b.src=a,document.head.appendChild(b)})},a.FileOperations.uploadScript=function(a,b,c){$.post("uploadfile.php",{name:a,file:b,path:c},function(){console.log("uploaded script: "+a)})},a.FileOperations.getJSON=function(a,b,c){$.get("getFile.php",{name:a,JSON:!0,path:c},function(a){$.getJSON(a,b)})},a.FileOperations.LoadResources=function(a,b){$.get("getFileArray.php",{"names[]":a.Resources[b],path:a.ResourcePath},function(c){var d=JSON.parse(c);a.ResourceHandlers[b](d),a.ResourcesLoaded(b)})},a.AllowAppStart=function(b){for(var c in d)d.hasOwnProperty(c)&&b.AddPermission(this.PERMISSIONS.OPEN,b.GetSceneObject(c));b.StateFromDropbox==1&&(b.OpenAfterLoad==1&&a.Open(b),b.MaximizeAfterLoad==1&&a.Maximize(b)),a.UI.ShowLoadCompleted()};var m=function(b){b.onclick=function(b){var c=a.PickLocation(this,b);if(c!=null){var d=a.GetApplication(c.object.parent);d!=null&&(d.CheckPermission(a.PERMISSIONS.CLOSE,c.object)&&a.Close(d),d.isClosed()||a.FireEvent("click",d,c.texture,c.object))}},b.ondblclick=function(b){var c=a.PickLocation(this,b);console.log("dblclick with loc: "+c);if(c!=null){var d=a.GetApplication(c.object.parent);d!=null&&(d.CheckPermission(a.PERMISSIONS.OPEN,c.object)?a.Open(d):d.CheckPermission(a.PERMISSIONS.CLOSE,c.object)?a.Close(d):d.CheckPermission(a.PERMISSIONS.MAXIMIZE,c.object)&&(d.isMaximized()?a.Minimize(d):a.Maximize(d)))}},b.onmousedown=function(b){var c=a.PickLocation(this,b);if(c!=null){var d=a.GetApplication(c.object.parent);d!=null&&(b.which==3?d.isClosed()||a.FireEvent("rightclick",d,c.texture,c.object):(d.CheckPermission(a.PERMISSIONS.DRAG,c.object)&&(d.isClosed()||!d.isMaximized())&&(a.GLGE.draggableObject=d.GetCurrentSceneObject()),d.isClosed()||(a.GLGE.clickedObject=d.GetWindowMaterial(),a.FireEvent("mousedown",d,c.texture,c.object))))}},b.onmousemove=function(b){if(a.GLGE.draggableObject!=null){var c=l(this),f=b.pageX-c.x,g=b.pageY-c.y,h=d[e].GetScene().makeRay(f,g),i=[h.origin[0]-h.coord[0]*120,h.origin[1]-h.coord[1]*120,h.origin[2]-h.coord[2]*120];a.GLGE.draggableObject.setLocX(i[0]),a.GLGE.draggableObject.setLocY(i[1])}else{var j=a.PickLocation(this,b);if(j!=null){var k=a.GetApplication(j.object.parent);j.object&&k!=a.GLGE.hoverObject&&(k!=null&&a.FireEvent("mouseover",k),a.GLGE.hoverObject&&a.FireEvent("mouseout",a.GLGE.hoverObject),a.GLGE.hoverObject=k),k!=null&&!k.isClosed()&&a.FireEvent("mousemove",k,j.texture,j.object)}}},b.onmouseup=function(b){if(a.GLGE.draggableObject!=null)a.GLGE.draggableObject=null;else{var c=a.PickLocation(this,b);if(c!=null){var d=a.GetApplication(c.object.parent);d!=null&&!d.isClosed()&&a.FireEvent("mouseup",d,c.texture,c.object)}}},b.onmousewheel=function(b){var c=a.PickLocation(this,b);if(c!=null){var d=a.GetApplication(c.object.parent);d!=null&&!d.isClosed()&&a.FireEvent("mousewheel",d,c.texture,c.object,b)}}},n=[];a.AddScene=function(b){console.log("Loading scene.."),n.push(b),b.Resources.Document="Document/"+b.File,b.ResourceHandlers.Document=function(b){a.GLGE.document.loadDocument(b[0])},a.LoadResources(b)};var o=function(b){var c=p(b);console.log("scene loaded from url: "+b+" and file is "+c.file);var e=!1;for(var f in n)n.hasOwnProperty(f)&&n[f].File==c.file&&(n[f].DocumentDone&&n[f].DocumentDone(),d.push((new a.Scene).SetScene(a.GLGE.document.getElement(n[f].Id)).SetModel(n[f])),q(n[f]),n.splice(f,1),e=!0);e==0&&console.log("error loading scene")},p=function(a){var b=/((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/;return a.match(b)?{url:RegExp["$&"],protocol:RegExp.$2,host:RegExp.$3,path:RegExp.$4,file:RegExp.$6,hash:RegExp.$7}:{url:"",protocol:"",host:"",path:"",file:"",hash:""}},q=function(b){for(var c in i)if(i.hasOwnProperty(c)){var e=b.CreateApplication(i[c].GetWindowObject().children[2].getMaterial());i[c].AddSceneObject(e),i[c].AddPermission(a.PERMISSIONS.OPEN,i[c].GetSceneObject(d.length-1))}};a.GetCurrentScene=function(){return d[e]},a.GetKeys=function(){return g},a.GetApplications=function(){return i},a.GetMouse=function(){return h},a.GetScenes=function(){return d},a.ChangeScene=function(){for(var c in i)i.hasOwnProperty(c)&&(i[c].isClosed()||a.Close(i[c]));e+=1,e==d.length&&(e=0);for(var c in i)i[c].SetCurrentSceneObject(e);a.GLGE.renderer.setScene(d[e].GetScene()),m(document.getElementById(b)),d[e].GetModel().BindCanvasEvents(document.getElementById(b))},a.LoadCompleted=function(){a.UI.ShowLoadCompleted()},a.Sync=function(){a.Proxies.Local.CheckAvailability(function(){a.Proxies.Local.Sync()})},a.LoadResources=function(b){for(var c in b.Resources)b.Resources.hasOwnProperty(c)&&(a.UI.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.LoadResources(b,c):(a.UI.HTTPServers.PROXY.inUse==1||a.UI.HTTPServers.NODE.inUse==1)&&a.FileOperations.LoadResources(b,c))};var r;return a.SetUsername=function(a){r=a},a.GetUsername=function(){return r},a}(Lively3D);(function(a){a.Application=function(){this.Save=function(){var a={};return a},this.Load=function(a){};var b=!0;this.isClosed=function(){return b};var c=function(){};this.Close=function(){b=!0,c()},this.SetAppClose=function(a){return c=a,this};var d=function(){};this.Open=function(){b=!1,d()},this.SetAppOpen=function(a){return d=a,this};var e=!1;this.isMaximized=function(){return e},this.Maximize=function(){e=!0},this.Minimize=function(){e=!1};var f=[];this.AddPermission=function(a,b){f[a]||(f[a]=[]),f[a].push(b)},this.CheckPermission=function(a,b){if(f[a])for(var c in f[a])if(f[a].hasOwnProperty(c))if(f[a][c]instanceof GLGE.Group){var d=f[a][c].children;for(var e in d)if(d[e]===b)return!0}else if(f[a][c]===b)return!0;return!1};var g=[];this.AddSceneObject=function(a){return g.push(a),i==null&&(i=a),this};var h;this.SetWindowObject=function(a){return h=a,this},this.GetWindowObject=function(){return h},this.GetWindowMaterial=function(){return h.children[2].getMaterial()};var i;this.ToggleWindowObject=function(){return i==h?i=g[a.GetCurrentSceneIndex()]:i=h,this},this.GetCurrentSceneObject=function(){return i.group?i.group:i},this.GetCurrentObject=function(){return i},this.SetCurrentSceneObject=function(a){a!=null&&a>=0&&a<g.length?i=g[a]:console.log("No such scene object")},this.GetSceneObject=function(a){if(a!=null&&a>=0&&a<g.length)return g[a].group;console.log("No such scene object")},this.GetAppObject=function(a){if(a!=null&&a>=0&&a<g.length)return g[a];console.log("No such scene object")};var j;this.SetName=function(a){return j=a,this},this.GetName=function(){return j};var k;this.SetApplicationCode=function(a){return k=a,this},this.GetApplicationCode=function(){return k};var l;this.SetInitializationCode=function(a){return l=a,this},this.GetInitializationCode=function(){return l},this.SetSave=function(a){return this.Save=a,this},this.SetLoad=function(a){return this.Load=a,this},this.SetStart=function(a){return this.StartApp=a,this}}})(Lively3D),function(a){a.Scene=function(){var a;this.SetModel=function(b){return a=b,this},this.GetModel=function(){return a};var b;this.SetScene=function(a){return b=a,this},this.GetScene=function(){return b}}}(Lively3D),function(a){a.UI={},a.UI.HTTPServers={LOCAL:{inUse:!1},NODE:{inUse:!1},PROXY:{inUse:!0}},a.UI.ToggleNode=function(){a.UI.HTTPServers.NODE.inUse=!a.UI.HTTPServers.NODE.inUse,a.UI.HTTPServers.PROXY.inUse=!a.UI.HTTPServers.PROXY.inUse},a.UI.UseLocal=function(){console.log("Going offline..."),a.UI.HTTPServers.LOCAL.inUse=!0},a.UI.UseOnline=function(){console.log("Going online..."),a.UI.HTTPServers.LOCAL.inUse=!1},a.UI.ShowLoadCompleted=function(){var a=$("<div id='loadcompleted'><p>Load Compeleted</p></div>");a.appendTo("#container"),a.fadeIn("slow",function(){setTimeout(function(){a.fadeOut("slow",function(){a.remove()})},1e3)})},a.UI.ShowAppList=function(){this.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.ShowAppList():this.HTTPServers.PROXY.inUse==1?a.Proxies.PHP.ShowAppList():a.Proxies.Node.ShowAppList()},a.UI.LoadApplication=function(b){this.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.LoadApplication(b):this.HTTPServers.PROXY.inUse==1?a.Proxies.PHP.LoadApplication(b):a.Proxies.Node.LoadApplication(b)},a.UI.ShowStateList=function(){this.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.ShowStateList():this.HTTPServers.PROXY.inUse==1?a.Proxies.PHP.ShowStateList():a.Proxies.Node.ShowStateList()},a.UI.ShowSceneList=function(){this.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.ShowSceneList():this.HTTPServers.PROXY.inUse==1?a.Proxies.PHP.ShowSceneList():a.Proxies.Node.ShowSceneList()},a.UI.LoadScene=function(b){this.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.LoadScene(b):this.HTTPServers.PROXY.inUse==1?a.Proxies.PHP.LoadScene(b):a.Proxies.Node.LoadScene(b)},a.UI.SaveDesktop=function(b){this.HTTPServers.LOCAL.inUse==1?a.Proxies.Local.SaveDesktop(b):this.HTTPServers.PROXY.inUse==1?a.Proxies.PHP.SaveDesktop(b):a.Proxies.Node.SaveDesktop(b)},a.UI.LoadDesktop=function(b){this.HTTPServers.LOCAL.inUse==1?(a.Proxies.Local.LoadDesktop(b),a.UI.CloseDialog()):this.HTTPServers.PROXY.inUse==1?(a.Proxies.PHP.LoadDesktop(b),a.UI.CloseDialog()):(a.Proxies.Node.LoadDesktop(b),a.UI.CloseDialog())};var b;a.UI.ShowSaveDialog=function(){var c=$('<h1>Save state</h1>State name:<input type="text" name="statename" id="statename"/><h3 onclick="Lively3D.UI.CloseSaveDialog();">Save</h3>');this.ShowHTML(c),b=a.GLGE.clickedObject,a.GLGE.clickedObject=null},a.UI.CloseSaveDialog=function(){var c=$("#statename");c[0].value.length!=0?(a.UI.SaveDesktop(c[0].value),this.CloseDialog(),b&&(a.GLGE.clickedObject=b,b=null)):$("<h3>Please supply state name</h3>").appendTo("#dialog")},a.UI.ShowAbout=function(){var a=$("<h1>About</h1><p>Lively3D code made by Jari-Pekka Voutilainen</p><p>Applications developed by: Arto Salminen, Matti Anttonen, Anna-Liisa Mattila, Lotta Liikkanen, Jani Heininen, Mika V�lim�ki</p>");this.ShowHTML(a)},a.UI.ShowMessage=function(a){var b=$("<p>"+a+"</p>");this.ShowHTML(b)},a.UI.ActiveDialog,a.UI.ShowHTML=function(a,b){if(this.ActiveDialog!=null)this.ActiveDialog.cancelOmitted!=1&&c(a);else{if(b!=1)var d=$("<div class='dialog' id='dialog'><h3 onclick='Lively3D.UI.CloseDialog();'>Cancel</h3></div>");else{var d=$("<div class='dialog' id='dialog'></div>");d.cancelOmitted=!0}d.prepend(a),d.appendTo($("#container")),d.fadeIn("slow"),this.ActiveDialog=d}},a.UI.CloseDialog=function(){this.ActiveDialog.fadeOut("slow",function(){a.UI.ActiveDialog.remove(),a.UI.ActiveDialog=null})};var c=function(b){a.UI.ActiveDialog.fadeOut("fast",function(){a.UI.ActiveDialog.remove();var c=$("<div class='dialog' id='dialog'><h3 onclick='Lively3D.UI.CloseDialog();'>Cancel</h3></div>");c.prepend(b),c.appendTo($("#container")),c.fadeIn("fast"),a.UI.ActiveDialog=c})};a.UI.EnterUsername=function(){var b=$("#username");b[0].value.length!=0?(a.SetUsername(b[0].value),this.CloseDialog()):$("<h3>Please enter username</h3>").appendTo("#dialog")}}(Lively3D),function(Lively3D){Lively3D.Proxies={},Lively3D.Proxies.PHP={ShowAppList:function(){$.get("getFileList.php",{path:"apps"},function(a){var b=JSON.parse(a),c=$("<h1>Select Application</h1><div></div>"),d=c.last();for(var e in b)if(b.hasOwnProperty(e)){var f=$("<span onclick=\"Lively3D.UI.LoadApplication('"+b[e]+"')\">"+b[e]+"</span></br>");f.appendTo(d)}Lively3D.UI.ShowHTML(c)})},LoadApplication:function(a){Lively3D.FileOperations.getScript(a,"apps/"),Lively3D.UI.CloseDialog()},SaveDesktop:function(a){var b=Lively3D.GetApplications(),c="",d=[];for(var e in b)if(b.hasOwnProperty(e)){var f=b[e],g=f.isMaximized();f.isMaximized()==1&&Lively3D.Minimize(f);var h={Name:f.GetName(),Location:{x:f.GetCurrentSceneObject().getLocX(),y:f.GetCurrentSceneObject().getLocY(),z:f.GetCurrentSceneObject().getLocZ()},Rotation:f.GetCurrentSceneObject().getRotation(),Closed:f.isClosed(),Maximized:g,Code:f.GetApplicationCode().toString(),Init:f.GetInitializationCode().toString(),AppState:f.Save()};d.push(h),g==1&&Lively3D.Maximize(f)}c=JSON.stringify(d),Lively3D.FileOperations.uploadScript(a,c,"states/"+Lively3D.GetUsername())},LoadDesktop:function(a){var b=Lively3D.GetApplications(),c=b.length,d=Lively3D.GetScenes();for(var e=0;e<c;++e){for(var f in d)d.hasOwnProperty(f)&&(d[f].GetScene().removeChild(b[0].GetSceneObject(f)),d[f].GetScene().removeChild(b[0].GetWindowObject()));b.splice(0,1)}Lively3D.FileOperations.getJSON(a,ParseDesktopJSON,"states/"+Lively3D.GetUsername()+"/")},ShowStateList:function(){$.get("getFileList.php",{path:"states/"+Lively3D.GetUsername()},function(a){var b=JSON.parse(a),c=$("<h1>Select State</h1><div></div>"),d=c.last();for(var e in b)if(b.hasOwnProperty(e)){var f=$("<span onclick=\"Lively3D.UI.LoadDesktop('"+b[e]+"')\">"+b[e]+"</span></br>");f.appendTo(d)}Lively3D.UI.ShowHTML(c)})},ShowSceneList:function(){$.get("getFileList.php",{path:"scenes"},function(a){var b=JSON.parse(a),c=$("<h1>Select Scene</h1><div></div>"),d=c.last();for(var e in b)if(b.hasOwnProperty(e)){var f=$("<span onclick=\"Lively3D.UI.LoadScene('"+b[e]+"')\">"+b[e]+"</span></br>");f.appendTo(d)}Lively3D.UI.ShowHTML(c)})},LoadScene:function(a){Lively3D.FileOperations.getScript(a,"scenes/"),Lively3D.UI.CloseDialog()}};var ParseDesktopJSON=function(data){var JSONArray=JSON.parse(data);for(var i in JSONArray)if(JSONArray.hasOwnProperty(i)){var JSONObject=JSONArray[i],CodeFunc=eval("("+JSONObject.Code+")"),InitFunc=eval("("+JSONObject.Init+")"),app=Lively3D.AddApplication(JSONObject.Name,CodeFunc,InitFunc);SetAppLocation(app,JSONObject.Location),SetAppRotation(app,JSONObject.Rotation),app.StateFromDropbox=!0,JSONObject.Closed!=1&&(app.OpenAfterLoad=!0),JSONObject.Maximized==1&&(app.MaximizeAfterLoad=!0),app.Load(JSONObject.AppState),app.StartApp()}},SetAppLocation=function(a,b){a.GetSceneObject(0).setLocX(b.x),a.GetSceneObject(0).setLocY(b.y),a.GetSceneObject(0).setLocZ(b.z)},SetAppRotation=function(a,b){a.GetSceneObject(0).setRotX(b.x),a.GetSceneObject(0).setRotY(b.y),a.GetSceneObject(0).setRotZ(b.z)};Lively3D.Proxies.Node={ShowAppList:function(){$.get("/lively3d/node/filelist/apps",function(a){var b=$("<h1>Select Application</h1><div></div>"),c=b.last();for(var d in a)if(a.hasOwnProperty(d)){var e=$("<span onclick=\"Lively3D.UI.LoadApplication('"+a[d]+"')\">"+a[d]+"</span></br>");e.appendTo(c)}Lively3D.UI.ShowHTML(b)})},LoadApplication:function(a){$.get("/lively3d/node/file/apps/"+a,function(a){var b=document.createElement("script");b.type="text/javascript",b.src=a,document.head.appendChild(b)}),Lively3D.UI.CloseDialog()},SaveDesktop:function(a){var b=Lively3D.GetApplications(),c={};c.name=a,c.user=Lively3D.GetUsername(),c.applications=[];for(var d in b)if(b.hasOwnProperty(d)){var e=b[d],f=e.isMaximized();e.isMaximized()==1&&Lively3D.Minimize(e);var g={Name:e.GetName(),Location:{x:e.GetCurrentSceneObject().getLocX(),y:e.GetCurrentSceneObject().getLocY(),z:e.GetCurrentSceneObject().getLocZ()},Rotation:e.GetCurrentSceneObject().getRotation(),Closed:e.isClosed(),Maximized:f,Code:e.AppCode.toString(),Init:e.AppInit.toString(),AppState:e.Save()};c.applications.push(g),f==1&&Lively3D.Maximize(e)}$.post("/lively3d/node/states/new",{state:c},function(a){console.log(a)})},LoadDesktop:function(name){var Applications=Lively3D.GetApplications(),appcount=Applications.length,scenes=Lively3D.GetScenes();for(var i=0;i<appcount;++i){for(var j in scenes)scenes.hasOwnProperty(j)&&(scenes[j].GetScene().removeChild(Applications[0].GetSceneObject(j)),scenes[j].GetScene().removeChild(Applications[0].GetWindowObject()));Applications.splice(0,1)}$.get("/lively3d/node/states/"+Lively3D.GetUsername()+"/"+name,function(data){var apps=data.applications;for(var i in apps)if(apps.hasOwnProperty(i)){var JSONObject=apps[i],CodeFunc=eval("("+JSONObject.Code+")"),InitFunc=eval("("+JSONObject.Init+")"),app=Lively3D.AddApplication(JSONObject.Name,CodeFunc,InitFunc);SetAppLocation(app,JSONObject.Location),SetAppRotation(app,JSONObject.Rotation),app.StateFromDropbox=!0,JSONObject.Closed!="true"&&(app.OpenAfterLoad=!0),JSONObject.Maximized=="true"&&(app.MaximizeAfterLoad=!0),app.Load(JSONObject.AppState),app.StartApp()}})},ShowStateList:function(){$.get("/lively3d/node/states/"+Lively3D.GetUsername(),function(a){var b=$("<h1>Select State</h1><div></div>"),c=b.last();for(var d in a)if(a.hasOwnProperty(d)){var e=$("<span onclick=\"Lively3D.UI.LoadDesktop('"+a[d]+"')\">"+a[d]+"</span></br>");e.appendTo(c)}Lively3D.UI.ShowHTML(b)})},ShowSceneList:function(){$.get("/lively3d/node/filelist/scenes",function(a){var b=$("<h1>Select Scene</h1><div></div>"),c=b.last();for(var d in a)if(a.hasOwnProperty(d)){var e=$("<span onclick=\"Lively3D.UI.LoadScene('"+a[d]+"')\">"+a[d]+"</span></br>");e.appendTo(c)}Lively3D.UI.ShowHTML(b)})},LoadScene:function(a){$.get("/lively3d/node/file/scenes/"+a,function(a){var b=document.createElement("script");b.type="text/javascript",b.src=a,document.head.appendChild(b)}),Lively3D.UI.CloseDialog()}},Lively3D.Proxies.Local={ShowAppList:function(){$.ajax({url:"http://127.0.0.1:8000/filelist/apps",success:function(a){var b=$("<h1>Select Application</h1><div></div>"),c=b.last();for(var d in a)if(a.hasOwnProperty(d)){var e=$("<span onclick=\"Lively3D.UI.LoadApplication('"+a[d]+"')\">"+a[d]+"</span></br>");e.appendTo(c)}Lively3D.UI.ShowHTML(b)}})},LoadApplication:function(a){$.get("http://127.0.0.1:8000/file/apps/"+a,function(a){var b=document.createElement("script");b.type="text/javascript",b.src=a,document.head.appendChild(b)}),Lively3D.UI.CloseDialog()},ShowSceneList:function(){$.get("http://localhost:8000/filelist/scenes",function(a){var b=$("<h1>Select Scene</h1><div></div>"),c=b.last();for(var d in a)if(a.hasOwnProperty(d)){var e=$("<span onclick=\"Lively3D.UI.LoadScene('"+a[d]+"')\">"+a[d]+"</span></br>");e.appendTo(c)}Lively3D.UI.ShowHTML(b)})},LoadScene:function(a){$.get("http://localhost:8000/file/scenes/"+a,function(a){var b=document.createElement("script");b.type="text/javascript",b.src=a,document.head.appendChild(b)}),Lively3D.UI.CloseDialog()},CheckAvailability:function(a){$.ajax({url:"http://localhost:8000/",success:function(b){b.success==0?Lively3D.UI.ShowMessage(b.message):a()},error:function(){Lively3D.UI.ShowMessage("Local Node.js application not found. Please run 'node local.js'")}})},Sync:function(a){$.ajax({url:"http://localhost:8000/sync",data:{host:window.location.host,port:window.location.port,path:window.location.pathname},success:function(a){a.success==1&&Lively3D.UI.ShowMessage(a.message)},error:function(){Lively3D.UI.ShowMessage("Local Node.js application not found. Please run 'node local.js' and associated Mongo-database.")}})},LoadResources:function(a,b){$.get("http://localhost:8000/filearray",{"names[]":a.Resources[b],path:a.ResourcePath},function(c){a.ResourceHandlers[b](c),a.ResourcesLoaded(b)})}}}(Lively3D)