/* 
 * a-tools 1.1
 * 
 * Copyright (c) 2009 Andrey Kramarev, Ampparit Inc. (www.ampparit.com)
 * Licensed under the MIT license.
 * http://www.ampparit.fi/a-tools/license.txt
 *
 * Basic usage:
 
    <textarea></textarea>
    <input type="text" />

    // Get current selection
    var sel = $("textarea").getSelection()
    
    // Replace current selection
    $("input").replaceSelection("foo");

    // Count characters
    alert($("textarea").countCharacters());

    // Set max length without callback function
    $("textarea").setMaxLength(7);

    // Set max length with callback function which will be called when limit is exceeded
    $("textarea").setMaxLength(10, function() {
        alert("hello")
    });

    // Removing limit:
    $("textarea").setMaxLength(-1);
    
    // Insert text at current caret position
    $("#textarea").insertAtCaretPos("hello");

 */
var caretPositionAmp;jQuery.fn.extend({getSelection:function(){var k=this.jquery?this[0]:this;var b;var f;var c;if(document.selection){var p=document.selection.createRange();if(p.text){var d=0;c=p.text;if(k.value.match(/\n/g)!=null){d=k.value.match(/\n/g).length}var e=0;var j=0;var g=0;if(typeof(k.selectionStart)=="number"){b=k.selectionStart;f=k.selectionEnd;if(b==f){return this}}else{var n=k.createTextRange();var m;var o;var a=n.duplicate();m=n.text;n.moveToBookmark(p.getBookmark());o=n.text;a.setEndPoint("EndToStart",n);if(m==o&&m!=p.text){return this}b=a.text.length;f=a.text.length+p.text.length}if(d>0){for(var h=0;h<=d;h++){var l=k.value.indexOf("\n",j);if(l!=-1&&l<b){j=l+1;e++;g=e}else{if(l!=-1&&l>=b&&l<=f){if(l==b+1){e--;g--;j=l+1;continue}j=l+1;g++}else{h=d}}}}if(p.text.indexOf("\n",0)==1){g=g+2}b=b-e;f=f-g;return{start:b,end:f,text:p.text,length:f-b}}return false}else{if(typeof(k.selectionStart)=="number"&&k.selectionStart!=k.selectionEnd){b=k.selectionStart;f=k.selectionEnd;c=k.value.substring(k.selectionStart,k.selectionEnd);return{start:b,end:f,text:c,length:f-b}}else{return{start:undefined,end:undefined,text:undefined,length:undefined}}}},replaceSelection:function(d){var e=this.jquery?this[0]:this;var b;var c;if(document.selection){var h=document.selection.createRange();if(typeof(e.selectionStart)=="number"){b=e.selectionStart;c=e.selectionEnd;if(b==c){return this}}if(typeof(e.selectionStart)!="number"){var g=e.createTextRange();var f;var i;var a=g.duplicate();f=g.text;g.moveToBookmark(h.getBookmark());i=g.text;a.setEndPoint("EndToStart",g);if(f==i&&f!=h.text){return this}}if(h.text){h.text=d}return this}else{if(typeof(e.selectionStart)=="number"&&e.selectionStart!=e.selectionEnd){b=e.selectionStart;c=e.selectionEnd;e.value=e.value.substr(0,b)+d+e.value.substr(c);return this}}return this},insertAtCaretPos:function(d){var c=this.jquery?this[0]:this;var h;var b;var a;var f;var e;var g;c.focus();c.onclick=function(){if(document.selection&&typeof(c.selectionStart)!="number"){f=document.selection.createRange();e=c.createTextRange();g=e.duplicate();e.moveToBookmark(f.getBookmark());g.setEndPoint("EndToStart",e);caretPositionAmp=g.text.length}};if(document.selection&&typeof(c.selectionStart)!="number"){f=document.selection.createRange();e=c.createTextRange();textLength=e.text.length;g=e.duplicate();e.moveToBookmark(f.getBookmark());g.setEndPoint("EndToStart",e);h=g.text.length;if(caretPositionAmp>=0&&h==0&&caretPositionAmp!=h){e.move("character",caretPositionAmp);e.select();f=document.selection.createRange();caretPositionAmp+=d.length}else{if(caretPositionAmp==undefined&&h==0){e.move("character",textLength);e.select();f=document.selection.createRange();caretPositionAmp=d.length}else{if(caretPositionAmp!=undefined){caretPositionAmp+=d.length}else{caretPositionAmp=h+d.length}}}f.text=d;c.focus();return this}else{if(typeof(c.selectionStart)=="number"&&c.selectionStart==c.selectionEnd){a=c.selectionStart+d.length;h=c.selectionStart;b=c.selectionEnd;c.value=c.value.substr(0,h)+d+c.value.substr(b);c.setSelectionRange(a,a);return this}}return this},countCharacters:function(b){var a=this.jquery?this[0]:this;if(a.value.match(/\r/g)!=null){return a.value.length-a.value.match(/\r/g).length}return a.value.length},setMaxLength:function(a,b){this.each(function(){var d=this.jquery?this[0]:this;var f=d.type;var e;var c;if(parseInt(a)<0){a=100000000}if(f=="text"){d.maxLength=a}if(f=="textarea"||f=="text"){d.onkeypress=function(j){var g=d.value.match(/\r/g);c=a;if(g!=null){c=parseInt(c)+g.length}var h=j||event;var i=h.keyCode;if(document.selection){e=document.selection.createRange().text.length>0}else{e=d.selectionStart!=d.selectionEnd}if(d.value.length>=c&&(i>47||i==32||i==0||i==13)&&!h.ctrlKey&&!h.altKey&&!e){d.value=d.value.substring(0,c);if(typeof(b)=="function"){b()}return false}};d.onkeyup=function(){var h=d.value.match(/\r/g);var k=0;var g=0;c=a;if(h!=null){for(var j=0;j<=h.length;j++){if(d.value.indexOf("\n",g)<=parseInt(a)){k++;g=d.value.indexOf("\n",g)+1}}c=parseInt(a)+k}if(d.value.length>c){d.value=d.value.substring(0,c);if(typeof(b)=="function"){b()}return this}}}else{return this}});return this}});
