/*
 * jQuery UI Position 1.8
 *
 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Position
 */
(function (f) {
    f.ui = f.ui || {};
    var c = /left|center|right/,
        e = "center",
        d = /top|center|bottom/,
        g = "center",
        a = f.fn.position,
        b = f.fn.offset;
    f.fn.position = function (i) {
        if (!i || !i.of) {
            return a.apply(this, arguments)
        }
        i = f.extend({}, i);
        var l = f(i.of),
            n = (i.collision || "flip").split(" "),
            m = i.offset ? i.offset.split(" ") : [0, 0],
            k, h, j;
        if (i.of.nodeType === 9) {
            k = l.width();
            h = l.height();
            j = {
                top: 0,
                left: 0
            }
        } else {
            if (i.of.scrollTo && i.of.document) {
                k = l.width();
                h = l.height();
                j = {
                    top: l.scrollTop(),
                    left: l.scrollLeft()
                }
            } else {
                if (i.of.preventDefault) {
                    i.at = "left top";
                    k = h = 0;
                    j = {
                        top: i.of.pageY,
                        left: i.of.pageX
                    }
                } else {
                    k = l.outerWidth();
                    h = l.outerHeight();
                    j = l.offset()
                }
            }
        }
        f.each(["my", "at"], function () {
            var o = (i[this] || "").split(" ");
            if (o.length === 1) {
                o = c.test(o[0]) ? o.concat([g]) : d.test(o[0]) ? [e].concat(o) : [e, g]
            }
            o[0] = c.test(o[0]) ? o[0] : e;
            o[1] = d.test(o[1]) ? o[1] : g;
            i[this] = o
        });
        if (n.length === 1) {
            n[1] = n[0]
        }
        m[0] = parseInt(m[0], 10) || 0;
        if (m.length === 1) {
            m[1] = m[0]
        }
        m[1] = parseInt(m[1], 10) || 0;
        if (i.at[0] === "right") {
            j.left += k
        } else {
            if (i.at[0] === e) {
                j.left += k / 2
            }
        }
        if (i.at[1] === "bottom") {
            j.top += h
        } else {
            if (i.at[1] === g) {
                j.top += h / 2
            }
        }
        j.left += m[0];
        j.top += m[1];
        return this.each(function () {
            var r = f(this),
                q = r.outerWidth(),
                p = r.outerHeight(),
                o = f.extend({}, j);
            if (i.my[0] === "right") {
                o.left -= q
            } else {
                if (i.my[0] === e) {
                    o.left -= q / 2
                }
            }
            if (i.my[1] === "bottom") {
                o.top -= p
            } else {
                if (i.my[1] === g) {
                    o.top -= p / 2
                }
            }
            f.each(["left", "top"], function (t, s) {
                if (f.ui.position[n[t]]) {
                    f.ui.position[n[t]][s](o, {
                        targetWidth: k,
                        targetHeight: h,
                        elemWidth: q,
                        elemHeight: p,
                        offset: m,
                        my: i.my,
                        at: i.at
                    })
                }
            });
            if (f.fn.bgiframe) {
                r.bgiframe()
            }
            r.offset(f.extend(o, {
                using: i.using
            }))
        })
    };
    f.ui.position = {
        fit: {
            left: function (h, i) {
                var k = f(window),
                    j = h.left + i.elemWidth - k.width() - k.scrollLeft();
                h.left = j > 0 ? h.left - j : Math.max(0, h.left)
            },
            top: function (h, i) {
                var k = f(window),
                    j = h.top + i.elemHeight - k.height() - k.scrollTop();
                h.top = j > 0 ? h.top - j : Math.max(0, h.top)
            }
        },
        flip: {
            left: function (i, j) {
                if (j.at[0] === "center") {
                    return
                }
                var l = f(window),
                    k = i.left + j.elemWidth - l.width() - l.scrollLeft(),
                    h = j.my[0] === "left" ? -j.elemWidth : j.my[0] === "right" ? j.elemWidth : 0,
                    m = -2 * j.offset[0];
                i.left += i.left < 0 ? h + j.targetWidth + m : k > 0 ? h - j.targetWidth + m : 0
            },
            top: function (i, k) {
                if (k.at[1] === "center") {
                    return
                }
                var m = f(window),
                    l = i.top + k.elemHeight - m.height() - m.scrollTop(),
                    h = k.my[1] === "top" ? -k.elemHeight : k.my[1] === "bottom" ? k.elemHeight : 0,
                    j = k.at[1] === "top" ? k.targetHeight : -k.targetHeight,
                    n = -2 * k.offset[1];
                i.top += i.top < 0 ? h + k.targetHeight + n : l > 0 ? h + j + n : 0
            }
        }
    };
    if (!f.offset.setOffset) {
        f.offset.setOffset = function (l, i) {
            if (/static/.test(f.curCSS(l, "position"))) {
                l.style.position = "relative"
            }
            var k = f(l),
                n = k.offset(),
                h = parseInt(f.curCSS(l, "top", true), 10) || 0,
                m = parseInt(f.curCSS(l, "left", true), 10) || 0,
                j = {
                    top: (i.top - n.top) + h,
                    left: (i.left - n.left) + m
                };
            if ("using" in i) {
                i.using.call(l, j)
            } else {
                k.css(j)
            }
        };
        f.fn.offset = function (h) {
            var i = this[0];
            if (!i || !i.ownerDocument) {
                return null
            }
            if (h) {
                return this.each(function () {
                    f.offset.setOffset(this, h)
                })
            }
            return b.call(this)
        }
    }
}(jQuery));;
/*
 * jQuery UI Menu (not officially released)
 * 
 * This widget isn't yet finished and the API is subject to change. We plan to finish
 * it for the next release. You're welcome to give it a try anyway and give us feedback,
 * as long as you're okay with migrating your code later on. We can help with that, too.
 *
 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Menu
 *
 * Depends:
 *	jquery.ui.core.js
 *  jquery.ui.widget.js
 */
