var TEH_PATH="/js/lib/textile/js/";
var TEH_IMAGE_PATH="/js/lib/textile/images/";
function TextileEditorButton(id,display,tagStart,tagEnd,access,title,sve,open,cssClasses){this.id=id;
this.display=display;
this.tagStart=tagStart;
this.tagEnd=tagEnd;
this.access=access;
this.title=title;
this.sve=sve;
this.open=open;
this.standard=true;
this.cssClasses=cssClasses||"standard"
}function TextileEditorButtonSeparator(sve){this.separator=true;
this.sve=sve
}var TextileEditor=Class.create();
TextileEditor.buttons=new Array();
TextileEditor.Methods={initialize:function(canvas,view){var toolbar=document.createElement("div");
toolbar.id="textile-toolbar-"+canvas;
toolbar.className="textile-toolbar";
this.canvas=document.getElementById(canvas);
this.canvas.parentNode.insertBefore(toolbar,this.canvas);
this.openTags=new Array();
var edButtons=new Array();
edButtons=this.buttons;
var standardButtons=new Array();
for(var i=0;
i<edButtons.length;
i++){var thisButton=this.prepareButton(edButtons[i]);
if(view=="s"){if(edButtons[i].sve=="s"){toolbar.appendChild(thisButton);
standardButtons.push(thisButton)
}}else{if(typeof thisButton=="string"){toolbar.innerHTML+=thisButton
}else{toolbar.appendChild(thisButton);
standardButtons.push(thisButton)
}}}var te=this;
$A(toolbar.getElementsByTagName("button")).each(function(button){if(!button.onclick){button.onclick=function(){te.insertTag(button);
return false
}
}button.tagStart=button.getAttribute("tagStart");
button.tagEnd=button.getAttribute("tagEnd");
button.open=button.getAttribute("open");
button.textile_editor=te;
button.canvas=te.canvas
})
},prepareButton:function(button){if(button.separator){var theButton=document.createElement("span");
theButton.className="ed_sep";
return theButton
}if(button.standard){var theButton=document.createElement("button");
theButton.id=button.id;
theButton.setAttribute("class",button.cssClasses);
theButton.setAttribute("tagStart",button.tagStart);
theButton.setAttribute("tagEnd",button.tagEnd);
theButton.setAttribute("open",button.open);
if(button.display.match(/\.(png|gif)$/)){var img=document.createElement("img");
img.src=TEH_IMAGE_PATH+button.display;
theButton.appendChild(img)
}else{$(theButton).update(button.display)
}}else{return button
}theButton.accessKey=button.access;
theButton.title=button.title;
return theButton
},addTag:function(button){if(button.tagEnd!=""){this.openTags[this.openTags.length]=button;
$(button).addClassName("selected")
}},removeTag:function(button){for(i=0;
i<this.openTags.length;
i++){if(this.openTags[i]==button){this.openTags.splice(button,1);
$(button).removeClassName("selected")
}}},checkOpenTags:function(button){var tag=0;
for(i=0;
i<this.openTags.length;
i++){if(this.openTags[i]==button){tag++
}}if(tag>0){return true
}else{return false
}},insertTag:function(button,tagStart,tagEnd){var myField=button.canvas;
myField.focus();
if(tagStart){button.tagStart=tagStart;
button.tagEnd=tagEnd?tagEnd:"\n"
}var textSelected=false;
var finalText="";
var FF=false;
if(document.selection){sel=document.selection.createRange();
var beginningText="";
var followupText="";
var selectedText=sel.text;
var removedLength=0;
selectedText.replace(/\s+$/,function(match,offset,s){removedLength=match.length;
return""
});
sel.moveEnd("character",-removedLength);
selectedText=sel.text;
if(sel.text.length>0){textSelected=true
}var newlineReplaceRegexClean=/\r\n\s\n/g;
var newlineReplaceRegexDirty="\\r\\n\\s\\n";
var newlineReplaceClean="\r\n\n"
}else{if(myField.selectionStart||myField.selectionStart=="0"){var startPos=myField.selectionStart;
var endPos=myField.selectionEnd;
var cursorPos=endPos;
var scrollTop=myField.scrollTop;
FF=true;
var beginningText=myField.value.substring(0,startPos);
var followupText=myField.value.substring(endPos,myField.value.length);
if(startPos!=endPos){textSelected=true;
var selectedText=myField.value.substring(startPos,endPos)
}var newlineReplaceRegexClean=/\n\n/g;
var newlineReplaceRegexDirty="\\n\\n";
var newlineReplaceClean="\n\n"
}}if(textSelected){var newlineStart="";
var newlineStartPos=0;
var newlineEnd="";
var newlineEndPos=0;
var newlineFollowup="";
var posDiffPos=0;
var posDiffNeg=0;
var mplier=1;
if(selectedText.match(/^\n/)){selectedText=selectedText.replace(/^\n/,"");
newlineStart="\n";
newlineStartpos=1
}if(selectedText.match(/\n$/g)){selectedText=selectedText.replace(/\n$/g,"");
newlineEnd="\n";
newlineEndPos=1
}if(followupText.match(/^\n/)){newlineFollowup=""
}else{newlineFollowup="\n\n"
}if((button.tagStart==" * ")||(button.tagStart==" # ")){listItems=0;
re_start=new RegExp("^ (\\*|\\#) ","g");
if(button.tagStart==" # "){re_tag=new RegExp(" \\# ","g")
}else{re_tag=new RegExp(" \\* ","g")
}re_replace=new RegExp(" (\\*|\\#) ","g");
re_word_bullet_m_s=new RegExp("• ","g");
re_word_bullet_m_f=new RegExp("∑ ","g");
selectedText=selectedText.replace(re_word_bullet_m_s,"").replace(re_word_bullet_m_f,"");
if(selectedText.match(re_start)){if(selectedText.match(re_tag)){finalText=beginningText+newlineStart+selectedText.replace(re_replace,"")+newlineEnd+followupText;
if(matches=selectedText.match(/ (\*|\#) /g)){listItems=matches.length
}posDiffNeg=listItems*3
}else{finalText=beginningText+newlineStart+selectedText.replace(re_replace,button.tagStart)+newlineEnd+followupText
}}else{finalText=beginningText+newlineStart+button.tagStart+selectedText.replace(newlineReplaceRegexClean,newlineReplaceClean+button.tagStart).replace(/\n(\S)/g,"\n"+button.tagStart+"$1")+newlineEnd+followupText;
if(matches=selectedText.match(/\n(\S)/g)){listItems=matches.length
}posDiffPos=3+listItems*3
}}else{if(button.tagStart.match(/^(h1|h2|h3|h4|h5|h6|bq|p|\>|\<\>|\<|\=|\(|\))/g)){var insertTag="";
var insertModifier="";
var tagPartBlock="";
var tagPartModifier="";
var tagPartModifierOrig="";
var drawSwitch="";
var captureIndentStart=false;
var captureListStart=false;
var periodAddition="\\. ";
var periodAdditionClean=". ";
var listItemsAddition=0;
var re_list_items=new RegExp("(\\*+|\\#+)","g");
var re_block_modifier=new RegExp("^(h1|h2|h3|h4|h5|h6|bq|p| [\\*]{1,} | [\\#]{1,} |)(\\>|\\<\\>|\\<|\\=|[\\(]{1,}|[\\)]{1,6}|)","g");
if(tagPartMatches=re_block_modifier.exec(selectedText)){tagPartBlock=tagPartMatches[1];
tagPartModifier=tagPartMatches[2];
tagPartModifierOrig=tagPartMatches[2];
tagPartModifierOrig=tagPartModifierOrig.replace(/\(/g,"\\(")
}if(tagPartBlock==button.tagStart){insertTag=tagPartBlock+tagPartModifierOrig;
drawSwitch=0
}else{if((tagPartModifier==button.tagStart)||(newm=tagPartModifier.match(/[\(]{2,}/g))){if((button.tagStart=="(")||(button.tagStart==")")){var indentLength=tagPartModifier.length;
if(button.tagStart=="("){indentLength=indentLength+1
}else{indentLength=indentLength-1
}for(var i=0;
i<indentLength;
i++){insertModifier=insertModifier+"("
}insertTag=tagPartBlock+insertModifier
}else{if(button.tagStart==tagPartModifier){insertTag=tagPartBlock
}else{if(button.tagStart.match(/(\>|\<\>|\<|\=)/g)){insertTag=tagPartBlock+button.tagStart
}else{insertTag=button.tagStart+tagPartModifier
}}}drawSwitch=1
}else{if(listPartMatches=re_list_items.exec(tagPartBlock)){var listTypeMatch=listPartMatches[1];
var indentLength=tagPartBlock.length-2;
var listInsert="";
if(button.tagStart=="("){indentLength=indentLength+1
}else{indentLength=indentLength-1
}if(listTypeMatch.match(/[\*]{1,}/g)){var listType="*";
var listReplace="\\*"
}else{var listType="#";
var listReplace="\\#"
}for(var i=0;
i<indentLength;
i++){listInsert=listInsert+listType
}if(listInsert!=""){insertTag=" "+listInsert+" "
}else{insertTag=""
}tagPartBlock=tagPartBlock.replace(/(\*|\#)/g,listReplace);
drawSwitch=1;
captureListStart=true;
periodAddition="";
periodAdditionClean="";
if(matches=selectedText.match(/\n\s/g)){listItemsAddition=matches.length
}}else{if(button.tagStart.match(/(h1|h2|h3|h4|h5|h6|bq|p)/g)){if(tagPartBlock==""){drawSwitch=2
}else{drawSwitch=1
}insertTag=button.tagStart+tagPartModifier
}else{if((tagPartModifier=="")&&(tagPartBlock!="")){drawSwitch=1
}else{if(tagPartModifier==""){drawSwitch=2
}else{drawSwitch=1
}}if(tagPartBlock==""){tagPartBlock="p"
}if(button.tagStart==")"){tagPartModifier=""
}else{tagPartModifier=button.tagStart;
captureIndentStart=true
}insertTag=tagPartBlock+tagPartModifier
}}}}mplier=0;
if(captureListStart||(tagPartModifier.match(/[\(\)]{1,}/g))){re_start=new RegExp(insertTag.escape+periodAddition,"g")
}else{re_start=new RegExp(insertTag+periodAddition,"g")
}re_old=new RegExp(tagPartBlock+tagPartModifierOrig+periodAddition,"g");
re_middle=new RegExp(newlineReplaceRegexDirty+insertTag.escape+periodAddition.escape,"g");
re_tag=new RegExp(insertTag.escape+periodAddition.escape,"g");
if((drawSwitch==0)||(drawSwitch==1)){if(drawSwitch==0){finalText=beginningText+newlineStart+selectedText.replace(re_start,"").replace(re_middle,newlineReplaceClean)+newlineEnd+followupText;
if(matches=selectedText.match(newlineReplaceRegexClean)){mplier=mplier+matches.length
}posDiffNeg=insertTag.length+2+(mplier*4)
}else{finalText=beginningText+newlineStart+selectedText.replace(re_old,insertTag+periodAdditionClean)+newlineEnd+followupText;
if(matches=selectedText.match(newlineReplaceRegexClean)){mplier=mplier+matches.length
}if(captureIndentStart){tagPreviousLength=tagPartBlock.length;
tagCurrentLength=insertTag.length
}else{if(captureListStart){if(button.tagStart=="("){tagPreviousLength=listTypeMatch.length+2;
tagCurrentLength=insertTag.length+listItemsAddition
}else{if(insertTag.match(/(\*|\#)/g)){tagPreviousLength=insertTag.length+listItemsAddition;
tagCurrentLength=listTypeMatch.length
}else{tagPreviousLength=insertTag.length+listItemsAddition;
tagCurrentLength=listTypeMatch.length-(3*listItemsAddition)-1
}}}else{tagPreviousLength=tagPartBlock.length+tagPartModifier.length;
tagCurrentLength=insertTag.length
}}if(tagCurrentLength>tagPreviousLength){posDiffPos=(tagCurrentLength-tagPreviousLength)+(mplier*(tagCurrentLength-tagPreviousLength))
}else{posDiffNeg=(tagPreviousLength-tagCurrentLength)+(mplier*(tagPreviousLength-tagCurrentLength))
}}}else{finalText=beginningText+newlineStart+insertTag+". "+selectedText.replace(newlineReplaceRegexClean,button.tagEnd+"\n"+insertTag+". ")+newlineFollowup+newlineEnd+followupText;
if(matches=selectedText.match(newlineReplaceRegexClean)){mplier=mplier+matches.length
}posDiffPos=insertTag.length+2+(mplier*4)
}}else{mplier=1;
re_start=new RegExp("^\\"+button.tagStart,"g");
re_end=new RegExp("\\"+button.tagEnd+"$","g");
re_middle=new RegExp("\\"+button.tagEnd+newlineReplaceRegexDirty+"\\"+button.tagStart,"g");
if(selectedText.match(re_start)&&selectedText.match(re_end)){finalText=beginningText+newlineStart+selectedText.replace(re_start,"").replace(re_end,"").replace(re_middle,newlineReplaceClean)+newlineEnd+followupText;
if(matches=selectedText.match(newlineReplaceRegexClean)){mplier=mplier+matches.length
}posDiffNeg=button.tagStart.length*mplier+button.tagEnd.length*mplier
}else{finalText=beginningText+newlineStart+button.tagStart+selectedText.replace(newlineReplaceRegexClean,button.tagEnd+newlineReplaceClean+button.tagStart)+button.tagEnd+newlineEnd+followupText;
if(matches=selectedText.match(newlineReplaceRegexClean)){mplier=mplier+matches.length
}posDiffPos=(button.tagStart.length*mplier)+(button.tagEnd.length*mplier)
}}}cursorPos+=button.tagStart.length+button.tagEnd.length
}else{if(button.tagStart==null){return 
}var buttonStart="";
var buttonEnd="";
var re_p=new RegExp("(\\<|\\>|\\=|\\<\\>|\\(|\\))","g");
var re_h=new RegExp("^(h1|h2|h3|h4|h5|h6|p|bq)","g");
if(!this.checkOpenTags(button)||button.tagEnd==""){if(button.tagStart.match(re_h)){buttonStart=button.tagStart+". "
}else{buttonStart=button.tagStart
}if(button.tagStart.match(re_p)){finalText=beginningText+followupText;
cursorPos=startPos
}else{finalText=beginningText+buttonStart+followupText;
this.addTag(button);
cursorPos=startPos+buttonStart.length
}}else{if(button.tagStart.match(re_p)){buttonEnd="\n\n"
}else{if(button.tagStart.match(re_h)){buttonEnd="\n\n"
}else{buttonEnd=button.tagEnd
}}finalText=beginningText+button.tagEnd+followupText;
this.removeTag(button);
cursorPos=startPos+button.tagEnd.length
}}if(FF==true){myField.value=finalText;
myField.scrollTop=scrollTop
}else{sel.text=finalText
}if(textSelected){myField.selectionStart=startPos+newlineStartPos;
myField.selectionEnd=endPos+posDiffPos-posDiffNeg-newlineEndPos
}else{myField.selectionStart=cursorPos;
myField.selectionEnd=cursorPos
}}};
Object.extend(TextileEditor,TextileEditor.Methods);
document.write('<script src="'+TEH_PATH+'textile-editor-config.js" type="text/javascript"><\/script>');
