(function(){var A={};window.RentalsSearchBox=A.exports=(function(){var F=function(G){this._init(G)};var D=F.prototype;var C=this;var B=5;var E=C.ListenerMixin||require("../listener-mixin");D.onUpdate=function(G){this._addListener("up",G);return this};D.setInput=function(G){this._setInput(G);return this};D.forceApts=function(){this._isApartments=true;return this};D.getQuery=function(){return this._query};D.getHood=function(){return this._hood};D.hasUniqueResult=function(){return this._uniqueResult()};D.autoSelect=function(){return this._autoSelect()};E(D);D._init=function(G){this.app=G;this._isApartments=window.location.pathname.indexOf("/apartments")==0;this._fetch()};D._setInput=function(G){(this._input=G).autocomplete({source:_(this._source).bind(this),select:_(this._select).bind(this),delay:0})};D._source=function(H,N){if(!this._all){return N([])}var L=this._all,G=L.length,M=H.term.toLowerCase().replace(/[^a-z ]/g,""),K=M.length,J=[];for(var I=0;I<G;I++){if(L[I][0].toLowerCase().replace(/[^a-z ]/g,"").substring(0,K)==M){J.push({value:L[I][0],hood:L[I][1]});if(J.length==B){break}}}N(this._current=J)};D._select=function(H,G){this._hood=G.item.hood;this._query=G.item.value;this._input.val(this._query);delete (this._current);this._track(G.autoSelect);G.item.path||(G.item.path=(this._isApartments?"/apartments/":"/")+_(G.item.value.split(", ").reverse()).map(encodeLocalityName).join("/"));this._alertListeners("up",this._selected=G.item)};D._track=function(G){var H=G?"(auto match autocomplete)":"(autocomplete)";trackEvent(ACTIVE_COMPONENT,"apt search "+H,this._query)};D._fetch=function(){$.ajax({url:"/apartments/suggest",dataType:"json",success:_(this._receive).bind(this),error:_(this._receive).bind(this,null)})};D._receive=function(G){this._all=G};D._uniqueResult=function(){return((this._current||[]).length==1)?this._current[0]:undefined};D._autoSelect=function(){var G=this._uniqueResult();if(G){this._input.autocomplete("close");this._select(null,{item:G,autoSelect:true});return true}return false};return F})()})();
