(function(b){var g="autosave",d="restoredraft",f=true,c,a,e=b.util.Dispatcher;b.create("tinymce.plugins.AutoSave",{init:function(k,l){var j=this,i=k.settings;j.editor=k;function h(m){var n={s:1000,m:60000};m=/^(\d+)([ms]?)$/.exec(""+m);return(m[2]?n[m[2]]:1)*parseInt(m)}b.each({ask_before_unload:f,interval:"30s",retention:"20m",minlength:50},function(m,n){n=g+"_"+n;if(i[n]===c){i[n]=m}});i.autosave_interval=h(i.autosave_interval);i.autosave_retention=h(i.autosave_retention);k.addButton(d,{title:g+".restore_content",onclick:function(){if(k.getContent().replace(/\s|&nbsp;|<\/?p[^>]*>|<br[^>]*>/gi,"").length>0){k.windowManager.confirm(g+".warning_message",function(m){if(m){j.restoreDraft()}})}else{j.restoreDraft()}}});k.onNodeChange.add(function(){var m=k.controlManager;if(m.get(d)){m.setDisabled(d,!j.hasDraft())}});k.onInit.add(function(){if(k.controlManager.get(d)){j.setupStorage(k);setInterval(function(){j.storeDraft();k.nodeChanged()},i.autosave_interval)}});j.onStoreDraft=new e(j);j.onRestoreDraft=new e(j);j.onRemoveDraft=new e(j);if(!a){window.onbeforeunload=b.plugins.AutoSave._beforeUnloadHandler;a=f}},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:b.majorVersion+"."+b.minorVersion}},getExpDate:function(){return new Date(new Date().getTime()+this.editor.settings.autosave_retention).toUTCString()},setupStorage:function(k){var j=this,i=g+"_test",h="OK";j.key=g+k.id;b.each([function(){if(localStorage){localStorage.setItem(i,h);if(localStorage.getItem(i)===h){localStorage.removeItem(i);return localStorage}}},function(){if(sessionStorage){sessionStorage.setItem(i,h);if(sessionStorage.getItem(i)===h){sessionStorage.removeItem(i);return sessionStorage}}},function(){if(b.isIE){k.getElement().style.behavior="url('#default#userData')";return{autoExpires:f,setItem:function(m,l){var n=k.getElement();n.setAttribute(m,l);n.expires=j.getExpDate();n.save("TinyMCE")},getItem:function(l){var m=k.getElement();m.load("TinyMCE");return m.getAttribute(l)},removeItem:function(l){k.getElement().removeAttribute(l)}}}},],function(l){try{j.storage=l();if(j.storage){return false}}catch(m){}})},storeDraft:function(){var k=this,i=k.storage,l=k.editor,j,h;if(i){if(!i.getItem(k.key)&&!l.isDirty()){return}h=l.getContent();if(h.length>l.settings.autosave_minlength){j=k.getExpDate();if(!k.storage.autoExpires){k.storage.setItem(k.key+"_expires",j)}k.storage.setItem(k.key,h);k.onStoreDraft.dispatch(k,{expires:j,content:h})}}},restoreDraft:function(){var h=this,i=h.storage;if(i){content=i.getItem(h.key);if(content){h.editor.setContent(content);h.onRestoreDraft.dispatch(h,{content:content})}}},hasDraft:function(){var j=this,i=j.storage,k,h;if(i){h=!!i.getItem(j.key);if(h){if(!j.storage.autoExpires){k=new Date(i.getItem(j.key+"_expires"));if(new Date().getTime()<k.getTime()){return f}j.removeDraft()}else{return f}}}return false},removeDraft:function(){var j=this,i=j.storage,k=j.key,h;if(i){h=i.getItem(k);i.removeItem(k);i.removeItem(k+"_expires");if(h){j.onRemoveDraft.dispatch(j,{content:h})}}},"static":{_beforeUnloadHandler:function(h){var i;b.each(tinyMCE.editors,function(j){if(j.plugins.autosave){j.plugins.autosave.storeDraft()}if(j.getParam("fullscreen_is_enabled")){return}if(!i&&j.isDirty()&&j.getParam("autosave_ask_before_unload")){i=j.getLang("autosave.unload_msg")}});return i}}});b.PluginManager.add("autosave",b.plugins.AutoSave)})(tinymce);