function showTabContent(e){$("."+e).length||(e="editor"),$(".tab-con").addClass("hidden"),$("."+e).removeClass("hidden"),$(".app-nav li").removeClass("active"),$(".app-nav a[href=#"+e+"]").parent().addClass("active")}function addCopy(){var e=require("nw.gui"),t=e.Window.get(),n=new e.Menu({type:"menubar"});try{n.createMacBuiltin("DebugGap"),t.menu=n}catch(o){console.log(o.message)}}function listenOpen(){function e(){setTimeout(function(){window.open("./components/editor/index.html#","__empty__")},100)}function t(t){t=t.replace(/^"|"$/g,""),t=t.split('"'),t=t[t.length-1],"function"!=typeof n.App._events.open&&1!=n.App._events.open.length||(n.App.argv.push(t),e())}var n=require("nw.gui"),o=n.Window.get();n.App.argv.length&&e(),o.on("close",function(){n.App.removeListener("open",t),this.close(!0)}),n.App.on("open",t)}function preventDefault(){$(document.body).bind("dragover drop",function(e){e.preventDefault()})}function checkUpdate(){$.ajax({url:"http://www.debuggap.com/package/version.json?_d="+(new Date).getTime(),dataType:"json",success:function(e){try{var t=require("nw.gui");if(e&&parseInt(t.App.manifest.version.replace(/\./g,""))<parseInt(e.version.replace(/\./g,""))){var n=['<div class="version_upgrade"><h4><a href="javascript:void(0)" onclick="require(\'nw.gui\').Shell.openExternal(\'http://www.debuggap.com#new\');">New Version:'];n.push(e.version+"</a></h4>"),e.desc&&(n.push("<ul><li>"),n.push(e.desc.join("</li><li>")),n.push("</li><ul>")),n.push("</div>"),$(document.body).append(n.join(""))}}catch(o){}}})}window.onhashchange=function(){var e=location.hash.slice(1);showTabContent(e)},window.editorInstance={openProject:function(e){var t=window.open("./components/editor/index.html#"+e,e?e:"__empty__");t.focus()},loadProject:function(){var e={};if(localStorage.projects)try{e=JSON.parse(localStorage.projects)}catch(t){e={}}this.showList(e)},showList:function(e){var t,n=['<li class="empty"><a href="javascript:void(0)">Open Empty Project</a></li>'];for(t in e)n.push('<li><a href="javascript:void(0)"><b>',t,"</b><hr/>",e[t].path,'<span class="delete"></span></a></li>');$("#list").html(n.join(""))},getFilenameByPath:function(e){return e.split(this.getPathSplitChar()).slice(-1)[0]},getDirByPath:function(e){var t=e.lastIndexOf(this.getPathSplitChar());return e.slice(0,t)},getPathSplitChar:function(){var e=/win/i.test(navigator.userAgent)?"\\":"/";return e},checkProject:function(e){var t={},n=0;if(localStorage.projects)try{t=JSON.parse(localStorage.projects)}catch(o){t={}}for(var a in t)if(t[a].path==e){n=1;break}return n},saveProject:function(e){var t={};if(localStorage.projects)try{t=JSON.parse(localStorage.projects)}catch(n){t={}}for(var o=this.getFilenameByPath(e),a=0,i=o;t[i];)i=o+"("+a+")",a++;t[i]={path:e},localStorage.projects=JSON.stringify(t)},deleteProject:function(e){var t=JSON.parse(localStorage.projects);delete t[e],localStorage.projects=JSON.stringify(t),this.loadProject()},newProject:function(){var e=this;$(".fileOpener").remove();var t="file_"+(new Date).getTime();$(".editor").append('<input style="display:none;" nwdirectory class="fileOpener" type="file" id='+t+" />"),setTimeout(function(){$("#"+t).click()},10),$("#"+t).change(function(){var n=$("#"+t).val();if(n){if(e.checkProject(n))return void alert("Project exists");e.saveProject(n),e.loadProject()}})}},$(document).ready(function(){checkUpdate(),showTabContent(location.hash.slice(1)),editorInstance.loadProject(),setInterval("editorInstance.loadProject()",3e3),addCopy(),listenOpen(),preventDefault(),$(".editor h3 span").bind("click",function(){editorInstance.newProject()}),$("#list").on("click","li a",function(){editorInstance.openProject($(this).children("b").text())}),$("#list").on("click","li span.delete",function(e){editorInstance.deleteProject($(this).prevAll("b").text()),e.stopPropagation()})});