!function(t){"use strict";var e=t.tools,s=e.simString(t.location.pathname.replace("/","").replace(/\//g,"_")).toUpperCase(),i="AMWikiDataBase@"+s,o="AMWikiStates@"+s,a=function(){this._db=null,this._states=null,this.$e={win:$(t),searchUpdate:$("#searchUpdate"),cacheState:$("#cacheState"),cacheDocTotal:$("#cacheDocTotal"),cacheLasttime:$("#cacheLasttime")},this._bridgeLocalStorage("read"),this._bindCtrl()};return a.prototype._bridgeLocalStorage=function(e){if("read"==e){var s='{"documents":{},"lastBuild":0}';this._db=JSON.parse(t.localStorage[i]||s),"undefined"!=typeof this._db.libraries&&(this._db.documents=this._db.libraries,delete this._db.libraries)}else"save"==e&&(t.localStorage[i]=JSON.stringify(this._db))},a.prototype._bindCtrl=function(){var t=this;this.$e.win.on("beforeunload",function(){t._bridgeLocalStorage("save")})},a.prototype.update=function(t,s){var i=e.simString(t,"short");return this._db.documents[i]?this._db.documents[i].content!=s&&(this.saveDoc(t,s,i),!0):(this.saveDoc(t,s,i),!0)},a.prototype.saveDoc=function(t,e,s){this.saveDocToDB(t,e,s),this._bridgeLocalStorage("save"),this._changeSummary("sateOnly")},a.prototype.saveDocToDB=function(t,s,i){if("string"!=typeof t&&""==t)throw new Error("Error, uri must be a string!");"undefined"==typeof i&&(i=e.simString(t,"short")),this._db.documents[i]={id:i,uri:t,content:s||"",timestamp:Date.now()},this._changeSummary("sateOnly","prepare")},a.prototype.read=function(t){var s=e.simString(t,"short"),i="";return this._db.documents[s]&&(i=this._db.documents[s].content),i},a.prototype.readTime=function(t){var s=e.simString(t,"short");return this._db.documents[s]?this._db.documents[s].timestamp:0},a.prototype.remove=function(t){var s=e.simString(t,"short");delete this._db.documents[s],this._bridgeLocalStorage("save"),this._changeSummary("sateOnly")},a.prototype.increaseOpenedCount=function(t){e.simString(t,"short")},a.prototype.checkLibChange=function(t){this._indexing=t;for(var s={},i="",o=0;o<t.length;o++)i=e.simString(t[o],"short"),"undefined"!=typeof this._db.documents[i]&&(s[i]=this._db.documents[i]);this._db.documents=s,this._bridgeLocalStorage("save"),this._changeSummary()},a.prototype._changeSummary=function(e,s){var i=0;for(var o in this._db.documents)this._db.documents.hasOwnProperty(o)&&i++;"prepare"==s?this.$e.cacheState.text(parseInt(i/this._indexing.length*100-1)+"%"):this.$e.cacheState.text(parseInt(i/this._indexing.length*100)+"%"),"stateOnly"!=e&&(this.$e.cacheDocTotal.text(this._indexing.length),this._db.lastBuild?this.$e.cacheLasttime.text(t.tools.formatTime(this._db.lastBuild)):this.$e.cacheLasttime.text("0000-00-00 00:00:00"))},a.prototype.clearLibraries=function(){this._db.documents={},this._changeSummary("sateOnly")},a.prototype.saveRebuild=function(){this._db.lastBuild=Date.now(),this._bridgeLocalStorage("save"),this._changeSummary()},a.prototype.getIndexList=function(){return this._indexing},a.prototype.getAllDocs=function(){return this._db.documents},a.prototype.getLastBuildTs=function(){return this._db.lastBuild},a.prototype.getStates=function(e){return this._states||(this._states=JSON.parse(t.localStorage[o]||"{}")),this._states[e]},a.prototype.setStates=function(e,s){this._states||(this._states=JSON.parse(t.localStorage[o]||"{}")),"undefined"==typeof s?delete this._states[e]:this._states[e]=s,t.localStorage[o]=JSON.stringify(this._states)},t.AWStorage=a}(window);