(function(d){var b=function(h){if(!h){return[]}if(h.toArray){return h.toArray()}else{var g=[];for(var f=0;f<h.length;f++){g.push(h[f])}return g}};Function.prototype.ebind=function(){var f=this,h=b(arguments),g=h.shift();return function(){return f.apply(g,h.concat(b(arguments)))}};Function.prototype.bindAsEventListener=function(){var f=this,h=b(arguments),g=h.shift();return function(i){return f.apply(g,[i||window.event].concat(h))}};K=function(f){return f};Array.prototype.collect=function(h,g){h=h||K;var f=[];d(this).each(function(i,j){f.push(h.call(g,j,i))});return f},d.fn.TextArea=function(f){f=f||{};var g={tab_spacing:true,tab_char:4,lineHeight:16,change:null,resizeable:true,resizeClassName:"resizeHandle"};var f=d.extend(g,f);return this.each(function(){new e(this,f)})};function e(g,f){return this instanceof e?this.init(g,f):e(g,f)}d.extend(e.prototype,{onChangeTimeoutLength:500,init:function(k,g){var f=this;this.el=d(k);this.element=this.el[0];this.options=g;this.toolbar=g.toolbar;this._change_callback=g.change;d.extend(k,{textarea:this});var j=navigator.userAgent.toLowerCase();this.isWebkit=(j.indexOf("webkit")>=0);this.isChrome=(j.indexOf("chrome")>=0);this.isOpera=(j.indexOf("opera")>=0);if(this.options.resizeable&&!this.isWebkit){resizeHandle=d('<div class="'+this.options.resizeClassName+'"></div>').insertAfter(this.el).bind("mousedown",function(n){var m=f.el.height(),o=n.clientY,l,i;l=function(p){f.el.css("height",Math.max(20,p.clientY+m-o)+"px");return false};i=function(p){d("html").unbind("mousemove",l).unbind("mouseup",i);return false};d("html").bind("mousemove",l).bind("mouseup",i)})}this.lastSelection={};if(this.isWebkit){this.options.tab_char+=1}if(this.options.tab_spacing){this.tabulation="";for(var h=0;h<this.options.tab_char;h++){this.tabulation+=" "}}else{this.tabulation="\t"}this.tab_detected=false;if(!!document.selection){this.element.selectionStart=this.element.selectionEnd=0}this.el.keydown(this.handleKey.ebind(this));var f=this;this.doOnChange=function(i){if(f._change_callback){f._change_callback(f.element.value)}return};this.el.keyup(this.doOnChange);this.el.bind("paste",this.doOnChange);this.el.bind("input",this.doOnChange);if(!!document.selection){this.el.bind("mouseup",this.saveRange.bindAsEventListener(this));this.el.bind("keyup",this.saveRange.bindAsEventListener(this))}if(this.isOpera){this.el.bind("blur",function(i){if(f.lastKey&&f.lastKey==9){f.el.focus()}})}},saveRange:function(){this.range=document.selection.createRange()},getValue:function(){return this.element.value},getSelection:function(){if(!!document.selection){return document.selection.createRange().text}else{if(!!this.element.setSelectionRange){return this.element.value.substring(this.element.selectionStart,this.element.selectionEnd)}else{return false}}},replaceSelection:function(h){var g=this.element.scrollTop;if(!!document.selection){this.element.focus();var f=(this.range)?this.range:document.selection.createRange();f.text=h;f.select()}else{if(!!this.element.setSelectionRange){var i=this.element.selectionStart;this.element.value=this.element.value.substring(0,i)+h+this.element.value.substring(this.element.selectionEnd);this.element.setSelectionRange(i+h.length,i+h.length)}}this.doOnChange();this.element.focus();this.element.scrollTop=g},wrapSelection:function(f,g){this.replaceSelection(f+this.getSelection()+g)},insertBeforeSelection:function(f){this.replaceSelection(f+this.getSelection())},insertAfterSelection:function(f){this.replaceSelection(this.getSelection()+f)},collectFromEachSelectedLine:function(h,f,g){this.replaceSelection((f||"")+b(this.getSelection().split("\n")).collect(h).join("\n")+(g||""))},insertBeforeEachSelectedLine:function(h,f,g){this.collectFromEachSelectedLine(function(i){},f,g)},handleKey:function(f){c=f.charCode||f.keyCode;this.lastKey=c;if(c==9){this.tab_selection();if(window.event){f.returnValue=false}f.preventDefault();f.stopPropagation();return false}else{if((c==13||c==10)&&(!this.isOpera)){if(this.do_enter()){if(window.event){f.returnValue=false}f.preventDefault();f.stopPropagation();return false}}}return true},tab_selection:function(){if(this._is_tabbing){return}this._is_tabbing=true;if(!!document.selection&&!this.isOpera){this._getIESelection()}if(!this._tab_detected){this._detect_tab()}var l=this.element.selectionStart;var f=this.element.selectionEnd;var h=this.element.value.substring(l,f);var j=this.el.scrollTop();var k=this.el.scrollLeft();var g=l;var i=f;if(h.length==0){this.element.value=this.element.value.substr(0,l)+this.tabulation+this.element.value.substr(f);g=l+this.tabulation.length;i=g}else{l=Math.max(0,this.element.value.substr(0,l).lastIndexOf("\n")+1);endText=this.element.value.substr(f);startText=this.element;value.substr(0,l);tmp=h.split("\n");h=this.tabulation+tmp.join("\n"+this.tabulation);this.el.val(startText+h+endText);g=l;i=this.element.value.indexOf("\n",startText.length+h.length);if(i==-1){i=this.element.value.length}}this.element.selectionStart=g;this.element.selectionEnd=i;if(!!document.selection&&!this.isOpera){this._setIESelection();setTimeout(function(){self._is_tabbing=false},100);this._is_tabbing=false}else{this._is_tabbing=false}this.el.scrollTop(j);this.el.scrollLeft(k)},do_enter:function(){if(!!document.selection&&!this.isOpera){this._getIESelection()}var j=this.el.scrollTop();var k=this.el.scrollLeft();var l=this.element.selectionStart;var f=this.element.selectionEnd;var i=Math.max(0,this.element.value.substring(0,l).lastIndexOf("\n")+1);var g=this.element.value.substring(i,l);if(g.match(/^[ \t]+$/mg,"")){return false}var h=g.replace(/^([ \t]*).*/gm,"$1");if(h=="\n"||h=="\r\n"){return false}h=h.replace(/\r?\n/g,"");if(!!document.selection){h="\r\n"+h}else{h="\n"+h}this.element.value=this.element.value.substring(0,l)+h+this.element.value.substring(f);this.area_select(l+h.length,0);this.el.scrollTop(j);this.el.scrollLeft(k);return true},area_select:function(g,f){value=this.el.val();g=Math.max(0,Math.min(value.length,g));end=Math.max(g,Math.min(value.length,g+f));if(!!document.selection&&!this.isOpera){this.element.selectionStart=g;this.element.selectionEnd=end;this._setIESelection()}else{if(this.isOpera){this.element.setSelectionRange(0,0);this.element.setSelectionRange(end,end)}else{this.element.setSelectionRange(g,end)}}},_detect_tab:function(){if(this.element.value.indexOf("\t")>0){this.tabulation="\t"}else{this.tabulation="";for(var f=0;f<this.options.tab_char;f++){this.tabulation+=" "}}this._tab_detected=true},_getIESelection:function(){this.el.focus();var f=this.elelement.createTextRange();var g=f.duplicate();f.moveToBookmark(document.selection.createRange().getBookmark());f.moveEnd("character",this.element.value.length);this.el.selectionStart=this.element.value.length-f.text.length;g.moveToBookmark(document.selection.createRange().getBookmark());g.moveStart("character",-this.element.value.length);this.el.selectionEnd=g.text.length;if(this.element.selectionEnd<this.element.selectionStart){this.element.selectionEnd=this.element.selectionStart}},_setIESelection:function(){var f=this.element.value.substr(0,this.element.selectionStart).split("\n").length-1;var g=this.element.value.substr(0,this.element.selectionEnd).split("\n").length-1;var h=document.selection.createRange();h.moveToElementText(this.element);h.setEndPoint("EndToStart",h);h.collapse(true);h.moveStart("character",this.element.selectionStart-f);h.moveEnd("character",this.element.selectionEnd-g-(this.element.selectionStart-f));h.select()}});d.Toolbar=function(f,g){g=g||{};var h={className:null};var g=d.extend(h,g);return new a(f,g)};function a(f,g){return this instanceof a?this.init(f,g):a(f,g)}d.extend(a.prototype,{init:function(f,h){this.textarea=f;this.options=h;this.containers=[];var i=this.options.className||"toolbar";var g=this;this.textarea.each(function(){var j=d(document.createElement("ul"));j.addClass(i);j.insertBefore(this);var k=this;g.containers.push({container:j,textarea:k})})},attachButton:function(g,f,i){g.onclick=function(){return false};var h=f.textarea;d(g).bind("click",i.bindAsEventListener(h))},addButton:function(k,n,l){for(var m=0;m<this.containers.length;m++){var j=this.containers[m];var f=document.createElement("li");var h=document.createElement("a");h.href="#";this.attachButton(h,j.textarea,n);f.appendChild(h);if("className" in l){var g=l.className;l.className=null;delete l.className;d(h).addClass(g)}l.title=k;d.extend(h,l||{});j.container[0].appendChild(f)}}})})(jQuery);$(document).ready(function(){var a=$("#id_comment").TextArea({});var b=$.Toolbar(a,{className:"comments_toolbar"});b.addButton("Link",function(){var e=this.getSelection();var d=prompt("URL","");if(d==null){return}this.replaceSelection('<a href="'+(d==""?"http://link_url/":d).replace(/^(?!(f|ht)tps?:\/\/)/,"http://")+'">'+(e==""?"Link Text":e)+"</a>")},{id:"html_link_button"});b.addButton("Zitat",function(d){this.wrapSelection("<blockquote>","</blockquote>")},{id:"html_quote_button"});b.addButton("Hilfe",function(){window.open("/help/commentformat/")},{id:"html_help_button"})});
