!function(a,b,c,d){a.fn.search=function(c,e){var f,g=a(this),h=a.extend(!0,{},a.fn.search.settings,e),i="."+h.namespace,j=h.namespace+"-module",k=g.selector||"",l=(new Date).getTime(),m=[],n=arguments[0],o="string"==typeof n,p=[].slice.call(arguments,1);return a(this).each(function(){var e,g=a(this),q=g.find(h.selector.prompt),r=g.find(h.selector.searchButton),s=g.find(h.selector.results),t=(g.find(h.selector.result),g.find(h.selector.category),this),u=g.data(j),v=h.className,w=h.errors;e={initialize:function(){e.verbose("Initializing module");var a=q[0],b=a.oninput!==d?"input":a.onpropertychange!==d?"propertychange":"keyup";q.on("focus"+i,e.event.focus).on("blur"+i,e.event.blur).on("keydown"+i,e.handleKeyboard),h.automatic&&q.on(b+i,e.search.throttle),r.on("click"+i,e.search.query),s.on("click"+i,h.selector.result,e.results.select),e.instantiate()},instantiate:function(){e.verbose("Storing instance of module",e),g.data(j,e)},destroy:function(){e.verbose("Destroying instance"),g.removeData(j)},event:{focus:function(){g.addClass(v.focus),e.results.show()},blur:function(){e.search.cancel(),g.removeClass(v.focus),e.results.hide()}},handleKeyboard:function(b){var c,d=g.find(h.selector.result),f=g.find(h.selector.category),i=b.which,j={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},k=v.active,l=d.index(d.filter("."+k)),m=d.size();if(i==j.escape&&q.trigger("blur"),s.filter(":visible").size()>0)if(i==j.enter){if(d.filter("."+k).exists())return a.proxy(e.results.select,d.filter("."+k))(),b.preventDefault(),!1}else i==j.upArrow?(c=0>l-1?l:l-1,f.removeClass(k),d.removeClass(k).eq(c).addClass(k).closest(f).addClass(k),b.preventDefault()):i==j.downArrow&&(c=l+1>=m?l:l+1,f.removeClass(k),d.removeClass(k).eq(c).addClass(k).closest(f).addClass(k),b.preventDefault());else i==j.enter&&(e.search.query(),r.addClass(v.down),q.one("keyup",function(){r.removeClass(v.down)}))},search:{cancel:function(){var a=g.data("xhr")||!1;a&&"resolved"!=a.state()&&(e.debug("Cancelling last search"),a.abort())},throttle:function(){var a=q.val(),b=a.length;clearTimeout(e.timer),b>=h.minCharacters?e.timer=setTimeout(e.search.query,h.searchThrottle):e.results.hide()},query:function(){var b=q.val(),d=e.search.cache.read(b);d?(e.debug("Reading result for '"+b+"' from cache"),e.results.add(d)):(e.debug("Querying for '"+b+"'"),"object"==typeof c?e.search.local(b):e.search.remote(b),a.proxy(h.onSearchQuery,g)(b))},local:function(b){var d,f=[],i=[],j=a.isArray(h.searchFields)?h.searchFields:[h.searchFields],k=new RegExp("(?:s|^)"+b,"i"),l=new RegExp(b,"i");g.addClass(v.loading),a.each(j,function(b,d){a.each(c,function(b,c){"string"==typeof c[d]&&-1==a.inArray(c,f)&&-1==a.inArray(c,i)&&(k.test(c[d])?f.push(c):l.test(c[d])&&i.push(c))})}),d=e.results.generate({results:a.merge(f,i)}),g.removeClass(v.loading),e.search.cache.write(b,d),e.results.add(d)},remote:function(b){var d,f={stateContext:g,url:c,urlData:{query:b},success:function(a){d=e.results.generate(a),e.search.cache.write(b,d),e.results.add(d)},failure:e.error};e.search.cancel(),e.debug("Executing search"),a.extend(!0,f,h.apiSettings),a.api(f)},cache:{read:function(a){var b=g.data("cache");return h.cache&&"object"==typeof b&&b[a]!==d?b[a]:!1},write:function(a,b){var c=g.data("cache")!==d?g.data("cache"):{};c[a]=b,g.data("cache",c)}}},results:{generate:function(b){e.debug("Generating html from response",b);var c=h.templates[h.type],d="";return a.isPlainObject(b.results)&&!a.isEmptyObject(b.results)||a.isArray(b.results)&&b.results.length>0?(h.maxResults>0&&(b.results=a.makeArray(b.results).slice(0,h.maxResults)),b.results.length>0&&(a.isFunction(c)?d=c(b):e.error(w.noTemplate,!1))):d=e.message(w.noResults,"empty"),a.proxy(h.onResults,g)(b),d},add:function(b){("default"==h.onResultsAdd||"default"==a.proxy(h.onResultsAdd,s)(b))&&s.html(b),e.results.show()},show:function(){0===s.filter(":visible").size()&&q.filter(":focus").size()>0&&""!==s.html()&&(s.stop().fadeIn(200),a.proxy(h.onResultsOpen,s)())},hide:function(){s.filter(":visible").size()>0&&(s.stop().fadeOut(200),a.proxy(h.onResultsClose,s)())},select:function(c){e.debug("Search result selected");var d=a(this),f=d.find(".title"),g=f.html();if("default"==h.onSelect||"default"==a.proxy(h.onSelect,this)(c)){var i=d.find("a[href]").eq(0),j=i.attr("href")||!1,k=i.attr("target")||!1;e.results.hide(),q.val(g),j&&("_blank"==k||c.ctrlKey?b.open(j):b.location.href=j)}}},setting:function(b,c){return e.debug("Changing setting",b,c),c===d?h[b]:(a.isPlainObject(b)?a.extend(!0,h,b):h[b]=c,void 0)},internal:function(b,c){return e.debug("Changing internal",b,c),c===d?e[b]:(a.isPlainObject(b)?a.extend(!0,e,b):e[b]=c,void 0)},debug:function(){h.debug&&(h.performance?e.performance.log(arguments):e.debug=Function.prototype.bind.call(console.info,console,h.moduleName+":"))},verbose:function(){h.verbose&&h.debug&&(h.performance?e.performance.log(arguments):e.verbose=Function.prototype.bind.call(console.info,console,h.moduleName+":"))},error:function(){e.error=Function.prototype.bind.call(console.log,console,h.moduleName+":")},performance:{log:function(a){var b,c,d;h.performance&&(b=(new Date).getTime(),d=l||b,c=b-d,l=b,m.push({Element:t,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(e.performance.timer),e.performance.timer=setTimeout(e.performance.display,100)},display:function(){var b=h.moduleName+":",c=0;l=!1,a.each(m,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",k&&(b+=" '"+k+"'"),(console.group!==d||console.table!==d)&&m.length>0&&(console.groupCollapsed(b),console.table?console.table(m):a.each(m,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),m=[]}},invoke:function(b,c,f){var g,h;return c=c||p,f=t||f,"string"==typeof b&&u!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(b,c){return a.isPlainObject(u[c])&&b!=g?(u=u[c],!0):u[c]!==d?(h=u[c],!0):(e.error(w.method),!1)})),a.isFunction(h)?h.apply(f,c):h||!1}},o?(u===d&&e.initialize(),f=e.invoke(n)):(u!==d&&e.destroy(),e.initialize())}),f?f:this},a.fn.search.settings={moduleName:"Search Module",namespace:"search",debug:!0,verbose:!0,performance:!0,onSelect:"default",onResultsAdd:"default",onSearchQuery:function(){},onResults:function(){},onResultsOpen:function(){},onResultsClose:function(){},automatic:"true",type:"simple",minCharacters:3,searchThrottle:300,maxResults:7,cache:!0,searchFields:["title","description"],apiSettings:{},className:{active:"active",down:"down",focus:"focus",empty:"empty",loading:"loading"},errors:{noResults:"Your search returned no results",logging:"Error in debug logging, exiting.",noTemplate:"A valid template name was not specified.",serverError:"There was an issue with querying the server.",method:"The method you called is not defined."},selector:{prompt:".prompt",searchButton:".search.button",results:".results",category:".category",result:".result"},templates:{message:function(a,b){var c="";return a!==d&&b!==d&&(c+='
',c+="empty"==b?'
No Results
'+a+'
':'
'+a+"
",c+="
"),c},categories:function(b){var c="";return b.results!==d?(a.each(b.results,function(b,e){e.results!==d&&e.results.length>0&&(c+='
'+e.name+"
",a.each(e.results,function(a,b){c+='
',c+='',b.image!==d&&(c+='
'+"
"),c+='
',b.price!==d&&(c+='
'+b.price+"
"),b.title!==d&&(c+='
'+b.title+"
"),b.description!==d&&(c+='
'+b.description+"
"),c+="
"}),c+="
")}),b.resultPage&&(c+=''+b.resultPage.text+""),c):!1},simple:function(b){var c="";return b.results!==d?(a.each(b.results,function(a,b){c+='',b.image!==d&&(c+='
'+"
"),c+='
',b.price!==d&&(c+='
'+b.price+"
"),b.title!==d&&(c+='
'+b.title+"
"),b.description!==d&&(c+='
'+b.description+"
"),c+="
"}),b.resultPage&&(c+=''+b.resultPage.text+""),c):!1}}}}(jQuery,window,document);