/*

  Implement Github like autocomplete mentions
  http://ichord.github.com/At.js

  Copyright (c) 2013 chord.luo@gmail.com
  Licensed under the MIT license.
*/


/*
本插件操作 textarea 或者 input 内的插入符
只实现了获得插入符在文本框中的位置，我设置
插入符的位置.
*/

(function(){(function(n){return typeof define=="function"&&define.amd?define(["jquery"],n):n(window.jQuery)})(function(n){"use strict";var u,r,f,t,i,e;return e="caret",u=function(){function i(n){this.$inputor=n,this.domInputor=this.$inputor[0]}return i.prototype.setPos=function(){return this.domInputor},i.prototype.getIEPosition=function(){return n.noop()},i.prototype.getPosition=function(){return n.noop()},i.prototype.getOldIEPos=function(){var n,t;return t=document.selection.createRange(),n=document.body.createTextRange(),n.moveToElementText(this.domInputor),n.setEndPoint("EndToEnd",t),n.text.length},i.prototype.getPos=function(){var n,i,t;return(t=this.range())?(n=t.cloneRange(),n.selectNodeContents(this.domInputor),n.setEnd(t.endContainer,t.endOffset),i=n.toString().length,n.detach(),i):document.selection?this.getOldIEPos():void 0},i.prototype.getOldIEOffset=function(){var t,n;return t=document.selection.createRange().duplicate(),t.moveStart("character",-1),n=t.getBoundingClientRect(),{height:n.bottom-n.top,left:n.left,top:n.top}},i.prototype.getOffset=function(){var u,f,i,r;f=null;if(window.getSelection&&(i=this.range())){if(i.endOffset-1<0)return null;u=i.cloneRange(),u.setStart(i.endContainer,i.endOffset-1),u.setEnd(i.endContainer,i.endOffset),r=u.getBoundingClientRect(),f={height:r.height,left:r.left+r.width,top:r.top},u.detach(),f}else document.selection&&this.getOldIEOffset();return t.adjustOffset(f,this.$inputor)},i.prototype.range=function(){var n;if(window.getSelection)return n=window.getSelection(),n.rangeCount>0?n.getRangeAt(0):null},i}(),r=function(){function n(n){this.$inputor=n,this.domInputor=this.$inputor[0]}return n.prototype.getIEPos=function(){var f,n,u,e,t,i,r;return n=this.domInputor,i=document.selection.createRange(),t=0,i&&i.parentElement()===n&&(e=n.value.replace(/\r\n/g,"\n"),u=e.length,r=n.createTextRange(),r.moveToBookmark(i.getBookmark()),f=n.createTextRange(),f.collapse(!1),t=r.compareEndPoints("StartToEnd",f)>-1?u:-r.moveStart("character",-u)),t},n.prototype.getPos=function(){return document.selection?this.getIEPos():this.domInputor.selectionStart},n.prototype.setPos=function(n){var t,i;return t=this.domInputor,document.selection?(i=t.createTextRange(),i.move("character",n),i.select()):t.setSelectionRange&&t.setSelectionRange(n,n),t},n.prototype.getIEOffset=function(n){var u,f,t,r,i;return t=this.domInputor.createTextRange(),n?t.move("character",n):(f=document.selection.createRange(),t.moveToBookmark(f.getBookmark())),r=t.boundingLeft,i=t.boundingTop,u=t.boundingHeight,{left:r,top:i,height:u}},n.prototype.getOffset=function(n){var u,i,r;return u=this.$inputor,document.selection?t.adjustOffset(this.getIEOffset(n),u):(i=u.offset(),r=this.getPosition(n),i={left:i.left+r.left,top:i.top+r.top,height:r.height})},n.prototype.getPosition=function(n){var i,o,e,t,u,r;return i=this.$inputor,e=function(n){return n.replace(/</g,"&lt").replace(/>/g,"&gt").replace(/`/g,"&#96").replace(/"/g,"&quot").replace(/\r\n|\r|\n/g,"<br />")},n===void 0&&(n=this.getPos()),r=i.val().slice(0,n),t="<span>"+e(r)+"</span>",t+="<span id='caret'>|</span>",u=new f(i),o=u.create(t).rect()},n.prototype.getIEPosition=function(n){var f,i,t,u,r;return t=this.getIEOffset(n),i=this.$inputor.offset(),u=t.left-i.left,r=t.top-i.top,f=t.height,{left:u,top:r,height:f}},n}(),f=function(){function t(n){this.$inputor=n}return t.prototype.css_attr=["overflowY","height","width","paddingTop","paddingLeft","paddingRight","paddingBottom","marginTop","marginLeft","marginRight","marginBottom","fontFamily","borderStyle","borderWidth","wordWrap","fontSize","lineHeight","overflowX","text-align"],t.prototype.mirrorCss=function(){var t,i=this;return t={position:"absolute",left:-9999,top:0,zIndex:-2e4,"white-space":"pre-wrap"},n.each(this.css_attr,function(n,r){return t[r]=i.$inputor.css(r)}),t},t.prototype.create=function(t){return this.$mirror=n("<div></div>"),this.$mirror.css(this.mirrorCss()),this.$mirror.html(t),this.$inputor.after(this.$mirror),this},t.prototype.rect=function(){var t,n,i;return t=this.$mirror.find("#caret"),n=t.position(),i={left:n.left,top:n.top,height:t.height()},this.$mirror.remove(),i},t}(),t={adjustOffset:function(t,i){if(t)return t.top+=n(window).scrollTop()+i.scrollTop(),t.left+=+n(window).scrollLeft()+i.scrollLeft(),t},contentEditable:function(n){return!!(n[0].contentEditable&&n[0].contentEditable==="true")}},i={pos:function(n){return n?this.setPos(n):this.getPos()},position:function(n){return document.selection?this.getIEPosition(n):this.getPosition(n)},offset:function(n){return this.getOffset(n)}},n.fn.caret=function(f){var e;return e=t.contentEditable(this)?new u(this):new r(this),i[f]?i[f].apply(e,Array.prototype.slice.call(arguments,1)):n.error("Method "+f+" does not exist on jQuery.caret")},n.fn.caret.EditableCaret=u,n.fn.caret.InputCaret=r,n.fn.caret.Utils=t,n.fn.caret.apis=i})}).call(this);