/*
 * This code is based on the Autocomplete function in jQuery.
 * 
 * jQuery UI Autocomplete 1.8.4
 * 
 * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about) Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 * 
 * http://docs.jquery.com/UI/Autocomplete
 * 
 * Depends: jquery.ui.core.js jquery.ui.widget.js jquery.ui.position.js
 */
(function(e) {
    e.widget("ui.autocomplete.nsb", {
        options : {
            appendTo : "body",
            delay : 25,
            minLength : 1,
            position : {
                my : "left top",
                at : "left bottom",
                collision : "none"
            },
            source : null
        },
        _create : function() {
            var a = this, b = this.element[0].ownerDocument;
            this.element.addClass("ui-autocomplete-input").attr("autocomplete", "off").attr({
                role : "textbox",
                "aria-autocomplete" : "list",
                "aria-haspopup" : "true"
            }).bind("keydown.autocomplete", function(c) {
                if (!a.options.disabled) {
                    var d = e.ui.keyCode;
                    switch (c.keyCode) {
                    case d.PAGE_UP:
                        a._move("previousPage", c);
                        break;
                    case d.PAGE_DOWN:
                        a._move("nextPage", c);
                        break;
                    case d.UP:
                        a._move("previous", c);
                        c.preventDefault();
                        break;
                    case d.DOWN:
                        a._move("next", c);
                        c.preventDefault();
                        break;
                    case d.ENTER:
                    case d.NUMPAD_ENTER:
                        a.menu.element.is(":visible") && c.preventDefault();
                    case d.TAB:
                        // Refreshing list (forcing filtering)
                        a.selectedItem = null;
                        a.search(null, c);

                        // Activate the new top item, if any
                        len = a.menu.element.context.children.length;
                        if (len >= 1) {
                            a.menu.activate(c, a.menu.element.children(0));
                        }
                        break;
                    case d.ESCAPE:
                        a.element.val(a.term);
                        a.close(c);
                        break;
                    default:
                        clearTimeout(a.searching);
                        a.searching = setTimeout(function() {
                            if (a.term != a.element.val()) {
                                a.selectedItem = null;
                                a.search(null, c)
                            }
                        }, a.options.delay);
                        break
                    }
                }
            }).bind("focus.autocomplete", function() {
                if (!a.options.disabled) {
                    a.selectedItem = null;
                    a.previous = a.element.val()
                }
            }).bind("blur.autocomplete", function(c) {
                if (!a.options.disabled) {
                    clearTimeout(a.searching);
                    a.closing = setTimeout(function() {
                        a.close(c);
                        a._change(c)
                    }, 150)
                }
            });
            this._initSource();
            this.response = function() {
                return a._response.apply(a, arguments)
            };
            this.menu = e("<ul></ul>").addClass("ui-autocomplete").appendTo(e(this.options.appendTo || "body", b)[0])
                    .mousedown(function(c) {
                        var d = a.menu.element[0];
                        c.target === d && setTimeout(function() {
                            e(document).one("mousedown", function(f) {
                                f.target !== a.element[0] && f.target !== d && !e.ui.contains(d, f.target) && a.close()
                            })
                        }, 1);
                        setTimeout(function() {
                            clearTimeout(a.closing)
                        }, 13)
                    }).menu({
                        focus : function(c, d) {
                            d = d.item.data("item.autocomplete");
                            false !== a._trigger("focus", null, {
                                item : d
                            }) && /^key/.test(c.originalEvent.type) && a.element.val(d.value)
                        },
                        selected : function(c, d) {
                            if (d == null || d.item == null) {
                                return;
                            }
                            d = d.item.data("item.autocomplete");
                            var f = a.previous;
                            if (a.element[0] !== b.activeElement) {
                                a.element.focus();
                                a.previous = f
                            }
                            false !== a._trigger("select", c, {
                                item : d
                            }) && a.element.val(d.value);
                            a.close(c);
                            a.selectedItem = d
                        },
                        blur : function() {
                            a.menu.element.is(":visible") && a.element.val() !== a.term && a.element.val(a.term)
                        }
                    }).zIndex(this.element.zIndex() + 1).css({
                        top : 0,
                        left : 0
                    }).hide().data("menu");
            e.fn.bgiframe && this.menu.element.bgiframe()
        },
        destroy : function() {
            this.element.removeClass("ui-autocomplete-input").removeAttr("autocomplete").removeAttr("role").removeAttr(
                    "aria-autocomplete").removeAttr("aria-haspopup");
            this.menu.element.remove();
            e.Widget.prototype.destroy.call(this)
        },
        _setOption : function(a, b) {
            e.Widget.prototype._setOption.apply(this, arguments);
            a === "source" && this._initSource();
            if (a === "appendTo")
                this.menu.element.appendTo(e(b || "body", this.element[0].ownerDocument)[0])
        },
        _initSource : function() {
            var a, b;
            if (e.isArray(this.options.source)) {
                a = this.options.source;
                this.source = function(c, d) {
                    d(e.ui.autocomplete.filter(a, c.term))
                }
            } else if (typeof this.options.source === "string") {
                b = this.options.source;
                this.source = function(c, d) {
                    e.getJSON(b, c, d)
                }
            } else
                this.source = this.options.source
        },
        search : function(a, b) {
            a = a != null ? a : this.element.val();
            if (a.length < this.options.minLength)
                return this.close(b);
            clearTimeout(this.closing);
            if (this._trigger("search") !== false)
                return this._search(a)
        },
        _search : function(a) {
            this.term = this.element.addClass("ui-autocomplete-loading").val();
            this.source({
                term : a
            }, this.response)
        },
        _response : function(a) {
            // Endre@Stolsvik.com: Always _suggest(a), so that we get to know if list.length == 0.
            a = this._normalize(a);
            this._suggest(a);
            if (a.length) {
                this._trigger("open")
            } else
                this.close();
            this.element.removeClass("ui-autocomplete-loading")
        },
        close : function(a) {
            clearTimeout(this.closing);
            if (this.menu.element.is(":visible")) {
                this._trigger("close", a);
                this.menu.element.hide();
                this.menu.deactivate()
            }
        },
        _change : function(a) {
            this.previous !== this.element.val() && this._trigger("change", a, {
                item : this.selectedItem
            })
        },
        _normalize : function(a) {
            if (a.length && a[0].label && a[0].value)
                return a;
            return e.map(a, function(b) {
                if (typeof b === "string")
                    return {
                        label : b,
                        value : b
                    };
                return e.extend({
                    label : b.label || b.value,
                    value : b.value || b.label
                }, b)
            })
        },
        _suggest : function(a) {
            var b = this.menu.element.empty().zIndex(this.element.zIndex() + 1), c;
            this._renderMenu(b, a);
            this.menu.deactivate();
            this.menu.refresh();
            this.menu.element.show().position(e.extend({
                of : this.element
            }, this.options.position));
            a = b.width("").outerWidth();
            c = this.element.outerWidth();
            b.outerWidth(Math.max(a, c))
        },
        _renderMenu : function(a, b) {
            var c = this;
            e.each(b, function(d, f) {
                c._renderItem(a, f)
            })
        },
        _renderItem : function(a, b) {
            return e("<li></li>").data("item.autocomplete", b).append(e("<a></a>").text(b.label)).appendTo(a)
        },
        _move : function(a, b) {
            if (this.menu.element.is(":visible"))
                if (this.menu.first() && /^previous/.test(a) || this.menu.last() && /^next/.test(a)) {
                    this.element.val(this.term);
                    this.menu.deactivate()
                } else
                    this.menu[a](b);
            else
                this.search(null, b)
        },
        widget : function() {
            return this.menu.element
        }
    });
    e.extend(e.ui.autocomplete, {
        escapeRegex : function(a) {
            return a.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
        },
        filter : function(a, b) {
            var c = new RegExp(e.ui.autocomplete.escapeRegex(b), "i");
            return e.grep(a, function(d) {
                return c.test(d.label || d.value || d)
            })
        }
    })
})(jQuery);
(function(e) {
    e
            .widget(
                    "ui.menu",
                    {
                        _create : function() {
                            var a = this;
                            this.element.addClass("ui-menu ui-widget ui-widget-content ui-corner-all").attr({
                                role : "listbox",
                                "aria-activedescendant" : "ui-active-menuitem"
                            }).click(function(b) {
                                if (e(b.target).closest(".ui-menu-item a").length) {
                                    b.preventDefault();
                                    a.select(b)
                                }
                            });
                            this.refresh()
                        },
                        refresh : function() {
                            var a = this;
                            this.element.children("li:not(.ui-menu-item):has(a)").addClass("ui-menu-item").attr("role",
                                    "menuitem").children("a").addClass("ui-corner-all").attr("tabindex", -1)
                                    .mouseenter(function(b) {
                                        a.activate(b, e(this).parent())
                                    }).mouseleave(function() {
                                        a.deactivate()
                                    })
                        },
                        activate : function(a, b) {
                            this.deactivate();
                            if (this.hasScroll()) {
                                var c = b.offset().top - this.element.offset().top, d = this.element.attr("scrollTop"), f = this.element
                                        .height();
                                if (c < 0)
                                    this.element.attr("scrollTop", d + c);
                                else
                                    c > f && this.element.attr("scrollTop", d + c - f + b.height())
                            }
                            this.active = b.eq(0).children("a").addClass("ui-state-hover").attr("id",
                                    "ui-active-menuitem").end();
                            this._trigger("focus", a, {
                                item : b
                            })
                        },
                        deactivate : function() {
                            if (this.active) {
                                this.active.children("a").removeClass("ui-state-hover").removeAttr("id");
                                this._trigger("blur");
                                this.active = null
                            }
                        },
                        next : function(a) {
                            this.move("next", ".ui-menu-item:first", a)
                        },
                        previous : function(a) {
                            this.move("prev", ".ui-menu-item:last", a)
                        },
                        first : function() {
                            return this.active && !this.active.prevAll(".ui-menu-item").length
                        },
                        last : function() {
                            return this.active && !this.active.nextAll(".ui-menu-item").length
                        },
                        move : function(a, b, c) {
                            if (this.active) {
                                a = this.active[a + "All"](".ui-menu-item").eq(0);
                                a.length ? this.activate(c, a) : this.activate(c, this.element.children(b))
                            } else
                                this.activate(c, this.element.children(b))
                        },
                        nextPage : function(a) {
                            if (this.hasScroll())
                                if (!this.active || this.last())
                                    this.activate(a, this.element.children(":first"));
                                else {
                                    var b = this.active.offset().top, c = this.element.height(), d = this.element
                                            .children("li").filter(function() {
                                                var f = e(this).offset().top - b - c + e(this).height();
                                                return f < 10 && f > -10
                                            });
                                    d.length || (d = this.element.children(":last"));
                                    this.activate(a, d)
                                }
                            else
                                this.activate(a, this.element
                                        .children(!this.active || this.last() ? ":first" : ":last"))
                        },
                        previousPage : function(a) {
                            if (this.hasScroll())
                                if (!this.active || this.first())
                                    this.activate(a, this.element.children(":last"));
                                else {
                                    var b = this.active.offset().top, c = this.element.height();
                                    result = this.element.children("li").filter(function() {
                                        var d = e(this).offset().top - b + c - e(this).height();
                                        return d < 10 && d > -10
                                    });
                                    result.length || (result = this.element.children(":first"));
                                    this.activate(a, result)
                                }
                            else
                                this.activate(a, this.element.children(!this.active || this.first() ? ":last"
                                        : ":first"))
                        },
                        hasScroll : function() {
                            return this.element.height() < this.element.attr("scrollHeight")
                        },
                        select : function(a) {
                            this._trigger("selected", a, {
                                item : this.active
                            })
                        }
                    })
})(jQuery);;
