!function(e){var t,n="autosave",o="restoredraft",r=!0,a=e.util.Dispatcher;e.create("tinymce.plugins.AutoSave",{init:function(n,i){var s=this,u=n.settings;function g(e){return((e=/^(\d+)([ms]?)$/.exec(""+e))[2]?{s:1e3,m:6e4}[e[2]]:1)*parseInt(e)}s.editor=n,e.each({ask_before_unload:r,interval:"30s",retention:"20m",minlength:50},(function(e,t){undefined===u[t="autosave_"+t]&&(u[t]=e)})),u.autosave_interval=g(u.autosave_interval),u.autosave_retention=g(u.autosave_retention),n.addButton(o,{title:"autosave.restore_content",onclick:function(){n.getContent({draft:!0}).replace(/\s|&nbsp;|<\/?p[^>]*>|<br[^>]*>/gi,"").length>0?n.windowManager.confirm("autosave.warning_message",(function(e){e&&s.restoreDraft()})):s.restoreDraft()}}),n.onNodeChange.add((function(){var e=n.controlManager;e.get(o)&&e.setDisabled(o,!s.hasDraft())})),n.onInit.add((function(){n.controlManager.get(o)&&(s.setupStorage(n),setInterval((function(){n.removed||(s.storeDraft(),n.nodeChanged())}),u.autosave_interval))})),s.onStoreDraft=new a(s),s.onRestoreDraft=new a(s),s.onRemoveDraft=new a(s),t||(window.onbeforeunload=e.plugins.AutoSave._beforeUnloadHandler,t=r)},getInfo:function(){return{longname:"Auto save",author:"Moxiecode Systems AB",authorurl:"http://tinymce.moxiecode.com",infourl:"http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autosave",version:e.majorVersion+"."+e.minorVersion}},getExpDate:function(){return new Date((new Date).getTime()+this.editor.settings.autosave_retention).toUTCString()},setupStorage:function(t){var o=this,a="autosave_test",i="OK";o.key=n+t.id,e.each([function(){if(localStorage&&(localStorage.setItem(a,i),localStorage.getItem(a)===i))return localStorage.removeItem(a),localStorage},function(){if(sessionStorage&&(sessionStorage.setItem(a,i),sessionStorage.getItem(a)===i))return sessionStorage.removeItem(a),sessionStorage},function(){if(e.isIE)return t.getElement().style.behavior="url('#default#userData')",{autoExpires:r,setItem:function(e,n){var r=t.getElement();r.setAttribute(e,n),r.expires=o.getExpDate();try{r.save("TinyMCE")}catch(e){}},getItem:function(e){var n=t.getElement();try{return n.load("TinyMCE"),n.getAttribute(e)}catch(e){return null}},removeItem:function(e){t.getElement().removeAttribute(e)}}}],(function(e){try{if(o.storage=e(),o.storage)return!1}catch(e){}}))},storeDraft:function(){var e,t,n=this,o=n.storage,r=n.editor;if(o){if(!o.getItem(n.key)&&!r.isDirty())return;(t=r.getContent({draft:!0})).length>r.settings.autosave_minlength&&(e=n.getExpDate(),n.storage.autoExpires||n.storage.setItem(n.key+"_expires",e),n.storage.setItem(n.key,t),n.onStoreDraft.dispatch(n,{expires:e,content:t}))}},restoreDraft:function(){var e,t=this,n=t.storage;n&&(e=n.getItem(t.key))&&(t.editor.setContent(e),t.onRestoreDraft.dispatch(t,{content:e}))},hasDraft:function(){var e,t=this,n=t.storage;if(n&&!!n.getItem(t.key)){if(t.storage.autoExpires)return r;if(e=new Date(n.getItem(t.key+"_expires")),(new Date).getTime()<e.getTime())return r;t.removeDraft()}return!1},removeDraft:function(){var e,t=this,n=t.storage,o=t.key;n&&(e=n.getItem(o),n.removeItem(o),n.removeItem(o+"_expires"),e&&t.onRemoveDraft.dispatch(t,{content:e}))},static:{_beforeUnloadHandler:function(t){var n;return e.each(tinyMCE.editors,(function(e){e.plugins.autosave&&e.plugins.autosave.storeDraft(),e.getParam("fullscreen_is_enabled")||!n&&e.isDirty()&&e.getParam("autosave_ask_before_unload")&&(n=e.getLang("autosave.unload_msg"))})),n}}}),e.PluginManager.add("autosave",e.plugins.AutoSave)}(tinymce);
