var FoswikiTiny={foswikiVars:null,request:null,metaTags:null,tml2html:new Array(),html2tml:new Array(),getFoswikiVar:function(name){if(FoswikiTiny.foswikiVars==null){var sets=tinyMCE.getParam("foswiki_vars","");FoswikiTiny.foswikiVars=eval(sets);}
return FoswikiTiny.foswikiVars[name];},expandVariables:function(url){for(var i in FoswikiTiny.foswikiVars){url=url.replace('%'+i+'%',FoswikiTiny.foswikiVars[i],'g');}
return url;},enableSave:function(enabled){var status=enabled?null:"disabled";var elm=document.getElementById("save");if(elm){elm.disabled=status;}
elm=document.getElementById("preview");if(elm){elm.style.display='none';elm.disabled=status;}},transform:function(editor,handler,text,onReadyToSend,onReply,onFail){var url=FoswikiTiny.getFoswikiVar("SCRIPTURL");var suffix=FoswikiTiny.getFoswikiVar("SCRIPTSUFFIX");if(suffix==null)suffix='';url+="/rest"+suffix+"/WysiwygPlugin/"+handler;var path=FoswikiTiny.getFoswikiVar("WEB")+'.'
+FoswikiTiny.getFoswikiVar("TOPIC");FoswikiTiny.request=new Object();if(tinyMCE.isIE){FoswikiTiny.request.req=new ActiveXObject("Microsoft.XMLHTTP");}else{FoswikiTiny.request.req=new XMLHttpRequest();}
FoswikiTiny.request.editor=editor;FoswikiTiny.request.req.open("POST",url,true);FoswikiTiny.request.req.setRequestHeader("Content-type","application/x-www-form-urlencoded");var params="nocache="+encodeURIComponent((new Date()).getTime())
+"&topic="+encodeURIComponent(path)
+"&text="+encodeURIComponent(text);FoswikiTiny.request.req.setRequestHeader("Content-length",params.length);FoswikiTiny.request.req.onreadystatechange=function(){if(FoswikiTiny.request.req.readyState==4){if(FoswikiTiny.request.req.status==200){onReply();}else{onFail();}}};onReadyToSend();FoswikiTiny.request.req.send(params);},initialisedFromServer:false,setUpContent:function(editor_id,body,doc){if(FoswikiTiny.initialisedFromServer)return;var editor=tinyMCE.getInstanceById(editor_id);FoswikiTiny.switchToWYSIWYG(editor);FoswikiTiny.initialisedFromServer=true;},switchToRaw:function(inst){inst.triggerSave(false,true);var text=inst.oldTargetElement.value;for(var i=0;i<FoswikiTiny.html2tml.length;i++){var cb=FoswikiTiny.html2tml[i];text=cb.apply(inst,[inst,text]);}
FoswikiTiny.transform(inst,"html2tml",text,function(){FoswikiTiny.enableSave(false);var te=FoswikiTiny.request.editor.oldTargetElement;te.value="Please wait... retrieving page from server";},function(){var te=FoswikiTiny.request.editor.oldTargetElement;var text=FoswikiTiny.request.req.responseText;te.value=text;FoswikiTiny.enableSave(true);},function(){var te=FoswikiTiny.request.editor.oldTargetElement;te.value="There was a problem retrieving the page: "
+FoswikiTiny.request.req.statusText;});var eid=inst.editorId;var id=eid+"_2WYSIWYG";var el=document.getElementById(id);if(el){el.style.display="block";}else{el=document.createElement('INPUT');el.id=id;el.type="button";el.value="WYSIWYG";el.className="foswikiButton";el.onclick=function(){tinyMCE.execCommand("mceToggleEditor",null,inst.editorId);return false;}
var pel=inst.oldTargetElement.parentNode;pel.insertBefore(el,inst.oldTargetElement);}
inst.oldTargetElement.onchange=function(){var inst=tinyMCE.getInstanceById(eid);inst.isNotDirty=false;return true;}},switchToWYSIWYG:function(editor){editor.oldTargetElement.onchange=null;FoswikiTiny.transform(editor,"tml2html",editor.oldTargetElement.value,function(){FoswikiTiny.enableSave(false);var editor=FoswikiTiny.request.editor;tinyMCE.setInnerHTML(FoswikiTiny.request.editor.getBody(),"<span class='foswikiAlert'>Please wait... retrieving page from server.</span>");},function(){var text=FoswikiTiny.request.req.responseText;for(var i=0;i<FoswikiTiny.tml2html.length;i++){var cb=FoswikiTiny.tml2html[i];text=cb.apply(editor,[editor,text]);}
tinyMCE.setInnerHTML(FoswikiTiny.request.editor.getBody(),text);FoswikiTiny.request.editor.isNotDirty=true;FoswikiTiny.enableSave(true);},function(){tinyMCE.setInnerHTML(FoswikiTiny.request.editor.getBody(),"<div class='foswikiAlert'>"
+"There was a problem retrieving the page: "
+FoswikiTiny.request.req.statusText+"</div>");});var id=editor.editorId+"_2WYSIWYG";var el=document.getElementById(id);if(el){el.style.display="none";}},saveCallback:function(editor_id,html,body){var editor=tinyMCE.getInstanceById(editor_id);for(var i=0;i<FoswikiTiny.html2tml.length;i++){var cb=FoswikiTiny.html2tml[i];html=cb.apply(editor,[editor,html]);}
var secret_id=tinyMCE.getParam('foswiki_secret_id');if(secret_id!=null&&html.indexOf('<!--'+secret_id+'-->')==-1){html='<!--'+secret_id+'-->'+html;}
return html;},convertLink:function(url,node,onSave){if(onSave==null)
onSave=false;var orig=url;var pubUrl=FoswikiTiny.getFoswikiVar("PUBURL");var vsu=FoswikiTiny.getFoswikiVar("VIEWSCRIPTURL");url=FoswikiTiny.expandVariables(url);if(onSave){if((url.indexOf(pubUrl+'/')!=0)&&(url.indexOf(vsu+'/')==0)){url=url.substr(vsu.length+1);url=url.replace(/\/+/g,'.');if(url.indexOf(FoswikiTiny.getFoswikiVar('WEB')+'.')==0){url=url.substr(FoswikiTiny.getFoswikiVar('WEB').length+1);}}}else{if(url.indexOf('/')==-1){var match=/^((?:\w+\.)*)(\w+)$/.exec(url);if(match!=null){var web=match[1];var topic=match[2];if(web==null||web.length==0){web=FoswikiTiny.getFoswikiVar("WEB");}
web=web.replace(/\.+/g,'/');web=web.replace(/\/+$/,'');url=vsu+'/'+web+'/'+topic;}}}
return url;},convertPubURL:function(url){var orig=url;var base=FoswikiTiny.getFoswikiVar("PUBURL")+'/'
+FoswikiTiny.getFoswikiVar("WEB")+'/'
+FoswikiTiny.getFoswikiVar("TOPIC")+'/';url=FoswikiTiny.expandVariables(url);if(url.indexOf('/')==-1){url=base+url;}
return url;},getMetaTag:function(inKey){if(FoswikiTiny.metaTags==null||FoswikiTiny.metaTags.length==0){var head=document.getElementsByTagName("META");head=head[0].parentNode.childNodes;FoswikiTiny.metaTags=new Array();for(var i=0;i<head.length;i++){if(head[i].tagName!=null&&head[i].tagName.toUpperCase()=='META'){FoswikiTiny.metaTags[head[i].name]=head[i].content;}}}
return FoswikiTiny.metaTags[inKey];},install:function(){var tmce_init=FoswikiTiny.getMetaTag('TINYMCEPLUGIN_INIT');if(tmce_init!=null){eval("tinyMCE.init({"+unescape(tmce_init)+"});");return;}
alert("Unable to install TinyMCE; <META name='TINYMCEPLUGIN_INIT' is missing");}};