/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jSuggest
 * Version: 1.0 (May 26, 2008)
 * Requires: jQuery 1.2.6+
 */
(function(A) {
    A.fn.jSuggest = function(D) {
        var F = A.extend({}, A.fn.jSuggest.defaults, D);
        var G = ".jSuggestHover";
        var H = "jSuggestHover";
        var D = this.value;
        var E = this.value;
        var I = this;
        var C = this.value;
        var B = "#jSuggestContainer";
        A("body").append('<div id="jSuggestContainer"></div>');
        A(B).hide();
        A(this).bind("keyup click", function(K) {
            I = this;
            C = this.value;
            if (this.value.length >= F.minchar && A.trim(this.value) != "Search Terms") {
                var J = A(this).offset();
                A(B).css({position:"absolute",top:J.top + A(this).outerHeight() + "px",left:J.left,width:A(this).outerWidth() - 2 + "px",opacity:F.opacity,zIndex:F.zindex}).show();
                if (K.keyCode == 27) {
                    A(B).hide()
                } else {
                    if (K.keyCode == 13) {
                        if (A(G).length == 1) {
                            A(I).val(A(G).text())
                        }
                        A(B).hide();
                        E = I.value
                    } else {
                        if (K.keyCode == 40) {
                            if (A(G).length == 1) {
                                if (!A(G).next().length == 0) {
                                    A(G).next().addClass(H);
                                    A(".jSuggestHover:eq(0)").removeClass(H);
                                    if (F.autoChange) {
                                        A(I).val(A(G).text())
                                    }
                                }
                            } else {
                                A("#jSuggestContainer ul li:first-child").addClass(H);
                                if (F.autoChange) {
                                    A(I).val(A(G).text())
                                }
                            }
                        } else {
                            if (K.keyCode == 38) {
                                if (A(G).length == 1) {
                                    if (!A(G).prev().length == 0) {
                                        A(G).prev().addClass(H);
                                        A(".jSuggestHover:eq(1)").removeClass(H);
                                        if (F.autoChange) {
                                            A(I).val(A(G).text())
                                        }
                                    } else {
                                        A(G).removeClass(H);
                                        A(I).val(E)
                                    }
                                }
                            } else {
                                if (I.value != E) {
                                    E = I.value;
                                    if (A(".jSuggestLoading").length == 0) {
                                        A('<div class="jSuggestLoading"><img src="' + F.loadingImg + '" align="bottom" /> ' + F.loadingText + "</div>").prependTo("#jSuggestContainer")
                                    }
                                    A(".jSuggestLoading").show();
                                    A(B).find("ul").remove();
                                    if (F.data == "") {
                                        F.data = A(this).serialize()
                                    } else {
                                        D = F.data + "=" + A(this).val()
                                    }
                                    setTimeout(function() {
                                        A.ajax({type:F.type,url:F.url,data:D,success:function(L) {
                                            A(B).find("ul").remove();
                                            A(B).append(L);
                                            A("#jSuggestContainer ul li").bind("mouseover", function() {
                                                A(G).removeClass(H);
                                                A(this).addClass(H);
                                                C = A(this).text();
                                                if (F.autoChange) {
                                                    A(I).val(A(G).text())
                                                }
                                            });
                                            A("#jSuggestContainer ul li").click(function() {
                                                A(this).addClass(H);
                                                A(I).val(C)
                                            });
                                            A(".jSuggestLoading").hide()
                                        }})
                                    }, F.delay)
                                }
                            }
                        }
                    }
                }
            } else {
                A(G).removeClass(H);
                A(B).hide()
            }
            return false
        });
        A(document).bind("click", function() {
            A(B).hide();
            E = I.value
        })
    };
    A.fn.jSuggest.defaults = {minchar:3,opacity:1,zindex:20000,delay:2500,loadingImg:"http://pic.shopin.net/images/ajax-loader.gif",loadingText:"Loading...",autoChange:false,url:"",type:"GET",data:""}
})(jQuery);