(function (a) {
    a.widget("ui.menu", {
        _create: function () {
            var b = this;
            this.element.addClass("ui-menu ui-widget ui-widget-content ui-corner-all").attr({
                role: "listbox",
                "aria-activedescendant": "ui-active-menuitem"
            }).click(function (c) {
                c.preventDefault();
                b.select()
            });
            this.refresh()
        },
        refresh: function () {
            var c = this;
            var b = this.element.children("li:not(.ui-menu-item):has(a)").addClass("ui-menu-item").attr("role", "menuitem");
            b.children("a").addClass("ui-corner-all").attr("tabindex", -1).mouseenter(function () {
                c.activate(a(this).parent())
            }).mouseleave(function () {
                c.deactivate()
            })
        },
        activate: function (d) {
            this.deactivate();
            if (this.hasScroll()) {
                var e = d.offset().top - this.element.offset().top,
                    b = this.element.attr("scrollTop"),
                    c = this.element.height();
                if (e < 0) {
                    this.element.attr("scrollTop", b + e)
                } else {
                    if (e > c) {
                        this.element.attr("scrollTop", b + e - c + d.height())
                    }
                }
            }
            this.active = d.eq(0).addClass("ui-state-hover").attr("id", "ui-active-menuitem").end();
            this._trigger("focus", null, {
                item: d
            })
        },
        deactivate: function () {
            if (!this.active) {
                return
            }
            this.active.removeClass("ui-state-hover").removeAttr("id");
            this._trigger("blur");
            this.active = null
        },
        next: function () {
            this.move("next", "li:first")
        },
        previous: function () {
            this.move("prev", "li:last")
        },
        first: function () {
            return this.active && !this.active.prev().length
        },
        last: function () {
            return this.active && !this.active.next().length
        },
        move: function (d, c) {
            if (!this.active) {
                this.activate(this.element.children(c));
                return
            }
            var b = this.active[d]();
            if (b.length) {
                this.activate(b)
            } else {
                this.activate(this.element.children(c))
            }
        },
        nextPage: function () {
            if (this.hasScroll()) {
                if (!this.active || this.last()) {
                    this.activate(this.element.children(":first"));
                    return
                }
                var d = this.active.offset().top,
                    c = this.element.height(),
                    b = this.element.children("li").filter(function () {
                        var e = a(this).offset().top - d - c + a(this).height();
                        return e < 10 && e > -10
                    });
                if (!b.length) {
                    b = this.element.children(":last")
                }
                this.activate(b)
            } else {
                this.activate(this.element.children(!this.active || this.last() ? ":first" : ":last"))
            }
        },
        previousPage: function () {
            if (this.hasScroll()) {
                if (!this.active || this.first()) {
                    this.activate(this.element.children(":last"));
                    return
                }
                var c = this.active.offset().top,
                    b = this.element.height();
                result = this.element.children("li").filter(function () {
                    var d = a(this).offset().top - c + b - a(this).height();
                    return d < 10 && d > -10
                });
                if (!result.length) {
                    result = this.element.children(":first")
                }
                this.activate(result)
            } else {
                this.activate(this.element.children(!this.active || this.first() ? ":last" : ":first"))
            }
        },
        hasScroll: function () {
            return this.element.height() < this.element.attr("scrollHeight")
        },
        select: function () {
            this._trigger("selected", null, {
                item: this.active
            })
        }
    })
}(jQuery));;

