function BBCode(a){this.construct(a)}BBCode.prototype={VK_TAB:9,VK_ENTER:13,VK_PAGE_UP:33,BRK_OP:'[',BRK_CL:']',textarea:null,stext:'',quoter:null,collapseAfterInsert:false,replaceOnInsert:false,construct:function(a){this.textarea=a;this.tags=new Object();this.addTag('_quoter',function(){return'[quote="'+b.quoter+'"]'},'[/quote]\n',null,null,function(){b.collapseAfterInsert=true;return b._prepareMultiline(b.quoterText)});var b=this;addEvent(a,'onkeydown',function(e){return b.onKeyPress(e,window.HTMLElement?'down':'press')});addEvent(a,'onkeypress',function(e){return b.onKeyPress(e,'press')})},onclickPoster:function(a){var b=this.getSelection()[0];if(b){this.quoter=a;this.quoterText=b;this.insertTag('_quoter')}else{this.insertAtCursor("[b]"+a+'[/b]\n')}return false},onclickQuoteSel:function(){var a=this.getSelection()[0];if(a){this.insertAtCursor('[quote]'+a+'[/quote]\n')}else{alert('Please select text.')}return false},emoticon:function(a){if(a){this.insertAtCursor(' '+a+' ')}else{return false}return false},refreshSelection:function(a){if(a)this.stext=this.getSelection()[0];else this.stext=''},getSelection:function(){var w=window;var a='',range;if(w.getSelection){a=w.getSelection()}else if(w.document.getSelection){a=w.document.getSelection()}else if(w.document.selection&&w.document.selection.createRange){range=w.document.selection.createRange();a=range.text}else{return[null,null]}if(a=='')a=this.stext;a=""+a;a=a.replace("/^\s+|\s+$/g","");return[a,range]},insertAtCursor:function(a){var t=this.textarea;t.focus();if(document.selection&&document.selection.createRange){var r=document.selection.createRange();if(!this.replaceOnInsert)r.collapse();r.text=a}else if(t.setSelectionRange){var b=this.replaceOnInsert?t.selectionStart:t.selectionEnd;var c=t.selectionEnd;var d=t.value.substr(0,b);var e=t.value.substr(c);t.value=d+a+e;t.setSelectionRange(b+a.length,b+a.length)}else{t.value+=a}setTimeout(function(){t.focus()},100)},surround:function(a,b,c){var t=this.textarea;t.focus();if(!c)c=function(t){return t};var d=this.getSelection();var e=d[0];var f=d[1];if(e==null)return false;var g=e!=null&&e!='';if(f){var g=e!=null&&e!='';var h=a+c(e)+(b?b:'');f.text=h;f.collapse();if(e!=''){var j=0;for(var i=0;i<h.length;i++)if(h.charAt(i)=='\r')j++;f.moveStart("character",-b.length-e.length-a.length+j);f.moveEnd("character",-0)}else{f.moveEnd("character",-b.length)}if(!this.collapseAfterInsert)f.select()}else if(t.setSelectionRange){var k=t.selectionStart;var l=t.selectionEnd;var m=t.value.substr(0,k);var n=t.value.substr(l);var o=c(t.value.substr(k,l-k));var p=a+o+b;t.value=m+p+n;if(o!=''){t.setSelectionRange(k,k+p.length);g=true}else{t.setSelectionRange(k+a.length,k+a.length);g=false}if(this.collapseAfterInsert)t.setSelectionRange(k+p.length,k+p.length)}else{t.value+=a+e+b}this.collapseAfterInsert=false;return g},_cancelEvent:function(e){if(e.preventDefault)e.preventDefault();if(e.stopPropagation)e.stopPropagation();return e.returnValue=false},onKeyPress:function(e,a){var b=String.fromCharCode(e.keyCode?e.keyCode:e.charCode);for(var c in this.tags){var d=this.tags[c];if(d.ctrlKey&&!e[d.ctrlKey+"Key"])continue;if(!d.key||b.toUpperCase()!=d.key.toUpperCase())continue;if(e.type=="keydown")this.insertTag(c);return this._cancelEvent(e)}if(a=='press'&&e.keyCode==this.VK_TAB&&!e.shiftKey&&!e.ctrlKey&&!e.altKey){this.surround("\t","");return this._cancelEvent(e)}if(e.keyCode==this.VK_TAB&&!e.shiftKey&&e.ctrlKey&&!e.altKey){this.textarea.form.post.focus();return this._cancelEvent(e)}var f=this.textarea.form;var g=null;if(e.keyCode==this.VK_PAGE_UP&&e.shiftKey&&!e.ctrlKey&&e.altKey)g=f.add_attachment_box;if(e.keyCode==this.VK_ENTER&&!e.shiftKey&&!e.ctrlKey&&e.altKey)g=f.preview;if(e.keyCode==this.VK_ENTER&&!e.shiftKey&&e.ctrlKey&&!e.altKey)g=f.post;if(g){g.click();return this._cancelEvent(e)}return true},addTag:function(a,b,c,d,e,f){if(!e)e="ctrl";var g=new Object();g.id=a;g.open=b;g.close=c;g.key=d;g.ctrlKey=e;g.multiline=f;g.elt=this.textarea.form[a];this.tags[a]=g;var h=g.elt;if(h){var i=this;if(h.type&&h.type.toUpperCase()=="BUTTON"){addEvent(h,'onclick',function(){i.insertTag(a);return false})}if(h.tagName&&h.tagName.toUpperCase()=="SELECT"){addEvent(h,'onchange',function(){i.insertTag(a);return false})}}else{if(a&&a.indexOf('_')!=0)return alert("addTag('"+a+"'): no such element in the form")}},insertTag:function(a){var b=this.tags[a];if(!b)return alert("Unknown tag ID: "+a);var c=b.open;if(typeof(b.open)=="function")c=b.open(b.elt);var d=b.close!=null?b.close:"/"+c;if(c.charAt(0)!=this.BRK_OP)c=this.BRK_OP+c+this.BRK_CL;if(d&&d.charAt(0)!=this.BRK_OP)d=this.BRK_OP+d+this.BRK_CL;this.surround(c,d,!b.multiline?null:b.multiline===true?this._prepareMultiline:b.multiline)},_prepareMultiline:function(a){a=a.replace(/\s+$/,'');a=a.replace(/^([ \t]*\r?\n)+/,'');if(a.indexOf("\n")>=0)a="\n"+a+"\n";return a}}
