﻿(function (a) { a.fn.extend({ autocomplete: function (b, c) { var d = typeof b == "string"; c = a.extend({}, a.Autocompleter.defaults, { url: d ? b : null, data: d ? null : b, delay: d ? a.Autocompleter.defaults.delay : 10, max: c && !c.scroll ? 10 : 150 }, c); c.highlight = c.highlight || function (a) { return a }; c.formatMatch = c.formatMatch || c.formatItem; c.resultsClass = c.resultsClass || c.resultsClass; return this.each(function () { new a.Autocompleter(this, c) }) }, result: function (a) { return this.bind("result", a) }, search: function (a) { return this.trigger("search", [a]) }, flushCache: function () { return this.trigger("flushCache") }, setOptions: function (a) { return this.trigger("setOptions", [a]) }, unautocomplete: function () { return this.trigger("unautocomplete") } }); a.fn.focusNextInputField = function () { return this.each(function () { var b = a(this).parents("form:eq(0),body").find("button,input,textarea,select"); var c = b.index(this); if (c > -1 && c + 1 < b.length) { b.eq(c + 1).focus() } return false }) }; a.Autocompleter = function (b, c) { function x() { e.removeClass(c.loadingClass) } function w(b) { var d = []; var e = b.split("\n"); for (var f = 0; f < e.length; f++) { var g = a.trim(e[f]); if (g) { g = g.split("|"); d[d.length] = { data: g, value: g[0], result: c.formatResult && c.formatResult(g, g[0]) || g[0]} } } return d } function v(d, e, f) { if (!c.matchCase) d = d.toLowerCase(); var g = h.load(d); if (g && g.length) { e(d, g) } else if (typeof c.url == "string" && c.url.length > 0) { var i = { timestamp: +(new Date) }; a.each(c.extraParams, function (a, b) { i[a] = typeof b == "function" ? b() : b }); a.ajax({ mode: "abort", port: "autocomplete" + b.name, dataType: c.dataType, url: c.url, data: a.extend({ q: q(d), limit: c.max }, i), success: function (a) { var b = c.parse && c.parse(a) || w(a); h.add(d, b); e(d, b) } }) } else { l.emptyList(); f(d) } } function u(a, b) { if (b && b.length && i) { x(); l.display(b, a); r(a, b[0].value); l.show() } else { t() } } function t() { var d = l.visible(); l.hide(); clearTimeout(f); x(); if (c.mustMatch) { e.search(function (a) { if (!a) { if (c.multiple) { var b = p(e.val()).slice(0, -1); e.val(b.join(c.multipleSeparator) + (b.length ? c.multipleSeparator : "")) } else e.val("") } }) } if (d) a.Autocompleter.Selection(b, b.value.length, b.value.length) } function s() { clearTimeout(f); f = setTimeout(t, 200); l.hide() } function r(f, h) { if (c.autoFill && q(e.val()).toLowerCase() == f.toLowerCase() && j != d.BACKSPACE) { e.val(e.val() + h.substring(q(g).length)); a.Autocompleter.Selection(b, g.length, g.length + h.length) } } function q(a) { if (!c.multiple) return a; var b = p(a); return b[b.length - 1] } function p(b) { if (!b) { return [""] } var d = b.split(c.multipleSeparator); var e = []; a.each(d, function (b, c) { if (a.trim(c)) e[b] = a.trim(c) }); return e } function o(a, b) { if (j == d.DEL) { l.hide(); return } var f = e.val(); if (!b && f == g) return; g = f; f = q(f); if (f.length >= c.minChars) { e.addClass(c.loadingClass); if (!c.matchCase) f = f.toLowerCase(); v(f, u, t) } else { x(); l.hide() } } function n() { var a = l.selected(); if (!a) return false; var b = a.result; g = b; if (c.multiple) { var d = p(e.val()); if (d.length > 1) { b = d.slice(0, d.length - 1).join(c.multipleSeparator) + c.multipleSeparator + b } b += c.multipleSeparator } e.val(b); t(); e.trigger("result", [a.data, a.value]); return true } var d = { UP: 38, DOWN: 40, DEL: 46, RETURN: 13, ESC: 27, COMMA: 188, PAGEUP: 33, PAGEDOWN: 34, BACKSPACE: 8 }; var e = a(b).attr("autocomplete", "off").addClass(c.inputClass); var f; var g = ""; var h = a.Autocompleter.Cache(c); var i = 0; var j; var k = { mouseDownOnSelect: false }; var l = a.Autocompleter.Select(c, b, n, k); var m; a.browser.opera && a(b.form).bind("submit.autocomplete", function () { if (m) { m = false; return false } }); e.bind((a.browser.opera ? "keypress" : "keydown") + ".autocomplete", function (b) { j = b.keyCode; switch (b.keyCode) { case d.UP: b.preventDefault(); if (l.visible()) { l.prev() } else { o(0, true) } break; case d.DOWN: b.preventDefault(); if (l.visible()) { l.next() } else { o(0, true) } break; case d.PAGEUP: b.preventDefault(); if (l.visible()) { l.pageUp() } else { o(0, true) } break; case d.PAGEDOWN: b.preventDefault(); if (l.visible()) { l.pageDown() } else { o(0, true) } break; case c.multiple && a.trim(c.multipleSeparator) == "," && d.COMMA: case d.RETURN: if (n()) { b.preventDefault(); var e = a(this).parent().parent(); var g = e.prevAll().length - 1; a("#productcode_" + g + "").focusNextInputField(); m = true; return false } break; case d.ESC: l.hide(); break; default: clearTimeout(f); f = setTimeout(o, c.delay); break } }).focus(function () { i++ }).blur(function () { i = 0; if (!k.mouseDownOnSelect) { s() } }).click(function () { if (i++ > 1 && !l.visible()) { o(0, true) } }).bind("search", function () { function c(a, c) { var d; if (c && c.length) { for (var f = 0; f < c.length; f++) { if (c[f].result.toLowerCase() == a.toLowerCase()) { d = c[f]; break } } } if (typeof b == "function") b(d); else e.trigger("result", d && [d.data, d.value]) } var b = arguments.length > 1 ? arguments[1] : null; a.each(p(e.val()), function (a, b) { v(b, c, c) }) }).bind("flushCache", function () { h.flush() }).bind("setOptions", function () { a.extend(c, arguments[1]); if ("data" in arguments[1]) h.populate() }).bind("unautocomplete", function () { l.unbind(); e.unbind(); a(b.form).unbind(".autocomplete") }); }; a.Autocompleter.defaults = { inputClass: "ac_input", resultsClass: "ac_results", loadingClass: "ac_loading", minChars: 1, delay: 400, matchCase: false, matchSubset: true, matchContains: false, cacheLength: 10, max: 100, mustMatch: false, extraParams: {}, selectFirst: true, formatItem: function (a) { return a[0] }, formatMatch: null, autoFill: false, width: 0, multiple: false, multipleSeparator: ", ", highlight: function (a, b) { return a.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + b.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>") }, scroll: true, scrollHeight: 180 }; a.Autocompleter.Cache = function (b) { function h() { c = {}; d = 0 } function g() { if (!b.data) return false; var c = {}, d = 0; if (!b.url) b.cacheLength = 1; c[""] = []; for (var e = 0, g = b.data.length; e < g; e++) { var h = b.data[e]; h = typeof h == "string" ? [h] : h; var i = b.formatMatch(h, e + 1, b.data.length); if (i === false) continue; var j = i.charAt(0).toLowerCase(); if (!c[j]) c[j] = []; var k = { value: i, data: h, result: b.formatResult && b.formatResult(h) || i }; c[j].push(k); if (d++ < b.max) { c[""].push(k) } } a.each(c, function (a, c) { b.cacheLength++; f(a, c) }) } function f(a, e) { if (d > b.cacheLength) { h() } if (!c[a]) { d++ } c[a] = e } function e(a, c) { if (!b.matchCase) a = a.toLowerCase(); var d = a.indexOf(c); if (b.matchContains == "word") { d = a.toLowerCase().search("\\b" + c.toLowerCase()) } if (d == -1) return false; return d == 0 || b.matchContains } var c = {}; var d = 0; setTimeout(g, 25); return { flush: h, add: f, populate: g, load: function (f) { if (!b.cacheLength || !d) return null; if (!b.url && b.matchContains) { var g = []; for (var h in c) { if (h.length > 0) { var i = c[h]; a.each(i, function (a, b) { if (e(b.value, f)) { g.push(b) } }) } } return g } else if (c[f]) { return c[f] } else if (b.matchSubset) { for (var j = f.length - 1; j >= b.minChars; j--) { var i = c[f.substr(0, j)]; if (i) { var g = []; a.each(i, function (a, b) { if (e(b.value, f)) { g[g.length] = b } }); return g } } } return null } } }; a.Autocompleter.Select = function (b, c, d, e) { function s() { m.empty(); var c = r(i.length); for (var d = 0; d < c; d++) { if (!i[d]) continue; var e = b.formatItem(i[d].data, d + 1, c, i[d].value, j); if (e === false) continue; var k = a("<li/>").html(b.highlight(e, j)).addClass(d % 2 == 0 ? "ac_even" : "ac_odd").appendTo(m)[0]; a.data(k, "ac_data", i[d]) } g = m.find("li"); if (b.selectFirst) { g.slice(0, 1).addClass(f.ACTIVE); h = 0 } if (a.fn.bgiframe) m.bgiframe() } function r(a) { return b.max && b.max < a ? b.max : a } function q(a) { h += a; if (h < 0) { h = g.size() - 1 } else if (h >= g.size()) { h = 0 } } function p(a) { g.slice(h, h + 1).removeClass(f.ACTIVE); q(a); var c = g.slice(h, h + 1).addClass(f.ACTIVE); if (b.scroll) { var d = 0; g.slice(0, h).each(function () { d += this.offsetHeight }); if (d + c[0].offsetHeight - m.scrollTop() > m[0].clientHeight) { m.scrollTop(d + c[0].offsetHeight - m.innerHeight()) } else if (d < m.scrollTop()) { m.scrollTop(d) } } } function o(a) { var b = a.target; while (b && b.tagName != "LI") b = b.parentNode; if (!b) return []; return b } function n() { if (!k) return; l = a("<div/>").hide().addClass(b.resultsClass).css("position", "absolute").appendTo(document.body); m = a("<ul/>").appendTo(l).mouseover(function (b) { if (o(b).nodeName && o(b).nodeName.toUpperCase() == "LI") { h = a("li", m).removeClass(f.ACTIVE).index(o(b)); a(o(b)).addClass(f.ACTIVE) } }).click(function (b) { a(o(b)).addClass(f.ACTIVE); d(); c.focus(); return false }).mousedown(function () { e.mouseDownOnSelect = true }).mouseup(function () { e.mouseDownOnSelect = false }); if (b.width > 0) l.css("width", b.width); k = false } var f = { ACTIVE: "ac_over" }; var g, h = -1, i, j = "", k = true, l, m; return { display: function (a, b) { n(); i = a; j = b; s() }, next: function () { p(1) }, prev: function () { p(-1) }, pageUp: function () { if (h != 0 && h - 8 < 0) { p(-h) } else { p(-8) } }, pageDown: function () { if (h != g.size() - 1 && h + 8 > g.size()) { p(g.size() - 1 - h) } else { p(8) } }, hide: function () { l && l.hide(); g && g.removeClass(f.ACTIVE); h = -1 }, visible: function () { return l && l.is(":visible") }, current: function () { return this.visible() && (g.filter("." + f.ACTIVE)[0] || b.selectFirst && g[0]) }, show: function () { var d = a(c).offset(); l.css({ width: typeof b.width == "string" || b.width > 0 ? b.width : a(c).width(), top: d.top + c.offsetHeight, left: d.left }).show(); if (b.scroll) { m.scrollTop(0); m.css({ maxHeight: b.scrollHeight, overflow: "auto" }); if (a.browser.msie && typeof document.body.style.maxHeight === "undefined") { var e = 0; g.each(function () { e += this.offsetHeight }); var f = e > b.scrollHeight; m.css("height", f ? b.scrollHeight : e); if (!f) { g.width(m.width() - parseInt(g.css("padding-left")) - parseInt(g.css("padding-right"))) } } } }, selected: function () { var b = g && g.filter("." + f.ACTIVE).removeClass(f.ACTIVE); return b && b.length && a.data(b[0], "ac_data") }, emptyList: function () { m && m.empty() }, unbind: function () { l && l.remove() } } }; a.Autocompleter.Selection = function (a, b, c) { if (a.createTextRange) { var d = a.createTextRange(); d.collapse(true); d.moveStart("character", b); d.moveEnd("character", c); d.select() } else if (a.setSelectionRange) { a.setSelectionRange(b, c) } else { if (a.selectionStart) { a.selectionStart = b; a.selectionEnd = c } } } })(jQuery)
