(function(){var each=tinymce.each;tinymce.PluginManager.requireLangPack('templatemanager');tinymce.create('tinymce.plugins.TemplatePlugin',{init:function(ed,url){var t=this;t.editor=ed;t.contentLoaded=false;ed.addCommand('mceTemplate',function(ui){ed.windowManager.open({file:ed.getParam('site_url')+'index.php?option=com_jce&view=editor&layout=plugin&plugin=templatemanager',width:760+ed.getLang('templatemanager_delta_width',0),height:400+ed.getLang('templatemanager_delta_height',0),inline:1,popup_css:false},{plugin_url:url})});ed.onInit.add(function(){if(ed&&ed.plugins.contextmenu){ed.plugins.contextmenu.onContextMenu.add(function(th,m,e){m.add({title:'templatemanager.desc',icon_src:url+'/img/templatemanager.png',cmd:'mceTemplate'})})}});ed.addCommand('mceInsertTemplate',t._insertTemplate,t);ed.addButton('templatemanager',{title:'templatemanager.desc',image:url+'/img/templatemanager.png',cmd:'mceTemplate'});ed.onPreProcess.add(function(ed,o){var dom=ed.dom;each(dom.select('div',o.node),function(e){if(dom.hasClass(e,'mceTmpl')){each(dom.select('*',e),function(e){if(dom.hasClass(e,ed.getParam('templatemanager_mdate_classes','mdate modifieddate').replace(/\s+/g,'|')))e.innerHTML=t._getDateTime(new Date(),ed.getParam("templatemanager_mdate_format",ed.getLang("templatemanager.mdate_format")))});t._replaceVals(e)}})});if(ed.getParam('templatemanager_content_url')){ed.onInit.add(function(){if(!t.contentLoaded&&ed.getContent()==''){var u=ed.getParam('templatemanager_content_url');if(!/http(s)?:\/\//.test(u)){ed.setProgressState(true);tinymce.util.XHR.send({url:ed.settings.document_base_url+'/'+u,success:function(x){ed.setContent(x);ed.setProgressState(false);t.contentLoaded=true},error:function(e,x){ed.setProgressState(false);t.contentLoaded=true}})}}})}},getInfo:function(){return{longname:'TemplateManager plugin',author:'Moxiecode Systems AB / Ryan Demmer',authorurl:'http://www.joomlacontenteditor.net',infourl:'http://www.joomlacontenteditor.net',version:'2.0.3'}},_insertTemplate:function(ui,v){var t=this,ed=t.editor,h,el,dom=ed.dom,sel=ed.selection.getContent();h=v.content;each(t.editor.getParam('templatemanager_replace_values'),function(v,k){if(typeof(v)!='function')h=h.replace(new RegExp('\\{\\$'+k+'\\}','g'),v)});el=dom.create('div',null,h);function hasClass(n,c){return new RegExp('\\b'+c+'\\b','g').test(n.className)};each(dom.select('*',el),function(n){if(hasClass(n,ed.getParam('templatemanager_cdate_classes','cdate creationdate').replace(/\s+/g,'|')))n.innerHTML=t._getDateTime(new Date(),ed.getParam("templatemanager_cdate_format",ed.getLang("templatemanager.cdate_format")));if(hasClass(n,ed.getParam('templatemanager_mdate_classes','mdate modifieddate').replace(/\s+/g,'|')))n.innerHTML=t._getDateTime(new Date(),ed.getParam("templatemanager_mdate_format",ed.getLang("templatemanager.mdate_format")));if(hasClass(n,ed.getParam('templatemanager_selected_content_classes','selcontent').replace(/\s+/g,'|')))n.innerHTML=sel});t._replaceVals(el);ed.execCommand('mceInsertContent',false,el.innerHTML);ed.addVisual()},_replaceVals:function(e){var dom=this.editor.dom,vl=this.editor.getParam('templatemanager_replace_values');each(dom.select('*',e),function(e){each(vl,function(v,k){if(dom.hasClass(e,k)){if(typeof(vl[k])=='function')vl[k](e)}})})},_getDateTime:function(d,fmt){var ed=this.editor;if(!fmt)return"";function addZeros(value,len){var i;value=""+value;if(value.length<len){for(i=0;i<(len-value.length);i++)value="0"+value}return value}fmt=fmt.replace("%D","%m/%d/%y");fmt=fmt.replace("%r","%I:%M:%S %p");fmt=fmt.replace("%Y",""+d.getFullYear());fmt=fmt.replace("%y",""+d.getYear());fmt=fmt.replace("%m",addZeros(d.getMonth()+1,2));fmt=fmt.replace("%d",addZeros(d.getDate(),2));fmt=fmt.replace("%H",""+addZeros(d.getHours(),2));fmt=fmt.replace("%M",""+addZeros(d.getMinutes(),2));fmt=fmt.replace("%S",""+addZeros(d.getSeconds(),2));fmt=fmt.replace("%I",""+((d.getHours()+11)%12+1));fmt=fmt.replace("%p",""+(d.getHours()<12?"AM":"PM"));fmt=fmt.replace("%B",""+ed.getLang("templatemanager_months_long").split(',')[d.getMonth()]);fmt=fmt.replace("%b",""+ed.getLang("templatemanager_months_short").split(',')[d.getMonth()]);fmt=fmt.replace("%A",""+ed.getLang("templatemanager_day_long").split(',')[d.getDay()]);fmt=fmt.replace("%a",""+ed.getLang("templatemanager_day_short").split(',')[d.getDay()]);fmt=fmt.replace("%%","%");return fmt}});tinymce.PluginManager.add('templatemanager',tinymce.plugins.TemplatePlugin)})();