!function(a){function b(a,b,e){var f=a.scrollTop;a.setSelectionRange?c(a,b,e):document.selection&&d(a,b,e),a.scrollTop=f}function c(a,b,c){var d=a.selectionStart,e=a.selectionEnd;if(d===e)b?d-c.tabString===a.value.substring(d-c.tabString.length,d)?(a.value=a.value.substring(0,d-c.tabString.length)+a.value.substring(d),a.focus(),a.setSelectionRange(d-c.tabString.length,d-c.tabString.length)):d-c.tabString===a.value.substring(d,d+c.tabString.length)&&(a.value=a.value.substring(0,d)+a.value.substring(d+c.tabString.length),a.focus(),a.setSelectionRange(d,d)):(a.value=a.value.substring(0,d)+c.tabString+a.value.substring(d),a.focus(),a.setSelectionRange(d+c.tabString.length,d+c.tabString.length));else{for(;d<a.value.length&&a.value.charAt(d).match(/[ \t]/);)d++;var f=a.value.split("\n"),g=[],h=0,i=0,j=0;for(j in f)i=h+f[j].length,g.push({start:h,end:i,selected:d>=h&&i>d||i>=e&&e>h||h>d&&e>i}),h=i+1;var k=0;for(j in g)if(g[j].selected){var l=g[j].start+k;b&&c.tabString===a.value.substring(l,l+c.tabString.length)?(a.value=a.value.substring(0,l)+a.value.substring(l+c.tabString.length),k-=c.tabString.length):b||(a.value=a.value.substring(0,l)+c.tabString+a.value.substring(l),k+=c.tabString.length)}a.focus();var m=d+(k>0?c.tabString.length:0>k?-c.tabString.length:0),n=e+k;a.setSelectionRange(m,n)}}function d(b,c,d){var e=document.selection.createRange();if(b===e.parentElement())if(""===e.text)if(c){var f=e.getBookmark();e.moveStart("character",-d.tabString.length),d.tabString===e.text?e.text="":(e.moveToBookmark(f),e.moveEnd("character",d.tabString.length),d.tabString===e.text&&(e.text="")),e.collapse(!0),e.select()}else e.text=d.tabString,e.collapse(!1),e.select();else{var g=e.text,h=g.length,i=g.split("\r\n"),j=document.body.createTextRange();j.moveToElementText(b),j.setEndPoint("EndToStart",e);var k=j.text,l=k.split("\r\n"),m=k.length,n=document.body.createTextRange();n.moveToElementText(b),n.setEndPoint("StartToEnd",e);var o=n.text,p=document.body.createTextRange();p.moveToElementText(b),p.setEndPoint("StartToEnd",j);var q=p.text,r=a(b).html();a("#r3").text(m+" + "+h+" + "+o.length+" = "+r.length),m+q.length<r.length?(l.push(""),m+=2,c&&d.tabString===i[0].substring(0,d.tabString.length)?i[0]=i[0].substring(d.tabString.length):c||(i[0]=d.tabString+i[0])):c&&d.tabString===l[l.length-1].substring(0,d.tabString.length)?l[l.length-1]=l[l.length-1].substring(d.tabString.length):c||(l[l.length-1]=d.tabString+l[l.length-1]);for(var s=1;s<i.length;s++)c&&d.tabString===i[s].substring(0,d.tabString.length)?i[s]=i[s].substring(d.tabString.length):c||(i[s]=d.tabString+i[s]);1===l.length&&0===m&&(c&&d.tabString===i[0].substring(0,d.tabString.length)?i[0]=i[0].substring(d.tabString.length):c||(i[0]=d.tabString+i[0])),m+h+o.length<r.length&&(i.push(""),h+=2),j.text=l.join("\r\n"),e.text=i.join("\r\n");var t=document.body.createTextRange();t.moveToElementText(b),m>0?t.setEndPoint("StartToEnd",j):t.setEndPoint("StartToStart",j),t.setEndPoint("EndToEnd",e),t.select()}}a.fn.tabby=function(c){var d=a.extend({},a.fn.tabby.defaults,c),e=a.fn.tabby.pressed;return this.each(function(){var c=a(this),f=a.meta?a.extend({},d,c.data()):d;c.bind("keydown",function(c){var d=a.fn.tabby.catch_kc(c);return 16===d&&(e.shft=!0),17===d&&(e.ctrl=!0,setTimeout(function(){a.fn.tabby.pressed.ctrl=!1},1e3)),18===d&&(e.alt=!0,setTimeout(function(){a.fn.tabby.pressed.alt=!1},1e3)),9!==d||e.ctrl||e.alt?void 0:(c.preventDefault(),e.last=d,setTimeout(function(){a.fn.tabby.pressed.last=null},0),b(a(c.target).get(0),e.shft,f),!1)}).bind("keyup",function(b){16===a.fn.tabby.catch_kc(b)&&(e.shft=!1)}).bind("blur",function(b){9===e.last&&a(b.target).one("focus",function(){e.last=null}).get(0).focus()})})},a.fn.tabby.catch_kc=function(a){return a.keyCode?a.keyCode:a.charCode?a.charCode:a.which},a.fn.tabby.pressed={shft:!1,ctrl:!1,alt:!1,last:null},a.fn.tabby.defaults={tabString:String.fromCharCode(9)}}(jQuery);