/*
 *
 * Copyright (c) 2010 C. F., Wong (<a href="http://cloudgen.w0ng.hk/jquery/caret.php">Cloudgen Examplet Store</a>)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
﻿(function(k,e,i,j){k.fn.caret=function(b,l){var a,d,f=this[0],m=k.browser.msie;if(typeof b==="object"&&typeof b.start==="number"&&typeof b.end==="number"){a=b.start;d=b.end}else if(typeof b==="number"&&typeof l==="number"){a=b;d=l}else if(typeof b==="string")if((a=f.value.indexOf(b))>-1)d=a+b[e];else a=null;else if(Object.prototype.toString.call(b)==="[object RegExp]"){b=b.exec(f.value);if(b!=null){a=b.index;d=a+b[0][e]}}if(typeof a!="undefined"){if(m){var c=this[0].createTextRange();c.collapse(true); c.moveStart("character",a);c.moveEnd("character",d-a);c.select()}else{this[0].selectionStart=a;this[0].selectionEnd=d}this[0].focus();return this}else{if(m)if(this[0].tagName.toLowerCase()!="textarea"){d=this.val();c=document.selection;a=c[i]()[j]();a.moveEnd("character",d[e]);var g=a.text==""?d[e]:d.lastIndexOf(a.text);a=c[i]()[j]();a.moveStart("character",-d[e]);var h=a.text[e]}else{a=c[i]();c=a[j]();c.moveToElementText(this[0]);c.setEndPsoint("EndToEnd",a);g=c.text[e]-a.text[e];h=g+a.text[e]}else{g= f.selectionStart;h=f.selectionEnd}c=f.value.substring(g,h);return{start:g,end:h,text:c,replace:function(n){return f.value.substring(0,g)+n+f.value.substring(h,f.value[e])}}}}})(jQuery,"length","createRange","duplicate");
