/*
  jQuery-SelectBox
  
  Traditional select elements are very difficult to style by themselves, 
  but they are also very usable and feature rich. This plugin attempts to 
  recreate all selectbox functionality and appearance while adding 
  animation and stylability.
  
  This product includes software developed 
  by RevSystems, Inc (http://www.revsystems.com/) and its contributors
  
  Please see the accompanying LICENSE.txt for licensing information.
*/
(function (a, y, ha) { a.fn.borderWidth = function () { return a(this).outerWidth() - a(this).innerWidth() }; a.fn.paddingWidth = function () { return a(this).innerWidth() - a(this).width() }; a.fn.extraWidth = function () { return a(this).outerWidth(true) - a(this).width() }; a.fn.offsetFrom = function (m) { m = a(m); return { left: a(this).offset().left - m.offset().left, top: a(this).offset().top - m.offset().top} }; a.fn.maxWidth = function () { var m = 0; a(this).each(function () { if (a(this).width() > m) m = a(this).width() }); return m }; a.fn.triggerAll = function (m, d) { return a(this).each(function () { a(this).triggerHandler(m, d) }) }; var L = Array.prototype.slice, D = function () { return Math.floor(Math.random() * 999999999) }; a.proto = function (m, d, g) { var o = d, f = {}; opts = a.extend({ elem: "elem", access: "access", init: "init", instantAccess: false }, g); if (d._super) { f[opts.init] = function () { }; o = d.extend(f) } a.fn[m] = function () { var h, e = arguments; a(this).each(function () { var n = a(this), k = n.data(m), s = !k; if (s) { k = new o; if (d._super) k[opts.init] = d.prototype.init; k[opts.elem] = n[0]; if (k[opts.init]) k[opts.init].apply(k, opts.instantAccess ? [] : L.call(e, 0)); n.data(m, k) } if (!s || opts.instantAccess) { k[opts.access] && k[opts.access].apply(k, L.call(e, 0)); if (e.length > 0) if (a.isFunction(k[e[0]])) h = k[e[0]].apply(k, L.call(e, 1)); else if (e.length === 1) h = a.getObject ? a.getObject(e[0], k) : k[e[0]]; else if (a.setObject) a.setObject(e[0], e[1], k); else k[e[0]] = e[1]; else if (h === void 0) h = n.data(m) } }); if (h === void 0) return a(this); return h } }; var M = function () { return false }; a.proto("sb", function () { var m = this, d = {}, g = null, o = null, f = null, h = null, e = null, n = null, k = "", s = null, S = null, T = null, U, E, V, W, F, z, X, A, p, v, G, N, O, Y, Z, B, w, $, aa, H, I, ba, P, Q, C, x, q, J, ca, da, ea, fa, K, R, r, ga; U = function () { f = a("<div class='sb " + d.selectboxClass + " " + g.attr("class") + "' id='sb" + D() + "'></div>").attr("role", "listbox").attr("aria-has-popup", "true").attr("aria-labelledby", o.attr("id") ? o.attr("id") : ""); a("body").append(f); var b = g.children().size() > 0 ? d.displayFormat.call(g.find("option:selected")[0], 0, 0) : "&nbsp;"; h = a("<div class='display " + g.attr("class") + "' id='sbd" + D() + "'></div>").append("<div class='text'>" + b + "</div>").append(d.arrowMarkup); f.append(h); e = a("<ul class='" + d.selectboxClass + " items " + g.attr("class") + "' role='menu' id='sbdd" + D() + "'></ul>").attr("aria-hidden", "true"); f.append(e).attr("aria-owns", e.attr("id")); g.children().size() === 0 ? e.append(E()) : g.children().each(function (c) { var j, i, l, t; if (a(this).is("optgroup")) { i = a(this); l = a("<li class='optgroup'>" + d.optgroupFormat.call(i[0], c + 1) + "</li>").addClass(i.is(":disabled") ? "disabled" : "").attr("aria-disabled", i.is(":disabled") ? "true" : ""); t = a("<ul class='items'></ul>"); l.append(t); e.append(l); i.children("option").each(function () { j = E(a(this), c).addClass(i.is(":disabled") ? "disabled" : "").attr("aria-disabled", i.is(":disabled") ? "true" : ""); t.append(j) }) } else e.append(E(a(this), c)) }); n = e.find("li").not(".optgroup"); f.attr("aria-active-descendant", n.filter(".selected").attr("id")); e.children(":first").addClass("first"); e.children(":last").addClass("last"); if (d.fixedWidth) d.maxWidth && f.width() > d.maxWidth && f.width(d.maxWidth); else { b = e.find(".text, .optgroup").maxWidth() + h.extraWidth() + 1; f.width(d.maxWidth ? Math.min(d.maxWidth, b) : b) } g.before(f).addClass("has_sb").hide().show(); G(); ga(); e.hide(); if (g.is(":disabled")) { f.addClass("disabled").attr("aria-disabled"); h.click(function (c) { c.preventDefault() }) } else { g.bind("blur.sb", W).bind("focus.sb", V); h.mousedown(ba).mousedown(Y).click(M).focus($).blur(aa).hover(H, I); x().click(Z).hover(H, I); e.find(".optgroup").hover(H, I).click(M); n.filter(".disabled").click(M); if (!a.browser.msie || a.browser.version >= 9) a(y).resize(a.throttle ? a.throttle(100, N) : O) } f.bind("close.sb", v).bind("destroy.sb", F); g.bind("reload.sb", z); a.fn.tie && d.useTie && g.bind("domupdate.sb", X) }; O = function () { clearTimeout(T); T = setTimeout(N, 50) }; N = function () { if (f.is(".open")) { G(); A(true) } }; E = function (b, c) { if (!b) { b = a("<option value=''>&nbsp;</option>"); c = 0 } var j = a("<li id='sbo" + D() + "'></li>").attr("role", "option").data("orig", b[0]).data("value", b ? b.attr("value") : "").addClass(b.is(":selected") ? "selected" : "").addClass(b.is(":disabled") ? "disabled" : "").attr("aria-disabled", b.is(":disabled") ? "true" : ""), i = a("<div class='item'></div>"), l = a("<div class='text'></div>").html(d.optionFormat.call(b[0], 0, c + 1)); return j.append(i.append(l)) }; V = function () { R(); h.triggerHandler("focus") }; W = function () { h.triggerHandler("blur") }; F = function (b) { f.remove(); g.unbind(".sb").removeClass("has_sb"); a(y).unbind("resize", O); b || g.removeData("sb") }; z = function () { if (f) { var b = f.is(".open"), c = h.is(".focused"); v(true); F(true); m.init(d); if (b) { g.focus(); A(true) } else c && g.focus() } }; X = function () { clearTimeout(S); S = setTimeout(z, 30) }; K = function () { v(); a(document).unbind("click", K) }; R = function () { a(".sb.focused." + d.selectboxClass).not(f[0]).find(".display").blur() }; fa = function () { a(".sb.open." + d.selectboxClass).not(f[0]).triggerAll("close") }; v = function (b) { if (f.is(".open")) { h.blur(); n.removeClass("hover"); a(document).unbind("keyup", B).unbind("keydown", r).unbind("keypress", r).unbind("keydown", w); e.attr("aria-hidden", "true"); if (b === true) { e.hide(); f.removeClass("open"); f.append(e) } else e.fadeOut(d.animDuration, function () { f.removeClass("open"); f.append(e) }) } }; Q = function () { var b = null; return b = d.ddCtx === "self" ? f : a.isFunction(d.ddCtx) ? a(d.ddCtx.call(g[0])) : a(d.ddCtx) }; C = function () { return n.filter(".selected") }; x = function () { return n.not(".disabled") }; p = function () { e.scrollTop(e.scrollTop() + C().offsetFrom(e).top - e.height() / 2 + C().outerHeight(true) / 2) }; ga = function () { a.browser.msie && a.browser.version < 8 && a("." + d.selectboxClass + " .display").hide().show() }; A = function (b) { var c; c = Q(); R(); f.addClass("open"); c.append(e); c = G(); e.attr("aria-hidden", "false"); if (b === true) { e.show(); p() } else c === "down" ? e.slideDown(d.animDuration, p) : e.fadeIn(d.animDuration, p); a(document).click(K); g.focus() }; G = function () { var b = Q(), c = 0, j = h.offsetFrom(b).left, i = 0, l = "", t, u; e.removeClass("above"); e.show().css({ maxHeight: "none", position: "relative", visibility: "hidden" }); d.fixedWidth || e.width(h.outerWidth() - e.extraWidth() + 1); c = a(y).scrollTop() + a(y).height() - h.offset().top - h.outerHeight(); l = h.offset().top - a(y).scrollTop(); i = h.offsetFrom(b).top + h.outerHeight(); u = c - l + d.dropupThreshold; if (e.outerHeight() < c) { c = d.maxHeight ? d.maxHeight : c; i = i; l = "down" } else if (e.outerHeight() < l) { c = d.maxHeight ? d.maxHeight : l; i = h.offsetFrom(b).top - Math.min(c, e.outerHeight()); l = "up" } else if (u >= 0) { c = d.maxHeight ? d.maxHeight : c; i = i; l = "down" } else if (u < 0) { c = d.maxHeight ? d.maxHeight : l; i = h.offsetFrom(b).top - Math.min(c, e.outerHeight()); l = "up" } else { c = d.maxHeight ? d.maxHeight : "none"; i = i; l = "down" } t = ("" + a("body").css("margin-left")).match(/^\d+/) ? a("body").css("margin-left") : 0; u = ("" + a("body").css("margin-top")).match(/^\d+/) ? a("body").css("margin-top") : 0; t = a().jquery >= "1.4.2" ? parseInt(t) : a("body").offset().left; u = a().jquery >= "1.4.2" ? parseInt(u) : a("body").offset().top; e.hide().css({ left: j + (b.is("body") ? t : 0), maxHeight: c, position: "absolute", top: i + (b.is("body") ? u : 0), visibility: "visible" }); l === "up" && e.addClass("above"); return l }; Y = function () { f.is(".open") ? v() : A(); return false }; q = function () { var b = a(this), c = g.val(), j = b.data("value"); if (d.useTie && a.fn.tie) { g.find("option").old_removeAttr("selected"); a(b.data("orig")).old_attr("selected", "selected") } else { g.find("option").removeAttr("selected"); a(b.data("orig")).attr("selected", "selected") } x().removeClass("selected"); b.addClass("selected"); f.attr("aria-active-descendant", b.attr("id")); h.find(".text").attr("title", b.find(".text").html()); h.find(".text").html(d.displayFormat.call(b.data("orig"))); c !== j && g.change() }; Z = function () { K(); g.focus(); q.call(this); return false }; J = function () { k = "" }; ca = function (b) { var c, j, i = x(); for (c = 0; c < i.size(); c++) { j = i.eq(c).find(".text"); j = j.children().size() == 0 ? j.text() : j.find("*").text(); if (b.length > 0 && j.toLowerCase().match("^" + b.toLowerCase())) return i.eq(c) } return null }; da = function (b) { b = ca(b); if (b !== null) { q.call(b[0]); return true } return false }; r = function (b) { if (!(b.ctrlKey || b.altKey)) if (b.which === 38 || b.which === 40 || b.which === 8 || b.which === 32) b.preventDefault() }; ea = function (b) { var c, j; c = C(); var i = x(); for (c = i.index(c) + 1; c < i.size(); c++) { j = i.eq(c).find(".text").text(); if (j !== "" && j.substring(0, 1).toLowerCase() === b.toLowerCase()) { q.call(i.eq(c)[0]); return true } } return false }; w = function (b) { if (b.altKey || b.ctrlKey) return false; var c = C(), j = x(); switch (b.which) { case 35: if (c.size() > 0) { b.preventDefault(); q.call(j.filter(":last")[0]); p() } break; case 36: if (c.size() > 0) { b.preventDefault(); q.call(j.filter(":first")[0]); p() } break; case 38: if (c.size() > 0) { if (j.filter(":first")[0] !== c[0]) { b.preventDefault(); q.call(j.eq(j.index(c) - 1)[0]) } p() } break; case 40: if (c.size() > 0) { if (j.filter(":last")[0] !== c[0]) { b.preventDefault(); q.call(j.eq(j.index(c) + 1)[0]); p() } } else if (n.size() > 1) { b.preventDefault(); q.call(n.eq(0)[0]) } } }; B = function (b) { if (b.altKey || b.ctrlKey) return false; if (b.which !== 38 && b.which !== 40) { k += String.fromCharCode(b.keyCode); if (da(k)) { clearTimeout(s); s = setTimeout(J, d.acTimeout) } else if (ea(String.fromCharCode(b.keyCode))) { p(); clearTimeout(s); s = setTimeout(J, d.acTimeout) } else { J(); clearTimeout(s) } } }; $ = function () { fa(); f.addClass("focused"); a(document).unbind("keyup", B).keyup(B).unbind("keypress", r).keypress(r).unbind("keydown", r).keydown(r).keydown(w).unbind("keydown", w).keydown(w) }; aa = function () { f.removeClass("focused"); a(document).unbind("keyup", B).unbind("keydown", r).unbind("keypress", r).unbind("keydown", w) }; H = function () { a(this).addClass("hover") }; I = function () { a(this).removeClass("hover") }; ba = function () { h.addClass("active"); a(document).bind("mouseup", P) }; P = function () { h.removeClass("active"); a(document).unbind("mouseup", P) }; this.init = function (b) { if (!(a.browser.msie && a.browser.version < 7)) { g = a(this.elem); if (g.attr("id")) o = a("label[for='" + g.attr("id") + "']:first"); if (!o || o.size() === 0) o = g.closest("label"); if (!g.hasClass("has_sb")) { d = a.extend({ acTimeout: 800, animDuration: 200, ddCtx: "body", dropupThreshold: 150, fixedWidth: false, maxHeight: false, maxWidth: false, selectboxClass: "selectbox", useTie: false, arrowMarkup: "<div class='arrow_btn'><span class='arrow'></span></div>", displayFormat: ha, optionFormat: function () { if (a(this).size() > 0) { var c = a(this).attr("label"); if (c && c.length > 0) return c; return a(this).text() } else return "" }, optgroupFormat: function () { return "<span class='label'>" + a(this).attr("label") + "</span>" } }, b); d.displayFormat = d.displayFormat || d.optionFormat; U() } } }; this.open = A; this.close = v; this.refresh = z; this.destroy = F; this.options = function (b) { d = a.extend(d, b); z() } }) })(jQuery, window);
