(function(){function k(b){b=parseInt(b);return isNaN(b)?0:b}var p={},v={buttons:"fullscreen,wrap",linenum:true,textarea:null,width:null,height:null},n=function(b){b=$.extend({},v,b||{});var c=this,a=b.textarea;a.jquery||(a=$(a));var d=a[0];b.width||(b.width=d.offsetWidth);b.height||(b.height=d.offsetHeight);var e=$('<div class="editbox" style="display:none;"><div class="editarea"></div></div>'),f=e.find("div");this.editarea=f;a.after(e).appendTo(f);e[0].style.cssText="visibility:hidden";a.css({outline:"none",
resize:"none",border:"none",padding:0,margin:0,"overflow-x":"auto"}).attr("wrap","off");this.ctrlarea=null;this.buttons={};if(b.buttons){for(var g=b.buttons.split(/\s*,\s*/),o=$('<p class="ctrlarea"></p>').prependTo(e),w=function(h,i){var l=$('<a href="#'+h+'" title="'+i.desc+'">'+i.text+"</a>");i.icon&&l.prepend('<img src="'+i.icon+'" />');l.click(function(){c[h](l,c.getSelection());return false});c.buttons[h]=l;o.append(l)},q=0,j;q<g.length;q++)if(j=g[q])if(j=="|")o.append("<span>|</span>");else if(j==
"/")o.append("<br />");else j in p&&w(j,p[j]);this.ctrlarea=o}if(b.linenum){g=$('<textarea class="linenum" disabled="disabled"></textarea>');g.css({outline:"none",resize:"none",border:"none",padding:0,margin:0,scrolling:"no"});var m=g[0];a.before(g);var x=0,s=function(h){for(var i="";h--;)i+=++x+"\n";m.value+=i};s(200);a.scroll(function(){for(m.scrollTop=d.scrollTop;m.scrollTop!=d.scrollTop;){s(10);m.scrollTop=d.scrollTop}});$.browser.msie&&e.bind("selectstart",function(h){if(h.target==m)return false});
this.linenum=g}a.tabindent();this.textarea=a;this.editbox=e;this.editarea=f;this.setDim(b.width,b.height);e.css("visibility","visible");return this};n.setPlugin=function(b,c,a){p[b]=$.extend({text:b,desc:b,icon:null},a||{});r[b]=c;return this};var r=n.prototype={setDim:function(b,c){c||(c=this.editbox.outerHeight());var a=document.compatMode=="CSS1Compat"?b-parseInt(this.editbox.css("border-left-width"))*2:b,d=a;if(this.linenum)d-=this.linenum.outerWidth();this.editbox.width(a);this.textarea.width(d);
d=a=document.compatMode=="CSS1Compat"?c-parseInt(this.editbox.css("border-top-width"))*2:c;if(this.ctrlarea)d-=this.ctrlarea.outerHeight();this.editbox.height(a);this.editarea.height(d);this.textarea.height(d);this.linenum&&this.linenum.height(d)},getSelection:function(){if(!this.textarea.is(":visible"))return null;this.textarea[0].focus();var b=document.selection&&document.selection.createRange(),c=b?b.text:this.textarea.selection();return{selection:b,text:c}}};r.hide=function(){this.editbox.hide()};
r.show=function(){this.editbox.show()};$.editplus=n;$.fn.editplus=function(b){b||(b={});b.textarea=this;return new n(b)};n.setPlugin("fullscreen",function(b){if(this._placeholder){var c=this._storedStyle.height,a=this._storedStyle.width;delete this._storedStyle.height;delete this._storedStyle.width;this.editbox.css(this._storedStyle);this._placeholder.replaceWith(this.editbox);this.setDim(a,c);$(document.body).css(this._storedBodyStyle);this._placeholder=this._storedBodyScroll=this._storedBodyStyle=
this._storedStyle=null;b.removeClass("active").attr("title","\u5168\u5c4f\u7f16\u8f91")}else{c=this.editbox.outerHeight();a=this.editbox.outerWidth();var d=k(this.editbox.css("margin-top")),e=k(this.editbox.css("margin-right")),f=k(this.editbox.css("margin-bottom")),g=k(this.editbox.css("margin-left"));this._storedStyle={width:a,height:c,margin:d+"px "+e+"px "+f+"px "+g+"px",position:"static",zIndex:0};this._placeholder=$('<div style="width:'+a+"px;height:"+c+'px;"></div>');this.editbox.after(this._placeholder);
c=(c=k(this.editbox.offsetParent().css("z-index")))?c+1:1E3;a=$(document.body);this.editbox.appendTo(a).css({position:"fixed",margin:0,top:0,left:0,zIndex:c});this._storedBodyStyle={"overflow-x":a.css("overflow-x"),"overflow-y":a.css("overflow-y"),scroll:a.css("scroll")};a.css({"overflow-x":"hidden","overflow-y":"hidden",scroll:"no"});this.setDim(ct.innerWidth(),ct.innerHeight());b.addClass("active").attr("title","\u8fd8\u539f\u5c3a\u5bf8")}},{text:"\u5168\u5c4f",desc:"\u5168\u5c4f\u7f16\u8f91"}).setPlugin("wrap",
function(b){if(this.textarea.attr("wrap")=="off"){this.textarea.attr("wrap","soft");this.textarea.css("overflow-x","hidden");b.addClass("active")}else{this.textarea.attr("wrap","off");this.textarea.css("overflow-x","auto");b.removeClass("active")}},{text:"\u81ea\u52a8\u6362\u884c",desc:"\u81ea\u52a8\u6362\u884c"}).setPlugin("save",function(){this._placeholder&&this.buttons.fullscreen.click();$(this.textarea[0].form).submit()},{text:"\u4fdd\u5b58"}).setPlugin("visual",function(b){if(tinyMCE in window&&
editor in window)if(b.data("visual")){tinyMCE.get("data").remove();this.linenum.show();b.data("visual",0);b.removeClass("active").attr("title","\u53ef\u89c6\u7f16\u8f91")}else{this.linenum.hide();editor("data",null,{width:this.editarea.innerWidth()+1,height:this.editarea.innerHeight()-5});b.data("visual",1);b.addClass("active").attr("title","\u8fd8\u539f")}},{text:"\u53ef\u89c6\u7f16\u8f91",desc:"\u53ef\u89c6\u7f16\u8f91"});var t=/(([^|\n] {1,3})\t)|((^|\n) {1,4}|\t)/g,u=function(b,c,a,d,e){return c?
a:e},y=function(b){var c=0;if(window.event){b=window.event;c=1}this.focus();var a=document.selection&&document.selection.createRange(),d=a?a.text:this.selectionStart!==undefined?this.value.substring(this.selectionStart,this.selectionEnd):"";if((b.keyCode?b.keyCode:b.charCode)==9){if(c)b.keyCode=0;b.preventDefault&&b.preventDefault();b.returnValue=false;b.stopPropagation&&b.stopPropagation();b.cancelBubble=true;if(b.shiftKey)if(a){c=0;for(d=a.text.length;;){a.moveStart("character",-1);if(a.parentElement()!=
this){this.focus();a=document.selection.createRange();a.moveStart("character",-c);break}b=a.text;var e=b.charAt(0);b=b.length;if(d+1>b||e=="\r"){a.moveStart("character",1);break}d=b;c+=1}a.moveEnd("character",1);if(a.parentElement()!=this){this.focus();a=document.selection.createRange();a.moveStart("character",-c);d=a.text}else{d=a.text;a.moveEnd("character",-1);if(a.parentElement()!=this){this.focus();a=document.selection.createRange();a.moveStart("character",-c)}else d=d.substr(0,d.length-1)}e=
(e=/(^ {0,3}\t)|(^ {1,4})/.exec(d))?e[2]?e[2].length:1:0;var f=d.replace(t,u);b=-f.replace(/\r\n/g,"\n").length;a.text=f;a.moveStart("character",c+b-e);a.select()}else{c=this.selectionStart+0;b=this.selectionEnd+0;for(a=c;c!=0&&this.value.charAt(c-1)!="\n";)c-=1;d=this.value.substring(c,b);e=(e=/(^ {0,3}\t)|(^ {1,4})/.exec(d))?e[2]?e[2].length:1:0;f=d.replace(t,u);this.value=this.value.substring(0,c)+f+this.value.substr(this.selectionEnd);this.selectionStart=a-e;this.selectionEnd=b+f.length-d.length}else if(d)if(a){c=
0;for(d=a.text.length;;){a.moveStart("character",-1);if(a.parentElement()!=this){this.focus();a=document.selection.createRange();a.moveStart("character",-c);break}b=a.text;e=b.charAt(0);b=b.length;if(d+1>b||e=="\r"){a.moveStart("character",1);break}d=b;c+=1}a.moveEnd("character",1);if(a.parentElement()!=this){this.focus();a=document.selection.createRange();a.moveStart("character",-c);d=a.text}else{d=a.text;a.moveEnd("character",-1);if(a.parentElement()!=this){this.focus();a=document.selection.createRange();
a.moveStart("character",-c)}else d=d.substr(0,d.length-1)}f="    "+d.replace(/\n/g,"\n    ");b=-f.replace(/\r\n/g,"\n").length;a.text=f;a.moveStart("character",b+c+4);a.select()}else{c=this.selectionStart+0;b=this.selectionEnd+0;for(a=c;c!=0&&this.value.charAt(c-1)!="\n";)c-=1;f=this.value.substring(c,b).replace(/\n/g,"\n    ");this.value=this.value.substring(0,c)+"    "+f+this.value.substr(this.selectionEnd);this.selectionStart=a+4;this.selectionEnd=this.selectionStart+f.length-a+c}else if(a)a.text=
"    "+a.text;else{c=this.selectionStart+0;this.value=this.value.substring(0,c)+"    "+this.value.substr(c);this.selectionEnd=this.selectionStart=c+4}}};$.fn.tabindent=function(){this.keydown(y);return this}})(jQuery);
