/*! 
 * a-tools 1.4
 * 
 * Copyright (c) 2009 Andrey Kramarev(andrey.kramarev[at]ampparit.com), 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);
    
    // Set Selection (1 = beginning)
    $("#textArea").setSelection(10,15);

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

