/*
 * Auto Complete 5.0
 * November 22, 2009
 * Corey Hart @ http://www.codenothing.com
 */
(function(g, h) {
    g.fn.autoComplete=function() {
        var j=c.call(arguments),i=this,l=j.shift(),k=(typeof l === "string");
        if (k) {
            l=l.replace(".", "-")
        }
        j=l === "button-supply" || l === "direct-supply" ? g.isArray(j[0]) && g.isArray(j[0][0]) ? j[0] : j : j[1] === h && g.isArray(j[0]) ? j[0] : j;
        return k ? g(i)[l === "option" && j.length < 2 ? "triggerHandler" : "trigger"]("autoComplete." + l, j) : l && l[g.expando] ? g(i).trigger(l, j) : a.call(i, l)
    };
    g.fn.bgiframe=g.fn.bgiframe ? g.fn.bgiframe : g.fn.bgIframe ? g.fn.bgIframe : function() {
        return this
    };
    g.expando=g.expando !== h ? g.expando : (function() {
        var k=g.Event("keyup"),j;
        for (j in k) {
            if (j.indexOf("jQuery") === 0) {
                return j
            }
        }
        return"jQuery" + k.timeStamp
    })();
    function b() {
        return(new Date).getTime()
    }

    var d=true,e=false,c=Array.prototype.slice,f=g.autoComplete={counter:0,length:0,stack:{},order:[],hasFocus:e,getFocus:function() {
        return this.order[0] ? this.stack[this.order[0]] : h
    },getPrevious:function() {
        for (var k=1,j=this.order.length; k < j; k++) {
            if (this.order[k]) {
                return this.stack[this.order[k]]
            }
        }
        return h
    },remove:function(n) {
        for (var m=0,j=this.order.length; m < j; m++) {
            if (this.order[m] === n) {
                this.order[m]=h
            }
        }
        this.stack[n]=h;
        this.length--;
        delete this.stack[n]
    },getAll:function() {
        for (var m=0,k=this.counter,j=[]; m < k; m++) {
            if (this.stack[m]) {
                j.push(this.stack[m])
            }
        }
        return g(j)
    },defaults:{backwardsCompatible:e,ajax:"ajax.php",ajaxCache:g.ajaxSettings.cache,dataSupply:[],dataFn:h,dataName:"ac-data",list:"auto-complete-list",rollover:"auto-complete-list-rollover",width:h,striped:h,maxHeight:h,newList:e,postVar:"value",postData:{},minChars:1,maxItems:-1,maxRequests:0,requestType:"POST",inputControl:h,autoFill:e,nonInput:h,multiple:e,multipleSeparator:" ",onBlur:h,onFocus:h,onHide:h,onLoad:h,onMaxRequest:function() {
    },onRollover:h,onSelect:h,onShow:h,onSubmit:function() {
        return d
    },spinner:h,preventEnterSubmit:d,delay:0,useCache:d,cacheLimit:50}},a=function(i) {
        return this.each(function() {
            var C=this,j=g(C).attr("autocomplete", "off"),G=d,p={},y="",z={length:0},v,x,N,k,F,I=e,H,r,o=-1,n,m,B=(function() {
                f.length++;
                return ++f.counter
            })(),l=0,A={length:0,val:h,list:{}},K=g.extend({width:j.outerWidth()}, f.defaults, i || {}, g.metadata ? j.metadata() : {}),u=!K.newList && g("ul." + K.list)[0] ? g("ul." + K.list).eq(0).bgiframe().data("autoComplete", d) : g("<ul/>").appendTo("body").addClass(K.list).bgiframe().hide().data("ac-selfmade", d).data("autoComplete", d),q=g(document).bind("click.autoComplete-" + B, function(P) {
                var O;
                if (G && I && (!p || P.timeStamp - p.timeStamp > 200) && (O=g(P.target)).closest("ul").data("ac-input-index") !== B && O.data("ac-input-index") !== B) {
                    u.hide(P);
                    j.blur()
                }
                p=P
            });
            s();
            K.requestType=K.requestType.toUpperCase();
            m=K.multiple ? K.multipleSeparator : h;
            f.stack[B]=C;
            j.data("autoComplete", d).data("ac-input-index", B).data("ac-active", G).data("ac-initial-settings", g.extend(d, {}, K)).data("ac-settings", K).bind(window.opera ? "keypress.autoComplete" : "keydown.autoComplete", function(P) {
                if (!G) {
                    return d
                }
                var O=(p=P).keyCode,Q=e;
                if (O == 9 && I) {
                    E(P)
                } else {
                    if (O == 13 && v) {
                        Q=K.preventEnterSubmit && I ? e : d;
                        E(P)
                    } else {
                        if (O == 38) {
                            if (o > 0) {
                                o--;
                                t(P)
                            } else {
                                o=-1;
                                j.val(y);
                                u.hide(P)
                            }
                        } else {
                            if (O == 40) {
                                if (o < z.length - 1) {
                                    o++;
                                    w(P)
                                }
                            } else {
                                if (O == 33) {
                                    if (o > 0) {
                                        o-=F;
                                        if (o < 0) {
                                            o=0
                                        }
                                        t(P)
                                    }
                                } else {
                                    if (O == 34) {
                                        if (o < z.length - 1) {
                                            o+=F;
                                            if (o > z.length - 1) {
                                                o=z.length - 1
                                            }
                                            w(P)
                                        }
                                    } else {
                                        if (K.nonInput && g.inArray(O, K.nonInput)) {
                                            u.html("").hide(P)
                                        } else {
                                            return d
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                p[g.expando + "_autoComplete_keydown"]=d;
                return Q
            }).bind("keyup.autoComplete", function(P) {
                if (!G || p[g.expando + "_autoComplete_keydown"]) {
                    return d
                }
                y=j.val();
                var O=(p=P).keyCode,Q=m ? y.split(m).pop() : y;
                if (O != 13) {
                    A.val=K.inputControl === h ? Q : K.inputControl.apply(C, K.backwardsCompatible ? [Q,O,u,P] : [P,{val:Q,key:O,ul:u}]);
                    if (A.val.length >= K.minChars) {
                        L(P, K, A, (O == 8 || O == 32))
                    } else {
                        if (O == 8) {
                            u.html("").hide(P)
                        }
                    }
                }
            }).bind("blur.autoComplete", function(O) {
                if (!G || I) {
                    return d
                }
                p=O;
                j.data("ac-hasFocus", e);
                o=-1;
                if (f.order[0] !== h) {
                    f.order.unshift(h)
                }
                f.hasFocus=e;
                u.hide(O);
                if (K.onBlur) {
                    K.onBlur.apply(C, K.backwardsCompatible ? [y,u,O] : [O,{val:y,ul:u}])
                }
            }).bind("focus.autoComplete", function(P, O) {
                if (!G || (f.focus === B && O === g.expando + "_autoComplete") || p[g.expando + "_autoComplete_enter"]) {
                    return d
                }
                p=P;
                if (B != u.data("ac-input-index")) {
                    u.html("").hide(P)
                }
                j.data("ac-hasFocus", d);
                if (f.order[0] === h) {
                    if (f.order[1] === B) {
                        f.order.shift()
                    } else {
                        f.order[0]=B
                    }
                } else {
                    if (f.order[0] != B && f.order[1] != B) {
                        f.order.unshift(B)
                    }
                }
                if (f.order.length > f.defaults.cacheLimit) {
                    f.order.pop()
                }
                f.hasFocus=d;
                if (K.onFocus) {
                    K.onFocus.apply(C, K.backwardsCompatible ? [u,P] : [P,{ul:u}])
                }
            }).bind("autoComplete.settings", function(Q, O) {
                if (!G) {
                    return d
                }
                if (g.isFunction(O)) {
                    var P=O.apply(C, K.backwardsCompatible ? [K,A,u,Q] : [Q,{settings:K,cache:A,ul:u}]);
                    if (g.isArray(P) && P[0] !== h) {
                        K=g.extend(d, {}, K, P[0] || K);
                        A=g.extend(d, {}, A, P[1] || A)
                    }
                } else {
                    K=g.extend(d, {}, K, O || {})
                }
                K.requestType=K.requestType.toUpperCase();
                m=K.multiple ? K.multipleSeparator : h;
                j.data("ac-settings", K);
                u=!K.newList && u.hasClass(K.list) ? u : !K.newList && g("ul." + K.list)[0] ? g("ul." + K.list).bgiframe().data("autoComplete", d) : g("<ul/>").appendTo("body").addClass(K.list).bgiframe().hide().data("ac-selfmade", d).data("autoComplete", d);
                s();
                return p=Q
            }).bind("autoComplete.flush", function(O, P) {
                if (!G) {
                    return d
                }
                A={length:0,val:h,list:{}};
                if (!P) {
                    l=0
                }
                return p=O
            }).bind("autoComplete.button-ajax", function(P, O, Q) {
                if (!G) {
                    return d
                }
                p=P;
                j.trigger("focus", [g.expando + "_autoComplete"]);
                if (typeof O === "string") {
                    Q=O;
                    O={}
                }
                A.val=Q || "NON_404_<>!@$^&";
                return L(P, g.extend(d, {}, K, {maxItems:-1,postData:O || {}}), A)
            }).bind("autoComplete.button-supply", function(O, P, Q) {
                if (!G) {
                    return d
                }
                p=O;
                j.trigger("focus", [g.expando + "_autoComplete"]);
                if (typeof P === "string") {
                    Q=P;
                    P=h
                }
                A.val=Q || "NON_404_SUPPLY_<>!@$^&";
                P=g.isArray(P) && P.length ? P : K.dataSupply;
                return L(O, g.extend(d, {}, K, {maxItems:-1,dataSupply:P,dataFn:function() {
                    return d
                }}), A)
            }).bind("autoComplete.direct-supply", function(O, P, Q) {
                if (!G) {
                    return d
                }
                p=O;
                j.trigger("focus", [g.expando + "_autoComplete"]);
                if (typeof P === "string") {
                    Q=P;
                    P=h
                }
                A.val=Q || "NON_404_SUPPLY_<>!@$^&";
                P=g.isArray(P) && P.length ? P : K.dataSupply;
                return M(O, P, g.extend(d, {}, K, {maxItems:-1,dataSupply:P,dataFn:function() {
                    return d
                }}), A)
            }).bind("autoComplete.search", function(O, P) {
                if (!G) {
                    return d
                }
                A.val=P || "";
                return L(p=O, K, A)
            }).bind("autoComplete.option", function(Q) {
                if (!G) {
                    return d
                }
                p=Q;
                var O=c.call(arguments),P=O.length;
                return P == 3 ? (function() {
                    K[O[1]]=O[2];
                    j.data("ac-settings", K);
                    return O[2]
                })() : P == 2 ? (function() {
                    switch (O[1]) {case"ul":return u;case"cache":return A;case"xhr":return r;case"input":return j;default:return K[O[1]] || h
                    }
                })() : K
            }).bind("autoComplete.enable", function(O) {
                j.data("ac-active", G=d);
                return p=O
            }).bind("autoComplete.disable", function(O) {
                j.data("ac-active", G=e);
                u.html("").hide(O);
                return p=O
            }).bind("autoComplete.destroy", function(P) {
                j.removeData("autoComplete").removeData("ac-input-index").removeData("ac-initial-settings").removeData("ac-settings").removeData("ac-active").unbind(".autoComplete").unbind("autoComplete." + ["settings","flush","button-ajax","button-supply","direct-supply","search","option","enable","disable","destroy"].join(" autoComplete.")).parents("form").eq(0).unbind("submit.autoComplete-" + B);
                q.unbind("click.autoComplete-" + B);
                f.remove(B);
                G=e;
                var Q=u.html("").hide(P).data("ac-inputs"),O;
                Q[B]=h;
                for (O in Q) {
                    if (Q[O] === d) {
                        return p=P
                    }
                }
                if (u.data("ac-selfmade") === d) {
                    u.remove()
                }
                return p=P
            }).parents("form").eq(0).bind("submit.autoComplete-" + B, function(P) {
                if (!G) {
                    return d
                }
                var O=p[g.expando + "_autoComplete_enter"] || e;
                p=P;
                return K.preventEnterSubmit ? (I || O) ? e : K.onSubmit.call(C, P, {form:this,ul:u}) : K.onSubmit.call(C, P, {form:this,ul:u})
            });
            function L(Q, P, O, S, R) {
                if (P.spinner) {
                    P.spinner.call(C, Q, {active:d,ul:u})
                }
                if (H) {
                    H=clearTimeout(H)
                }
                if (P.delay > 0 && R === h) {
                    return H=setTimeout(function() {
                        L(Q, P, O, S, d);
                        H=clearTimeout(H)
                    }, P.delay)
                }
                if (r) {
                    r.abort()
                }
                if (P.useCache && O.list[O.val]) {
                    return M(Q, O.list[O.val], P, O, S)
                }
                if (P.dataSupply.length) {
                    return J(Q, P, O, S)
                }
                if (P.maxRequests && ++l >= P.maxRequests) {
                    u.html("").hide(Q);
                    if (P.spinner) {
                        P.spinner.call(C, Q, {active:e,ul:u})
                    }
                    return l > P.maxRequests ? e : P.onMaxRequest.apply(C, P.backwardsCompatible ? [O.val,u,Q,y] : [Q,{search:O.val,val:y,ul:u}])
                }
                P.postData[P.postVar]=O.val;
                return r=g.ajax({type:P.requestType,url:P.ajax,data:P.postData,dataType:"json",cache:P.ajaxCache,success:function(T) {
                    M(Q, T, P, O, S)
                },error:function() {
                    u.html("").hide(Q);
                    if (P.spinner) {
                        P.spinner.call(C, Q, {active:e,ul:u})
                    }
                }})
            }

            function J(P, S, O, R) {
                var V=[],W=[],X=g.isFunction(S.dataFn),Y=X ? h : new RegExp("^" + O.val, "i"),T=0,Z,U=0,Q=S.dataSupply.length;
                for (; U < Q; U++) {
                    Z=S.dataSupply[U];
                    Z=typeof Z === "object" && Z.value ? Z : {value:Z};
                    W=S.backwardsCompatible ? [O.val,Z.value,V,U,S.dataSupply,u,P] : [P,{val:O.val,entry:Z.value,list:V,i:U,supply:S.dataSupply,ul:u}];
                    if ((X && S.dataFn.apply(C, W)) || (!X && Z.value.match(Y))) {
                        if (S.maxItems > -1 && ++T > S.maxItems) {
                            break
                        }
                        V.push(Z)
                    }
                }
                return M(P, V, S, O, R)
            }

            function E(O) {
                if (I) {
                    if (K.onSelect) {
                        K.onSelect.apply(C, K.backwardsCompatible ? [n,v,u,O] : [O,{data:n,li:v,ul:u}])
                    }
                    D(h);
                    y=j.val();
                    if (p.type == "keydown") {
                        p[g.expando + "_autoComplete_enter"]=d
                    }
                }
                u.hide(O);
                return v
            }

            function t(P) {
                if (v) {
                    v.removeClass(K.rollover)
                }
                u.show(P);
                v=z.eq(o).addClass(K.rollover);
                n=v.data(K.dataName);
                if (!v.length || !n) {
                    return e
                }
                D(n.value || "");
                if (K.onRollover) {
                    K.onRollover.apply(C, K.backwardsCompatible ? [n,v,u,P] : [P,{data:n,li:v,ul:u}])
                }
                var O=o * k;
                if (O < x - N) {
                    x=O + N;
                    u.scrollTop(O)
                }
                return v
            }

            function w(P) {
                if (v) {
                    v.removeClass(K.rollover)
                }
                u.show(P);
                v=z.eq(o).addClass(K.rollover);
                n=v.data(K.dataName);
                if (!v.length || !n) {
                    return e
                }
                D(n.value || "");
                var O=(o + 1) * k;
                if (O > x) {
                    u.scrollTop((x=O) - N)
                }
                if (K.onRollover) {
                    K.onRollover.apply(C, K.backwardsCompatible ? [n,v,u,P] : [P,{data:n,li:v,ul:u}])
                }
                return v
            }

            function s() {
                if (!u[g.expando + "_autoComplete"]) {
                    var P=u.hide,O=u.show;
                    u.hide=function(R, S, T) {
                        if (K.onHide && I) {
                            K.onHide.call(C, R, {ul:u});
                            p[g.expando + "_autoComplete_hide"]=d
                        }
                        I=e;
                        return P.call(u, S, T)
                    };
                    u.show=function(R, S, T) {
                        if (K.onShow && !I) {
                            K.onShow.call(C, R, {ul:u})
                        }
                        I=d;
                        return O.call(u, S, T)
                    };
                    u[g.expando + "_autoComplete"]=d
                }
                var Q=u.data("ac-inputs") || {};
                Q[B]=d;
                return u.data("ac-inputs", Q)
            }

            function D(Q) {
                if (Q === h) {
                    var R,O;
                    R=O=j.val().length
                } else {
                    if (m) {
                        Q=y.substr(0, y.length - y.split(m).pop().length) + Q + m
                    }
                    var R=y.length,O=Q.length;
                    j.val(Q)
                }
                if (!K.autoFill || R > O) {
                    return e
                } else {
                    if (C.createTextRange) {
                        var P=C.createTextRange();
                        if (Q === h) {
                            P.move("character", R);
                            P.select()
                        } else {
                            P.collapse(d);
                            P.moveStart("character", R);
                            P.moveEnd("character", O);
                            P.select()
                        }
                    } else {
                        if (C.setSelectionRange) {
                            C.setSelectionRange(R, O)
                        } else {
                            if (C.selectionStart) {
                                C.selectionStart=R;
                                C.selectionEnd=O
                            }
                        }
                    }
                }
                return d
            }

            function M(P, Z, U, O, T) {
                if (U.onLoad) {
                    Z=U.onLoad.call(C, P, {list:Z,settings:U,cache:O,ul:u})
                }
                if (U.spinner) {
                    U.spinner.call(C, P, {active:e,ul:u})
                }
                if (U.useCache && O.list[O.val] === h) {
                    O.length++;
                    O.list[O.val]=Z;
                    if (O.length > U.cacheLimit) {
                        O.list={};
                        O.length=0
                    }
                }
                if (!Z || Z.length < 1) {
                    return u.html("").hide(P)
                }
                o=-1;
                var X=j.offset(),Q=[],R=0,V=0,Y=0,W=e,S=Z.length;
                for (; Y < S; Y++) {
                    if (Z[Y].value) {
                        if (U.maxItems > -1 && ++R > U.maxItems) {
                            break
                        }
                        Q.push(U.striped && W ? '<li class="' + U.striped + '">' : "<li>", Z[Y].display || Z[Y].value, "</li>");
                        W=!W
                    }
                }
                z=u.html(Q.join("")).children("li");
                for (S=z.length; V < S; V++) {
                    g.data(z[V], U.dataName, Z[V]);
                    g.data(z[V], "ac-index", V)
                }
                if (U.autoFill && !T) {
                    o=0;
                    n=Z[0];
                    D(n.value || "");
                    v=z.eq(0).addClass(U.rollover)
                }
                u.unbind(".autoComplete").data("ac-input-index", B).bind("mouseout.autoComplete", function() {
                    v.removeClass(U.rollover)
                }).bind("mouseover.autoComplete", function(aa) {
                    v=g(aa.target).closest("li");
                    if (v.length < 1) {
                        return e
                    }
                    z.filter("." + U.rollover).removeClass(U.rollover);
                    o=v.addClass(U.rollover).data("ac-index");
                    n=v.data(U.dataName);
                    if (U.onRollover) {
                        U.onRollover.apply(C, U.backwardsCompatible ? [n,v,u,aa] : [aa,{data:n,li:v,ul:u}])
                    }
                }).bind("click.autoComplete", function(aa) {
                    j.trigger("focus", [g.expando + "_autoComplete"]);
                    n=v.data(U.dataName);
                    j.val(y=m ? y.substr(0, y.length - y.split(m).pop().length) + n.value + m : n.value);
                    u.hide(aa);
                    D(h);
                    if (U.onSelect) {
                        U.onSelect.apply(C, U.backwardsCompatible ? [n,v,u,aa] : [aa,{data:n,li:v,ul:u}])
                    }
                }).css({top:X.top + j.outerHeight(),left:X.left,width:U.width}).scrollTop(0);
                if (U.maxHeight) {
                    u.css({height:k * z.length > U.maxHeight ? U.maxHeight : "auto",overflow:"auto"})
                }
                N=u.show(P).outerHeight();
                x=N;
                k=z.eq(0).outerHeight();
                F=Math.floor(x / k);
                p.timeStamp=b();
                return u
            }
        })
    }
})(jQuery);