!function(t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof exports?module.exports=t(require("jquery")):t(jQuery)}(function(l){var i,h;function t(t){this.currentFlag=null,this.controllers={},this.aliasMaps={},this.$inputor=l(t),this.setupRootElement(),this.listen()}h={ESC:27,TAB:9,ENTER:13,CTRL:17,A:65,P:80,N:78,LEFT:37,UP:38,RIGHT:39,DOWN:40,BACKSPACE:8,SPACE:32},i={beforeSave:function(t){return s.arrayToDefaultHash(t)},matcher:function(t,e,i,n){var r;return t=t.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),i&&(t="(?:^|\\s)"+t),r=decodeURI("%C3%80"),i=decodeURI("%C3%BF"),(e=new RegExp(t+"([A-Za-z"+r+"-"+i+"0-9_"+(n?" ":"")+"'.+-]*)$|"+t+"([^\\x00-\\xff]*)$","gi").exec(e))?e[2]||e[1]:null},filter:function(t,e,i){for(var n,r=[],o=0,s=e.length;o<s;o++)n=e[o],~String(n[i]).toLowerCase().indexOf(t.toLowerCase())&&r.push(n);return r},remoteFilter:null,sorter:function(t,e,i){var n,r,o,s;if(!t)return e;for(n=[],r=0,s=e.length;r<s;r++)(o=e[r]).atwho_order=String(o[i]).toLowerCase().indexOf(t.toLowerCase()),-1<o.atwho_order&&n.push(o);return n.sort(function(t,e){return t.atwho_order-e.atwho_order})},tplEval:function(t,i){var e=t;try{return(e="string"!=typeof t?t(i):e).replace(/\$\{([^\}]*)\}/g,function(t,e){return i[e]})}catch(t){return""}},highlighter:function(t,e){return e?(e=new RegExp(">\\s*([^<]*?)("+e.replace("+","\\+")+")([^<]*)\\s*<","ig"),t.replace(e,function(t,e,i,n){return"> "+e+"<strong>"+i+"</strong>"+n+" <"})):t},beforeInsert:function(t){return t},beforeReposition:function(t){return t},afterMatchFailed:function(){}},t.prototype.createContainer=function(t){var e;return null!=(e=this.$el)&&e.remove(),l(t.body).append(this.$el=l('<div class="atwho-container"></div>'))},t.prototype.setupRootElement=function(e,t){if(null==t&&(t=!1),e)this.window=e.contentWindow,this.document=e.contentDocument||this.window.document,this.iframe=e;else{this.document=this.$inputor[0].ownerDocument,this.window=this.document.defaultView||this.document.parentWindow;try{this.iframe=this.window.frameElement}catch(t){if(e=t,this.iframe=null,l.fn.atwho.debug)throw new Error("iframe auto-discovery is failed.\nPlease use `setIframe` to set the target iframe manually.\n"+e)}}return this.createContainer((this.iframeAsRoot=t)?this.document:document)},t.prototype.controller=function(t){var e,i,n,r;if(this.aliasMaps[t])i=this.controllers[this.aliasMaps[t]];else for(n in r=this.controllers)if(e=r[n],n===t){i=e;break}return i||this.controllers[this.currentFlag]},t.prototype.setContextFor=function(t){return this.currentFlag=t,this},t.prototype.reg=function(t,e){var i=(i=this.controllers)[t]||(i[t]=new(this.$inputor.is("[contentEditable]")?g:u)(this,t));return e.alias&&(this.aliasMaps[e.alias]=t),i.init(e),this},t.prototype.listen=function(){return this.$inputor.on("compositionstart",(a=this,function(){var t;return null!=(t=a.controller())&&t.view.hide(),a.isComposing=!0,null})).on("compositionend",(s=this,function(){return s.isComposing=!1,setTimeout(function(t){return s.dispatch(t)}),null})).on("keyup.atwhoInner",(o=this,function(t){return o.onKeyup(t)})).on("keydown.atwhoInner",(n=this,function(t){return n.onKeydown(t)})).on("blur.atwhoInner",(i=this,function(t){var e;if(e=i.controller())return e.expectedQueryCBId=null,e.view.hide(t,e.getOpt("displayTimeout"))})).on("click.atwhoInner",(e=this,function(t){return e.dispatch(t)})).on("scroll.atwhoInner",(r=this,function(){var n=r.$inputor.scrollTop();return function(t){var e,i=t.target.scrollTop;return n!==i&&null!=(e=r.controller())&&e.view.hide(t),n=i,!0}}()));var r,e,i,n,o,s,a},t.prototype.shutdown=function(){var t,e=this.controllers;for(t in e)e[t].destroy(),delete this.controllers[t];return this.$inputor.off(".atwhoInner"),this.$el.remove()},t.prototype.dispatch=function(t){var e,i,n=this.controllers,r=[];for(e in n)i=n[e],r.push(i.lookUp(t));return r},t.prototype.onKeyup=function(t){var e;switch(t.keyCode){case h.ESC:t.preventDefault(),null!=(e=this.controller())&&e.view.hide();break;case h.DOWN:case h.UP:case h.CTRL:case h.ENTER:l.noop();break;case h.P:case h.N:t.ctrlKey||this.dispatch(t);break;default:this.dispatch(t)}},t.prototype.onKeydown=function(t){var e,i=null!=(e=this.controller())?e.view:void 0;if(i&&i.visible())switch(t.keyCode){case h.ESC:t.preventDefault(),i.hide(t);break;case h.UP:t.preventDefault(),i.prev();break;case h.DOWN:t.preventDefault(),i.next();break;case h.P:if(!t.ctrlKey)return;t.preventDefault(),i.prev();break;case h.N:if(!t.ctrlKey)return;t.preventDefault(),i.next();break;case h.TAB:case h.ENTER:case h.SPACE:if(!i.visible())return;if(!this.controller().getOpt("spaceSelectsMatch")&&t.keyCode===h.SPACE)return;if(!this.controller().getOpt("tabSelectsMatch")&&t.keyCode===h.TAB)return;i.highlighted()?(t.preventDefault(),i.choose(t)):i.hide(t);break;default:l.noop()}};var o=t,n=[].slice;function e(t,e){this.app=t,this.at=e,this.$inputor=this.app.$inputor,this.id=this.$inputor[0].id||this.uid(),this.expectedQueryCBId=null,this.setting=null,this.query=null,this.pos=0,this.range=null,0===(this.$el=l("#atwho-ground-"+this.id,this.app.$el)).length&&this.app.$el.append(this.$el=l('<div id="atwho-ground-'+this.id+'"></div>')),this.model=new p(this),this.view=new f(this)}e.prototype.uid=function(){return(Math.random().toString(16)+"000000000").substr(2,8)+(new Date).getTime()},e.prototype.init=function(t){return this.setting=l.extend({},this.setting||l.fn.atwho.default,t),this.view.init(),this.model.reload(this.setting.data)},e.prototype.destroy=function(){return this.trigger("beforeDestroy"),this.model.destroy(),this.view.destroy(),this.$el.remove()},e.prototype.callDefault=function(){var e=arguments[0],t=2<=arguments.length?n.call(arguments,1):[];try{return i[e].apply(this,t)}catch(t){return l.error(t+" Or maybe At.js doesn't have function "+e)}},e.prototype.trigger=function(t,e){var i;return(e=null==e?[]:e).push(this),i=this.getOpt("alias"),this.$inputor.trigger(i?t+"-"+i+".atwho":t+".atwho",e)},e.prototype.callbacks=function(t){return this.getOpt("callbacks")[t]||i[t]},e.prototype.getOpt=function(t){try{return this.setting[t]}catch(t){return null}},e.prototype.insertContentFor=function(t){var e=this.getOpt("insertTpl"),t=l.extend({},t.data("item-data"),{"atwho-at":this.at});return this.callbacks("tplEval").call(this,e,t,"onInsert")},e.prototype.renderView=function(t){var e=this.getOpt("searchKey");return t=this.callbacks("sorter").call(this,this.query.text,t.slice(0,1001),e),this.view.render(t.slice(0,this.getOpt("limit")))},e.arrayToDefaultHash=function(t){var e,i,n,r;if(!l.isArray(t))return t;for(r=[],e=0,n=t.length;e<n;e++)i=t[e],l.isPlainObject(i)?r.push(i):r.push({name:i});return r},e.prototype.lookUp=function(t){var e;if((!t||"click"!==t.type||this.getOpt("lookUpOnClick"))&&(!this.getOpt("suspendOnComposing")||!this.app.isComposing))return(e=this.catchQuery(t))?(this.app.setContextFor(this.at),(t=this.getOpt("delay"))?this._delayLookUp(e,t):this._lookUp(e)):this.expectedQueryCBId=null,e},e.prototype._delayLookUp=function(t,e){var i,n,r=Date.now?Date.now():(new Date).getTime();return this.previousCallTime||(this.previousCallTime=r),0<(i=e-(r-this.previousCallTime))&&i<e?(this.previousCallTime=r,this._stopDelayedCall(),this.delayedCallTimeout=setTimeout((n=this,function(){return n.previousCallTime=0,n.delayedCallTimeout=null,n._lookUp(t)}),e)):(this._stopDelayedCall(),this.previousCallTime!==r&&(this.previousCallTime=0),this._lookUp(t))},e.prototype._stopDelayedCall=function(){if(this.delayedCallTimeout)return clearTimeout(this.delayedCallTimeout),this.delayedCallTimeout=null},e.prototype._generateQueryCBId=function(){return{}},e.prototype._lookUp=function(t){var e=function(t,e){if(t===this.expectedQueryCBId)return e&&0<e.length?this.renderView(this.constructor.arrayToDefaultHash(e)):this.view.hide()};return this.expectedQueryCBId=this._generateQueryCBId(),this.model.query(t.text,l.proxy(e,this,this.expectedQueryCBId))};var r,s=e,a={}.hasOwnProperty,u=((r=function(t,e){for(var i in e)a.call(e,i)&&(t[i]=e[i]);function n(){this.constructor=t}return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t})(c,s),c.prototype.catchQuery=function(){var t=this.$inputor.val(),e=this.$inputor.caret("pos",{iframe:this.app.iframe}),i=t.slice(0,e),t=this.callbacks("matcher").call(this,this.at,i,this.getOpt("startWithSpace"),this.getOpt("acceptSpaceBar")),i="string"==typeof t;if(!(i&&t.length<this.getOpt("minLen",0)))return i&&t.length<=this.getOpt("maxLen",20)?(e=(i=e-t.length)+t.length,this.pos=i,this.trigger("matched",[this.at,(t={text:t,headPos:i,endPos:e}).text])):(t=null,this.view.hide()),this.query=t},c.prototype.rect=function(){var t,e;if(t=this.$inputor.caret("offset",this.pos-1,{iframe:this.app.iframe}))return this.app.iframe&&!this.app.iframeAsRoot&&(e=l(this.app.iframe).offset(),t.left+=e.left,t.top+=e.top),e=this.app.document.selection?0:2,{left:t.left,top:t.top,bottom:t.top+t.height+e}},c.prototype.insert=function(t){var e=this.$inputor,i=e.val(),n=i.slice(0,Math.max(this.query.headPos-this.at.length,0)),r=""===(r=this.getOpt("suffix"))?r:r||" ",i=""+n+(t+=r)+i.slice(this.query.endPos||0);return e.val(i),e.caret("pos",n.length+t.length,{iframe:this.app.iframe}),e.is(":focus")||e.focus(),e.change()},c);function c(){return c.__super__.constructor.apply(this,arguments)}var p,f,d,g=(r(y,s),y.prototype._getRange=function(){var t=this.app.window.getSelection();if(0<t.rangeCount)return t.getRangeAt(0)},y.prototype._setRange=function(t,e,i){if((i=null==i?this._getRange():i)&&e)return e=l(e)[0],"after"===t?(i.setEndAfter(e),i.setStartAfter(e)):(i.setEndBefore(e),i.setStartBefore(e)),i.collapse(!1),this._clearRange(i)},y.prototype._clearRange=function(t){var e;if(null==t&&(t=this._getRange()),e=this.app.window.getSelection(),null==this.ctrl_a_pressed)return e.removeAllRanges(),e.addRange(t)},y.prototype._movingEvent=function(t){var e;return"click"===t.type||(e=t.which)===h.RIGHT||e===h.LEFT||e===h.UP||e===h.DOWN},y.prototype._unwrap=function(t){var e;return(e=(t=l(t).unwrap().get(0)).nextSibling)&&e.nodeValue&&(t.nodeValue+=e.nodeValue,l(e).remove()),t},y.prototype.catchQuery=function(t){var e,i,n,r,o,s,a;if((a=this._getRange())&&a.collapsed&&null!=t){if(t.which===h.ENTER)return(i=l(a.startContainer).closest(".atwho-query")).contents().unwrap(),i.is(":empty")&&i.remove(),(i=l(".atwho-query",this.app.document)).text(i.text()).contents().last().unwrap(),void this._clearRange();if(/firefox/i.test(navigator.userAgent)){if(l(a.startContainer).is(this.$inputor))return void this._clearRange();t.which===h.BACKSPACE&&a.startContainer.nodeType===document.ELEMENT_NODE&&0<=(o=a.startOffset-1)?((n=a.cloneRange()).setStart(a.startContainer,o),l(n.cloneContents()).contents().last().is(".atwho-inserted")&&(o=l(a.startContainer).contents().get(o),this._setRange("after",l(o).contents().last()))):t.which===h.LEFT&&a.startContainer.nodeType===document.TEXT_NODE&&(e=l(a.startContainer.previousSibling)).is(".atwho-inserted")&&0===a.startOffset&&this._setRange("after",e.contents().last())}if(l(a.startContainer).closest(".atwho-inserted").addClass("atwho-query").siblings().removeClass("atwho-query"),0<(i=l(".atwho-query",this.app.document)).length&&i.is(":empty")&&0===i.text().length&&i.remove(),this._movingEvent(t)||i.removeClass("atwho-inserted"),0<i.length)switch(t.which){case h.LEFT:return this._setRange("before",i.get(0),a),void i.removeClass("atwho-query");case h.RIGHT:return this._setRange("after",i.get(0).nextSibling,a),void i.removeClass("atwho-query")}return 0<i.length&&(s=i.attr("data-atwho-at-query"))&&(i.empty().html(s).attr("data-atwho-at-query",null),this._setRange("after",i.get(0),a)),(n=a.cloneRange()).setStart(a.startContainer,0),n="string"==typeof(s=this.callbacks("matcher").call(this,this.at,n.toString(),this.getOpt("startWithSpace"),this.getOpt("acceptSpaceBar"))),0===i.length&&n&&0<=(r=a.startOffset-this.at.length-s.length)&&(a.setStart(a.startContainer,r),i=l("<span/>",this.app.document).attr(this.getOpt("editableAtwhoQueryAttrs")).addClass("atwho-query"),a.surroundContents(i.get(0)),(r=i.contents().last().get(0))&&(/firefox/i.test(navigator.userAgent)?(a.setStart(r,r.length),a.setEnd(r,r.length),this._clearRange(a)):this._setRange("after",r,a))),n&&s.length<this.getOpt("minLen",0)?void 0:n&&s.length<=this.getOpt("maxLen",20)?(this.trigger("matched",[this.at,(s={text:s,el:i}).text]),this.query=s):(this.view.hide(),this.query={el:i},0<=i.text().indexOf(this.at)&&(this._movingEvent(t)&&i.hasClass("atwho-inserted")?i.removeClass("atwho-query"):!1!==this.callbacks("afterMatchFailed").call(this,this.at,i)&&this._setRange("after",this._unwrap(i.text(i.text()).contents().first()))),null)}},y.prototype.rect=function(){var t,e=this.query.el.offset();if(e&&this.query.el[0].getClientRects().length)return this.app.iframe&&!this.app.iframeAsRoot&&(t=l(this.app.iframe).offset(),e.left+=t.left-this.$inputor.scrollLeft(),e.top+=t.top-this.$inputor.scrollTop()),e.bottom=e.top+this.query.el.height(),e},y.prototype.insert=function(t,e){var i;return this.$inputor.is(":focus")||this.$inputor.focus(),(i=this.getOpt("functionOverrides")).insert?i.insert.call(this,t,e):(i=""===(i=this.getOpt("suffix"))?i:i||" ",e=e.data("item-data"),this.query.el.removeClass("atwho-query").addClass("atwho-inserted").html(t).attr("data-atwho-at-query",""+e["atwho-at"]+this.query.text).attr("contenteditable","false"),(e=this._getRange())&&(this.query.el.length&&e.setEndAfter(this.query.el[0]),e.collapse(!1),e.insertNode(i=this.app.document.createTextNode(""+i)),this._setRange("after",i,e)),this.$inputor.is(":focus")||this.$inputor.focus(),this.$inputor.change())},y);function y(){return y.__super__.constructor.apply(this,arguments)}function v(t){this.context=t,this.at=this.context.at,this.storage=this.context.$inputor}function w(t){this.context=t,this.$el=l('<div class="atwho-view"><ul class="atwho-view-ul"></ul></div>'),this.$elUl=this.$el.children(),this.timeoutID=null,this.context.$el.append(this.$el),this.bindEvent()}v.prototype.destroy=function(){return this.storage.data(this.at,null)},v.prototype.saved=function(){return 0<this.fetch()},v.prototype.query=function(t,e){var i=this.fetch(),n=this.context.getOpt("searchKey");return i=this.context.callbacks("filter").call(this.context,t,i,n)||[],n=this.context.callbacks("remoteFilter"),0<i.length||!n&&0===i.length?e(i):n.call(this.context,t,e)},v.prototype.fetch=function(){return this.storage.data(this.at)||[]},v.prototype.save=function(t){return this.storage.data(this.at,this.context.callbacks("beforeSave").call(this.context,t||[]))},v.prototype.load=function(t){if(!this.saved()&&t)return this._load(t)},v.prototype.reload=function(t){return this._load(t)},v.prototype._load=function(t){return"string"==typeof t?l.ajax(t,{dataType:"json"}).done((e=this,function(t){return e.save(t)})):this.save(t);var e},p=v,w.prototype.init=function(){var t=this.context.getOpt("alias")||this.context.at.charCodeAt(0),e=this.context.getOpt("headerTpl");return e&&1===this.$el.children().length&&this.$el.prepend(e),this.$el.attr({id:"at-view-"+t})},w.prototype.destroy=function(){return this.$el.remove()},w.prototype.bindEvent=function(){var e,i=this.$el.find("ul"),n=0,r=0;i.on("mousemove.atwho-view","li",function(t){var e;if((n!==t.clientX||r!==t.clientY)&&(n=t.clientX,r=t.clientY,!(e=l(t.currentTarget)).hasClass("cur")))return i.find(".cur").removeClass("cur"),e.addClass("cur")}).on("click.atwho-view","li",(e=this,function(t){return i.find(".cur").removeClass("cur"),l(t.currentTarget).addClass("cur"),e.choose(t),t.preventDefault()}))},w.prototype.visible=function(){return l.expr.filters.visible(this.$el[0])},w.prototype.highlighted=function(){return 0<this.$el.find(".cur").length},w.prototype.choose=function(t){var e,i;if((e=this.$el.find(".cur")).length&&(i=this.context.insertContentFor(e),this.context._stopDelayedCall(),this.context.insert(this.context.callbacks("beforeInsert").call(this.context,i,e,t),e),this.context.trigger("inserted",[e,t]),this.hide(t)),this.context.getOpt("hideWithoutSuffix"))return this.stopShowing=!0},w.prototype.reposition=function(t){var e=window!==window.top,i=e?window:window.top,i=l(i);return e&&(e=l(window.frameElement).offset(),t.top+=e.top,t.bottom-=e.top,t.left-=e.left),t.bottom+this.$el.height()-i.scrollTop()>i.height()&&(t.bottom=t.top-this.$el.height()),t.left>(i=i.width()-this.$el.width()-5)&&(t.left=i),i={left:t.left,top:t.bottom},null!=(t=this.context.callbacks("beforeReposition"))&&t.call(this.context,i),this.$el.offset(i),this.context.trigger("reposition",[i])},w.prototype.next=function(){var t=this.$el.find(".cur").removeClass("cur").next();return(t=!t.length?this.$el.find("li:first"):t).addClass("cur"),t=(t=t[0]).offsetTop+t.offsetHeight+(t.nextSibling?t.nextSibling.offsetHeight:0),this.scrollTop(Math.max(0,t-this.$el.height()))},w.prototype.prev=function(){var t=this.$el.find(".cur").removeClass("cur").prev();return(t=!t.length?this.$el.find("li:last"):t).addClass("cur"),t=(t=t[0]).offsetTop+t.offsetHeight+(t.nextSibling?t.nextSibling.offsetHeight:0),this.scrollTop(Math.max(0,t-this.$el.height()))},w.prototype.scrollTop=function(t){var e=this.context.getOpt("scrollDuration");return e?this.$elUl.animate({scrollTop:t},e):this.$elUl.scrollTop(t)},w.prototype.show=function(){var t;if(!this.stopShowing)return this.visible()||(this.$el.show(),this.$el.scrollTop(0),this.context.trigger("shown")),(t=this.context.rect())?this.reposition(t):void 0;this.stopShowing=!1},w.prototype.hide=function(t,e){var i;if(this.visible())return isNaN(e)?(this.$el.hide(),this.context.trigger("hidden",[t])):(i=this,t=function(){return i.hide()},clearTimeout(this.timeoutID),this.timeoutID=setTimeout(t,e))},w.prototype.render=function(t){var e,i,n,r,o,s;if(l.isArray(t)&&0<t.length){for(this.$el.find("ul").empty(),e=this.$el.find("ul"),s=this.context.getOpt("displayTpl"),i=0,r=t.length;i<r;i++)n=t[i],n=l.extend({},n,{"atwho-at":this.context.at}),o=this.context.callbacks("tplEval").call(this.context,s,n,"onDisplay"),(o=l(this.context.callbacks("highlighter").call(this.context,o,this.context.query.text))).data("item-data",n),e.append(o);return this.show(),this.context.getOpt("highlightFirst")?e.find("li:first").addClass("cur"):void 0}this.hide()},f=w,d={load:function(t,e){if(t=this.controller(t))return t.model.load(e)},isSelecting:function(){var t;return!(null==(t=this.controller())||!t.view.visible())},hide:function(){var t;return null!=(t=this.controller())?t.view.hide():void 0},reposition:function(){var t;if(t=this.controller())return t.view.reposition(t.rect())},setIframe:function(t,e){return this.setupRootElement(t,e),null},run:function(){return this.dispatch()},destroy:function(){return this.shutdown(),this.$inputor.data("atwho",null)}},l.fn.atwho=function(i){var n=arguments,r=null;return this.filter('textarea, input, [contenteditable=""], [contenteditable=true]').each(function(){var t,e;return(e=(t=l(this)).data("atwho"))||t.data("atwho",e=new o(this)),"object"!=typeof i&&i?d[i]&&e?r=d[i].apply(e,Array.prototype.slice.call(n,1)):l.error("Method "+i+" does not exist on jQuery.atwho"):e.reg(i.at,i)}),null!=r?r:this},l.fn.atwho.default={at:void 0,alias:void 0,data:null,displayTpl:"<li>${name}</li>",insertTpl:"${atwho-at}${name}",headerTpl:null,callbacks:i,functionOverrides:{},searchKey:"name",suffix:void 0,hideWithoutSuffix:!1,startWithSpace:!0,acceptSpaceBar:!1,highlightFirst:!0,limit:5,maxLen:20,minLen:0,displayTimeout:300,delay:null,spaceSelectsMatch:!1,tabSelectsMatch:!0,editableAtwhoQueryAttrs:{},scrollDuration:150,suspendOnComposing:!0,lookUpOnClick:!0},l.fn.atwho.debug=!1});