tinymce.PluginManager.add("template",function(e){function t(){function t(e){var t=e.control.value();t.url?tinymce.util.XHR.send({url:t.url,success:function(e){a=e,n.find("iframe")[0].html(e)}}):(a=t.content,n.find("iframe")[0].html(t.content)),n.find("#description")[0].text(e.control.value().description)}var n,a,r=[];return e.settings.templates?(tinymce.each(e.settings.templates,function(e){r.push({selected:!r.length,text:e.title,value:{url:e.url,content:e.content,description:e.description}})}),n=e.windowManager.open({title:"Insert template",body:[{type:"listbox",name:"template",flex:0,label:"Templates",values:r,onselect:t},{type:"label",name:"description",label:"Description",text:" "},{type:"iframe",minWidth:600,minHeight:400,border:1}],onsubmit:function(){i(!1,a)}}),n.find("listbox")[0].fire("select"),void 0):(e.windowManager.alert("No templates defined"),void 0)}function n(t,n){function a(e,t){if(e=""+e,e.length<t)for(var n=0;n<t-e.length;n++)e="0"+e;return e}var i="Sun Mon Tue Wed Thu Fri Sat Sun".split(" "),r="Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday".split(" "),o="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),l="January February March April May June July August September October November December".split(" ");return n=n||new Date,t=t.replace("%D","%m/%d/%Y"),t=t.replace("%r","%I:%M:%S %p"),t=t.replace("%Y",""+n.getFullYear()),t=t.replace("%y",""+n.getYear()),t=t.replace("%m",a(n.getMonth()+1,2)),t=t.replace("%d",a(n.getDate(),2)),t=t.replace("%H",""+a(n.getHours(),2)),t=t.replace("%M",""+a(n.getMinutes(),2)),t=t.replace("%S",""+a(n.getSeconds(),2)),t=t.replace("%I",""+((n.getHours()+11)%12+1)),t=t.replace("%p",""+(n.getHours()<12?"AM":"PM")),t=t.replace("%B",""+e.translate(l[n.getMonth()])),t=t.replace("%b",""+e.translate(o[n.getMonth()])),t=t.replace("%A",""+e.translate(r[n.getDay()])),t=t.replace("%a",""+e.translate(i[n.getDay()])),t=t.replace("%%","%")}function a(t){var n=e.dom,a=e.getParam("template_replace_values");r(n.select("*",t),function(e){r(a,function(t,i){n.hasClass(e,i)&&"function"==typeof a[i]&&a[i](e)})})}function i(t,i){function o(e,t){return new RegExp("\\b"+t+"\\b","g").test(e.className)}var l,c,s=e.dom,d=e.selection.getContent();r(e.getParam("template_replace_values"),function(e,t){"function"!=typeof e&&(i=i.replace(new RegExp("\\{\\$"+t+"\\}","g"),e))}),l=s.create("div",null,i),c=s.select(".mceTmpl",l),c&&c.length>0&&(l=s.create("div",null),l.appendChild(c[0].cloneNode(!0))),r(s.select("*",l),function(t){o(t,e.getParam("template_cdate_classes","cdate").replace(/\s+/g,"|"))&&(t.innerHTML=n(e.getParam("template_cdate_format",e.getLang("template.cdate_format")))),o(t,e.getParam("template_mdate_classes","mdate").replace(/\s+/g,"|"))&&(t.innerHTML=n(e.getParam("template_mdate_format",e.getLang("template.mdate_format")))),o(t,e.getParam("template_selected_content_classes","selcontent").replace(/\s+/g,"|"))&&(t.innerHTML=d)}),a(l),e.execCommand("mceInsertContent",!1,l.innerHTML),e.addVisual()}var r=tinymce.each;e.addCommand("mceInsertTemplate",i),e.addButton("template",{title:"Insert template",onclick:t}),e.addMenuItem("template",{text:"Insert template",onclick:t,context:"insert"}),e.on("PreProcess",function(t){var i=e.dom;r(i.select("div",t.node),function(t){i.hasClass(t,"mceTmpl")&&(r(i.select("*",t),function(t){i.hasClass(t,e.getParam("template_mdate_classes","mdate").replace(/\s+/g,"|"))&&(t.innerHTML=n(e.getParam("template_mdate_format",e.getLang("template.mdate_format"))))}),a(t))})})});