/*
*
*  Ajax Autocomplete for jQuery, version 1.0.3
*  (c) 2009 Tomas Kirda
*
*  Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
*  For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
*
*  Last Review: 11:30 AM 3/24/2009
*
*/
(function(b) { b.fn.autocomplete = function(c) { return this.each(function() { return new a(this, c); }); }; var a = function(d, c) { this.el = b(d); this.id = this.el.attr("id"); this.el.attr("autocomplete", "off"); this.suggestions = []; this.data = []; this.badQueries = []; this.selectedIndex = -1; this.currentValue = this.el.val(); this.intervalId = 0; this.cachedResponse = []; this.onChangeInterval = null; this.ignoreValueChange = false; this.serviceUrl = c.serviceUrl; this.options = { autoSubmit: false, minChars: 1, maxHeight: 300, deferRequestBy: 0, width: 0, highlight: true }; if (c) { b.extend(this.options, c); } this.initialize(); }; a.isArray = function(c) { return c && c.constructor === Array; }; a.highlight = function(d, c) { return d.replace(c, function(e) { return "<strong>" + e + "</strong>"; }); }; a.prototype = { killerFn: null, initialize: function() { var e = this; this.killerFn = function(f) { if (b(f.target).parents(".autocomplete").size() === 0) { e.killSuggestions(); e.disableKillerFn(); } }; var c = new Date().getTime(); var d = "Autocomplete_" + c; if (!this.options.width) { this.options.width = this.el.width(); } this.mainContainerId = "AutocompleteContainter_" + c; b('<div id="' + this.mainContainerId + '" style="position:absolute;"><div class="autocomplete-w1"><div class="autocomplete-w2"><div class="autocomplete" id="' + d + '" style="display:none; width:' + this.options.width + 'px;"></div></div></div></div>').appendTo("body"); this.container = b("#" + d); this.fixPosition(); if (window.opera) { this.el.keypress(function(f) { e.onKeyPress(f); }); } else { this.el.keydown(function(f) { e.onKeyPress(f); }); } this.el.keyup(function(f) { e.onKeyUp(f); }); this.el.blur(function() { e.enableKillerFn(); }); this.el.focus(function() { e.fixPosition(); }); this.container.css({ maxHeight: this.options.maxHeight + "px" }); }, fixPosition: function() { var c = this.el.offset(); b("#" + this.mainContainerId).css({ top: (c.top + this.el.height()) + "px", left: c.left + "px" }); }, enableKillerFn: function() { var c = this; b(document).bind("click", c.killerFn); }, disableKillerFn: function() { var c = this; b(document).unbind("click", c.killerFn); }, killSuggestions: function() { var c = this; this.stopKillSuggestions(); this.intervalId = window.setInterval(function() { c.hide(); c.stopKillSuggestions(); }, 300); }, stopKillSuggestions: function() { window.clearInterval(this.intervalId); }, onKeyPress: function(c) { if (!this.enabled) { return; } switch (c.keyCode) { case 27: this.el.val(this.currentValue); this.hide(); break; case 9: case 13: if (this.selectedIndex === -1) { this.hide(); return; } this.select(this.selectedIndex); if (c.keyCode === 9) { return; } break; case 38: this.moveUp(); break; case 40: this.moveDown(); break; default: return; } c.stopImmediatePropagation(); c.preventDefault(); }, onKeyUp: function(c) { switch (c.keyCode) { case 38: case 40: return; } clearInterval(this.onChangeInterval); if (this.currentValue !== this.el.val()) { if (this.options.deferRequestBy > 0) { this.onChangeInterval = setInterval((function() { this.onValueChange(); }).bind(this), this.options.deferRequestBy); } else { this.onValueChange(); } } }, onValueChange: function() { clearInterval(this.onChangeInterval); this.currentValue = this.el.val(); this.selectedIndex = -1; if (this.ignoreValueChange) { this.ignoreValueChange = false; return; } if (this.currentValue === "" || this.currentValue.length < this.options.minChars) { this.hide(); } else { this.getSuggestions(); } }, getSuggestions: function() { var d = this.cachedResponse[this.currentValue]; if (d && a.isArray(d.suggestions)) { this.suggestions = d.suggestions; this.data = d.data; this.suggest(); } else { if (!this.isBadQuery(this.currentValue)) { var c = this; b.get(this.serviceUrl, { query: this.currentValue }, function(e) { c.processResponse(e); }, "json"); } } }, isBadQuery: function(d) { var c = this.badQueries.length; while (c--) { if (d.indexOf(this.badQueries[c]) === 0) { return true; } } return false; }, hide: function() { this.enabled = false; this.selectedIndex = -1; this.container.hide(); }, suggest: function() { if (this.suggestions.length === 0) { this.hide(); return; } if (this.options.highlight) { var g = this.currentValue.match(/\S+/g); var e = new RegExp("\\b" + g.join("|\\b"), "gi"); } var f = this; var c = this.suggestions.length; var h; this.container.html(""); for (var d = 0; d < c; d++) { h = b((f.selectedIndex === d ? '<div class="selected"' : "<div") + ' title="' + f.suggestions[d] + '">' + a.highlight(f.suggestions[d], e) + "</div>"); h.mouseover((function(i) { return function() { f.activate(i); }; })(d)); h.click((function(i) { return function() { f.select(i); }; })(d)); this.container.append(h); } this.enabled = true; this.container.show(); }, processResponse: function(d) { var c; try { c = d; if (!a.isArray(c.data)) { c.data = []; } } catch (e) { return; } this.suggestions = c.suggestions; this.data = c.data; this.cachedResponse[c.query] = c; if (c.suggestions.length === 0) { this.badQueries.push(c.query); } if (c.query === this.currentValue) { this.suggest(); } }, activate: function(c) { var d = this.container.children(); var e; if (this.selectedIndex !== -1 && d.length > this.selectedIndex) { b(d.get(this.selectedIndex)).attr("class", ""); } this.selectedIndex = c; if (this.selectedIndex !== -1 && d.length > this.selectedIndex) { e = d.get(this.selectedIndex); b(e).attr("class", "selected"); } return e; }, deactivate: function(d, c) { d.className = ""; if (this.selectedIndex === c) { this.selectedIndex = -1; } }, select: function(d) { var c = this.suggestions[d]; if (c) { this.el.val(c); if (this.options.autoSubmit) { var e = this.el.parents("form"); if (e.length > 0) { e.get(0).submit(); } } this.ignoreValueChange = true; this.hide(); this.onSelect(d); } }, moveUp: function() { if (this.selectedIndex === -1) { return; } if (this.selectedIndex === 0) { this.container.children().get(0).className = ""; this.selectedIndex = -1; this.el.val(this.currentValue); return; } this.adjustScroll(this.selectedIndex - 1); }, moveDown: function() { if (this.selectedIndex === (this.suggestions.length - 1)) { return; } this.adjustScroll(this.selectedIndex + 1); }, adjustScroll: function(c) { var g, d, e, f; g = this.activate(c); d = g.offsetTop; e = this.container.scrollTop(); f = e + this.options.maxHeight - 25; if (d < e) { this.container.scrollTop(d); } else { if (d > f) { this.container.scrollTop(d - this.options.maxHeight + 25); } } this.el.val(this.suggestions[c]); }, onSelect: function(c) { (this.options.onSelect || function() { })(this.suggestions[c], this.data[c]); } }; })(jQuery);
