/*! 
 * a-tools 1.3.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");
    
    // Set caret position (1 = beginning, -1 = end)
    $("#textArea").setCaretPos(10);

 */
var caretPositionAmp; jQuery.fn.extend({getSelection:function(){var b=this.jquery?this[0]:this,a,c,f=0;if(document.selection){var g=document.selection.createRange(),e=0,d=0,j=0;if(b.value.match(/\n/g)!=null)f=b.value.match(/\n/g).length;if(g.text){if(typeof b.selectionStart=="number"){a=b.selectionStart;c=b.selectionEnd;if(a==c)return{start:a,end:c,text:g.text,length:c-a}}else{var i,h;a=b.createTextRange();c=a.duplicate();i=a.text;a.moveToBookmark(g.getBookmark());h=a.text;c.setEndPoint("EndToStart",a);if(i==h&&i!=g.text)return this; a=c.text.length;c=c.text.length+g.text.length}if(f>0)for(i=0;i<=f;i++){h=b.value.indexOf("\n",d);if(h!=-1&&h<a){d=h+1;e++;j=e}else if(h!=-1&&h>=a&&h<=c)if(h==a+1){e--;j--;d=h+1}else{d=h+1;j++}else i=f}if(g.text.indexOf("\n",0)==1)j+=2;a-=e;c-=j;return{start:a,end:c,text:g.text,length:c-a}}b.focus();if(typeof b.selectionStart=="number")a=b.selectionStart;else{g=document.selection.createRange();a=b.createTextRange();c=a.duplicate();a.moveToBookmark(g.getBookmark());c.setEndPoint("EndToStart",a);a=c.text.length}if(f> 0)for(i=0;i<=f;i++){h=b.value.indexOf("\n",d);if(h!=-1&&h<a){d=h+1;e++}else i=f}a-=e;return{start:a,end:a,text:g.text,length:0}}else if(typeof b.selectionStart=="number"){a=b.selectionStart;c=b.selectionEnd;b=b.value.substring(b.selectionStart,b.selectionEnd);return{start:a,end:c,text:b,length:c-a}}else return{start:undefined,end:undefined,text:undefined,length:undefined}},replaceSelection:function(b){var a=this.jquery?this[0]:this,c,f;f=0;var g,e,d=0,j=0,i=a.scrollTop==undefined?0:a.scrollTop;if(document.selection&& typeof a.selectionStart!="number"){i=document.selection.createRange();if(typeof a.selectionStart!="number"){var h;e=a.createTextRange();g=e.duplicate();c=e.text;e.moveToBookmark(i.getBookmark());h=e.text;g.setEndPoint("EndToStart",e);if(c==h&&c!=i.text)return this}if(i.text){part=i.text;if(a.value.match(/\n/g)!=null)d=a.value.match(/\n/g).length;c=g.text.length;if(d>0)for(h=0;h<=d;h++){var k=a.value.indexOf("\n",f);if(k!=-1&&k<c){f=k+1;j++}else h=d}i.text=b;caretPositionAmp=g.text.length+b.length; e.move("character",caretPositionAmp);document.selection.empty();a.blur()}return this}else if(typeof a.selectionStart=="number"&&a.selectionStart!=a.selectionEnd){c=a.selectionStart;f=a.selectionEnd;a.value=a.value.substr(0,c)+b+a.value.substr(f);f=c+b.length;a.setSelectionRange(f,f);a.scrollTop=i;return this}return this},insertAtCaretPos:function(b){var a=this.jquery?this[0]:this,c,f,g,e,d,j,i=f=0;a.focus();var h=a.scrollTop==undefined?0:a.scrollTop;if(document.selection&&typeof a.selectionStart!= "number"){if(a.value.match(/\n/g)!=null)i=a.value.match(/\n/g).length;c=parseInt(caretPositionAmp);if(i>0)for(var k=0;k<=i;k++){var l=a.value.indexOf("\n",g);if(l!=-1&&l<=c){g=l+1;c-=1;f++}}}caretPositionAmp=parseInt(caretPositionAmp);a.onclick=function(){if(document.selection&&typeof a.selectionStart!="number"){e=document.selection.createRange();d=a.createTextRange();j=d.duplicate();d.moveToBookmark(e.getBookmark());j.setEndPoint("EndToStart",d);caretPositionAmp=j.text.length}};if(document.selection&& typeof a.selectionStart!="number"){e=document.selection.createRange();if(e.text.length!=0)return this;d=a.createTextRange();textLength=d.text.length;j=d.duplicate();d.moveToBookmark(e.getBookmark());j.setEndPoint("EndToStart",d);c=j.text.length;if(caretPositionAmp>=0&&c==0&&caretPositionAmp!=c){f=caretPositionAmp-f;d.move("character",f);d.select();e=document.selection.createRange();caretPositionAmp+=b.length}else if(!(caretPositionAmp>=0)&&c==0){d.move("character",textLength);d.select();e=document.selection.createRange(); caretPositionAmp=b.length+textLength}else if(parseInt(caretPositionAmp)>=0)if(parseInt(caretPositionAmp)>=0&&parseInt(caretPositionAmp)==textLength){d.move("character",textLength);d.select();e=document.selection.createRange();caretPositionAmp=b.length+textLength}else{parseInt(caretPositionAmp)>=0?d.move("character",0):d.move("character",caretPositionAmp-c);document.selection.empty();d.select();e=document.selection.createRange();caretPositionAmp+=b.length}else{caretPositionAmp>=0?d.move("character", c):d.move("character",0);document.selection.empty();d.select();e=document.selection.createRange();caretPositionAmp=c+b.length}e.text=b;a.focus();return this}else if(typeof a.selectionStart=="number"&&a.selectionStart==a.selectionEnd){g=a.selectionStart+b.length;c=a.selectionStart;f=a.selectionEnd;a.value=a.value.substr(0,c)+b+a.value.substr(f);a.setSelectionRange(g,g);a.scrollTop=h;return this}return this},setCaretPos:function(b){var a=this.jquery?this[0]:this,c,f=0,g=0,e;a.focus();if(parseInt(b)== 0)return this;if(parseInt(b)>0){b=parseInt(b)-1;if(document.selection&&typeof a.selectionStart=="number"&&a.selectionStart==a.selectionEnd){if(a.value.match(/\n/g)!=null)f=a.value.match(/\n/g).length;if(f>0)for(var d=0;d<=f;d++){e=a.value.indexOf("\n",c);if(e!=-1&&e<=b){c=e+1;b=parseInt(b)+1}}}}else if(parseInt(b)<0){b=parseInt(b)+1;if(document.selection&&typeof a.selectionStart!="number"){b=a.value.length+parseInt(b);if(a.value.match(/\n/g)!=null)f=a.value.match(/\n/g).length;if(f>0){for(d=0;d<= f;d++){e=a.value.indexOf("\n",c);if(e!=-1&&e<=b){c=e+1;b=parseInt(b)-1;g+=1}}b=b+g-f}}else if(document.selection&&typeof a.selectionStart=="number"){b=a.value.length+parseInt(b);if(a.value.match(/\n/g)!=null)f=a.value.match(/\n/g).length;if(f>0){b=parseInt(b)-f;for(d=0;d<=f;d++){e=a.value.indexOf("\n",c);if(e!=-1&&e<=b){c=e+1;b=parseInt(b)+1;g+=1}}}}else b=a.value.length+parseInt(b)}else return this;if(document.selection&&typeof a.selectionStart!="number"){c=document.selection.createRange();if(c.text!= 0)return this;a=a.createTextRange();a.collapse(true);a.moveEnd("character",b);a.moveStart("character",b);a.select();caretPositionAmp=b;return this}else if(typeof a.selectionStart=="number"&&a.selectionStart==a.selectionEnd){a.setSelectionRange(b,b);return this}return this},countCharacters:function(){var b=this.jquery?this[0]:this;if(b.value.match(/\r/g)!=null)return b.value.length-b.value.match(/\r/g).length;return b.value.length},setMaxLength:function(b,a){this.each(function(){var c=this.jquery? this[0]:this,f=c.type,g,e;if(parseInt(b)<0)b=1E8;if(f=="text")c.maxLength=b;if(f=="textarea"||f=="text"){c.onkeypress=function(d){var j=c.value.match(/\r/g);e=b;if(j!=null)e=parseInt(e)+j.length;d=d||event;j=d.keyCode;g=document.selection?document.selection.createRange().text.length>0:c.selectionStart!=c.selectionEnd;if(c.value.length>=e&&(j>47||j==32||j==0||j==13)&&!d.ctrlKey&&!d.altKey&&!g){c.value=c.value.substring(0,e);typeof a=="function"&&a();return false}};c.onkeyup=function(){var d=c.value.match(/\r/g), j=0,i=0;e=b;if(d!=null){for(var h=0;h<=d.length;h++)if(c.value.indexOf("\n",i)<=parseInt(b)){j++;i=c.value.indexOf("\n",i)+1}e=parseInt(b)+j}if(c.value.length>e){c.value=c.value.substring(0,e);typeof a=="function"&&a();return this}}}else return this});return this}});