(function (a) {
    a.widget("ui.autocomplete", {
        options: {
            cid: "",
            minLength: 1,
            num_products: 5,
            num_terms: 10,
            delay: 300,
            termsHeader: '<div class="termsHeader">Popular Searches:</div>',
            productsHeader: '<div class="productsHeader">Your Search Matched:</div>',
            itemOrder: ["terms", "products"],
            footer: '<div style="text-align:center;border-top:1px solid #FDF0BF;"><a target="_blank" href="http://www.nextopia.com"><img border="0" class="nextopia" src="' + (("https:" == document.location.protocol) ? "https:" : "http:") + '//vector.nextopiasoftware.com/autocomplete-powered-by-nextopiav1.gif"></a></div>'
        },
        _create: function () {
            var b = this,
                c = this.element[0].ownerDocument;
            this.overContainer = false;
            this.lastSearch = "";
            this.element.addClass("ui-autocomplete-input").attr("autocomplete", "off").bind("keydown.autocomplete", function (d) {
                var e = a.ui.keyCode;
                switch (d.keyCode) {
                case e.UP:
                    b._move("previous", d);
                    d.preventDefault();
                    break;
                case e.DOWN:
                    b._move("next", d);
                    d.preventDefault();
                    break;
                case e.ENTER:
                    if (b.menu.active) {
                        d.preventDefault()
                    }
                case e.TAB:
                    if (!b.menu.active) {
                        return
                    }
                    b.menu.select();
                    break;
                case e.ESCAPE:
                    b.element.val(b.term);
                    b.close(d);
                    break;
                case e.SHIFT:
                case e.CONTROL:
                case 18:
                    break;
                default:
                    clearTimeout(b.searching);
                    b.searching = setTimeout(function () {
                        b.search(null, d)
                    }, b.options.delay);
                    break
                }
            }).bind("focus.autocomplete", function () {
                b.previous = b.element.val()
            }).bind("blur.autocomplete", function (d) {
                if (b.overContainer !== true) {
                    clearTimeout(b.searching);
                    b.close(d)
                }
            });
            this.source = function (e, d) {
                a.ajax({
                    url: (("https:" == document.location.protocol) ? "https://" : "http://") + "vector.nextopiasoftware.com/return_autocomplete_jsonp.php",
                    dataType: "jsonp",
                    data: {
                        q: e.term,
                        cid: b.options.cid,
                        num_products: b.options.num_products,
                        num_terms: b.options.num_terms
                    },
                    success: function (f) {
                        d(f)
                    }
                })
            };
            this.response = function () {
                return b._response.apply(b, arguments)
            };
            this.menu = a("<ul></ul>").addClass("ui-autocomplete").bind("mouseover.autocomplete", function (d) {
                b.overContainer = true
            }).bind("mouseout.autocomplete", function (d) {
                b.overContainer = false
            }).appendTo("body", c).menu({
                selected: function (e, f) {
                    if (f.item !== undefined && f.item !== null) {
                        var d = f.item.data("item.autocomplete");
                        if (false !== b._trigger("select", e, {
                            item: d
                        })) {
                            if (typeof d === "string") {
                                b._handleItemSelect(d)
                            } else {
                                b._handleProductSelect(d)
                            }
                        }
                        b.lastSearch = b.element.val();
                        b.close(e);
                        b.previous = b.element.val();
                        if (b.element[0] !== c.activeElement) {
                            b.element.focus()
                        }
                    }
                }
            }).unbind("click").bind("click", function (d) {
                b.menu.select()
            }).zIndex(this._getZIndex() + 1).css({
                top: 0,
                left: 0
            }).hide().data("menu");
            if (a.fn.bgiframe) {
                this.menu.element.bgiframe()
            }
        },
        destroy: function () {
            this.element.removeClass("ui-autocomplete-input ui-widget ui-widget-content").removeAttr("autocomplete").removeAttr("role").removeAttr("aria-autocomplete").removeAttr("aria-haspopup");
            this.menu.element.remove();
            a.Widget.prototype.destroy.call(this)
        },
        _setOption: function (b) {
            a.Widget.prototype._setOption.apply(this, arguments)
        },
        search: function (c, b) {
            c = c != null ? c : this.element.val();
            if (c.length < this.options.minLength) {
                return this.close(b)
            }
            clearTimeout(this.closing);
            if (c == this.lastSearch || this._trigger("search") === false) {
                return
            }
            this.lastSearch = c;
            return this._search(c)
        },
        _search: function (b) {
            this.term = this.element.addClass("ui-autocomplete-loading").val();
            this.source({
                term: b
            }, this.response)
        },
        _response: function (b) {
            if (b !== undefined && (b.terms !== undefined || b.products !== undefined)) {
                this._suggest(b);
                this._trigger("open")
            } else {
                this.close()
            }
            this.element.removeClass("ui-autocomplete-loading")
        },
        close: function (b) {
            clearTimeout(this.closing);
            if (this.menu.element.is(":visible")) {
                this._trigger("close", b);
                this.menu.element.hide();
                this.menu.deactivate()
            }
            if (this.previous !== this.element.val()) {
                this._trigger("change", b)
            }
        },
        _suggest: function (b) {
            var c = this.menu.element.empty().zIndex(this._getZIndex() + 1),
                d, e;
            this._renderMenu(c, b);
            this.menu.deactivate();
            this.menu.refresh();
            this.menu.element.show();
            d = c.width("").width();
            e = this.element.width();
            c.width(Math.max(d, e));
            this._positionMenu(c)
        },
        _positionMenu: function (b) {
            b.position({
                my: "left top",
                at: "left bottom",
                of: this.element,
                offset: "0 0",
                collision: "none"
            })
        },
        _renderMenu: function (e, c) {
            var b = this;
            for (var d in b.options.itemOrder) {
                var f = b.options.itemOrder[d];
                if (c[f] !== undefined) {
                    if (b.options[f + "Header"] != "") {
                        a(b.options[f + "Header"]).appendTo(e)
                    }
                    a.each(c[f], function (g, h) {
                        b._renderItem(e, h, f)
                    })
                }
            }
            if (b.options.footer != "") {
                a(b.options.footer).appendTo(e)
            }
        },
        _renderItem: function (b, e, c) {
            var d = "";
            switch (c) {
            case "terms":
                d = this._drawTerm(e);
                break;
            case "products":
                d = this._drawProduct(e);
                break
            }
            return a("<li></li>").data("item.autocomplete", e).addClass(c + "Item").append("<a>" + d + "</a>").appendTo(b)
        },
        _drawTerm: function (b) {
            return this._encapsulateMatches(b)
        },
        _drawProduct: function (b) {
            if (b.Image == '') {
                return '<img src="/skin/frontend/americanmeadows/default/images/ui/icon_ami-article.gif" alt="Article" />' + '<p class="search-name">' + this._encapsulateMatches(b.Name) + '</p>'
            }            
            if ("https:" == document.location.protocol && b.Image.substr(0, 6) != "https:") {
                b.Image = "https:" + b.Image.substr(5)
            }
            return '<img src="' + b.Image + '"/> ' + '<p class="search-name">' + this._encapsulateMatches(b.Name) + '</p>'
        },
        _handleItemSelect: function (b) {
            this.element.val(b);
            this.element.context.form.submit()
        },
        _handleProductSelect: function (b) {
            if (b.Url != "") {
		urls = b.Url.split(',');
                window.location = urls[0];
            } else {
                this.element.val(b.Name)
            }
        },
        _encapsulateMatches: function (e) {
            var c = this.term.replace(/^\s*/, "").replace(/\s*$/, "").split(" ");
            var b = c.length;
            for (var d = 0; d < b; d++) {
                e = e.replace(new RegExp("(^|\\b)(" + c[d].replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")", "gi"), String.fromCharCode(9677) + "$1$2" + String.fromCharCode(9678))
            }
            e = e.replace(new RegExp(String.fromCharCode(9677), "g"), "<b>").replace(new RegExp(String.fromCharCode(9678), "g"), "</b>");
            return e
        },
        _move: function (c, b) {
            if (!this.menu.element.is(":visible")) {
                this.search(null, b);
                return
            }
            this.menu[c]();
            if (this.menu.active.data("item.autocomplete") == null) {
                this.menu[c]()
            }
        },
        _getZIndex: function () {
            var b = parseInt(this.element.context.style.zIndex);
            if (isNaN(b)) {
                b = 0
            }
            return b
        },
        widget: function () {
            return this.menu.element
        }
    })
}(jQuery));

