SCC.AutoSuggest = function(a, b) {
    this.init(a, b)
};
SCC.AutoSuggest.prototype = {
    init: function(a, b) {
        b = b || {};
        this.$container = $(a).addClass("autosuggest");
        this.size = b.size || 30;
        this.inputText = (b.inputText != undefined) ? b.inputText: "Enter Company Name or Symbol";
        this.buttonText = (b.buttonText != undefined && b.buttonText.length > 0) ? b.buttonText: null;
        this.searchType = (b.searchType != undefined) ? b.searchType: "companies";
        this.maxResults = (b.maxResults != undefined && b.maxResults > 0) ? b.maxResults: 10;
        this.pauseTime = (b.pauseTime != undefined) ? b.pauseTime: 250;
        this.onSelect = b.onSelect ||
        function(d) {
            window.location = "/h-sc/ui?s=" + d;
            return
        };
        this.onMore = b.onMore ||
        function(d) {
            window.location = "/symsearch?" + d;
            return
        };
        this.onInit = b.onInit ||
        function() {
            return
        };
        this.onInit();
        this.timerId = 0;
        this.isReturnKeyDown = false;
        this.isMultiSymols = false;
        this.isExpandDown = true;
        this.isExpandRight = true;
        var c = this;
        this.$searchbox = $("<input>").attr({
            type: "text",
            autocomplete: "off",
            size: this.size
        }).focus(function() {
            if ($(this).val() == c.inputText) {
                $(this).val("")
            }
        }).blur(function() {
            if ($(this).val() == "") {
                $(this).val(c.inputText)
            }
            setTimeout(function() {
                c.destroySuggestions()
            },
            500)
        }).keydown(function(d) {
            if (d.keyCode === SCC.KEY.RETURN && !c.isReturnKeyDown) {
                c.isReturnKeyDown = true;
                c.submit(d)
            }
        }).keyup(function(d) {
            var i = $(this).val().toLowerCase().trim();
            var j = i.charAt(i.length - 1);
            var e = (d.keyCode == SCC.KEY.DOWN);
            var h = (d.keyCode == SCC.KEY.UP);
            var g = (d.keyCode == SCC.KEY.RETURN);
            var k = (d.keyCode == SCC.KEY.COMMA) || (j == ",");
            var l = (d.keyCode == SCC.KEY.SPACE) || (j == " ");
            var f = (j == ":");
            if (e) {
                c.next()
            } else {
                if (h) {
                    c.prev()
                } else {
                    if (g) {
                        c.isReturnKeyDown = false
                    } else {
                        if (k || l || f) {
                            c.lastTyped = $(this).val()
                        } else {
                            if (i.length > 0) {
                                c.lastTyped = $(this).val();
                                if (c.pauseTime > 0) {
                                    clearTimeout(c.timerId);
                                    c.timerId = setTimeout(function() {
                                        c.refreshSuggestions()
                                    },
                                    c.pauseTime)
                                } else {
                                    c.refreshSuggestions()
                                }
                            } else {
                                c.lastTyped = "";
                                c.destroySuggestions()
                            }
                        }
                    }
                }
            }
        }).addClass("searchbox").val(this.inputText).appendTo(this.$container);
        if (this.buttonText) {
            this.$searchbutton = $("<input>").attr({
                type: "submit",
                value: this.buttonText
            }).click(function(d) {
                c.submit(d)
            }).addClass("searchbtn").appendTo(this.$container)
        }
        this.$suggestionbox = null;
        $(window).resize(function() {
            c.positionSuggestions()
        });
        this.cache = {};
        this.lastTyped = ""
    },
    refreshSuggestions: function() {
        var e = this.$searchbox.val().toLowerCase().trim();
        var a = e.lastIndexOf(",");
        var g = e.lastIndexOf(":");
        var f = a != -1 && a > g;
        var b = g != -1 && g > a;
        if (f) {
            e = e.substring(a + 1).trim()
        } else {
            if (b) {
                e = e.substring(g + 1).trim()
            }
        }
        if (this.cache[e]) {
            var c = this.cache[e];
            this.showSuggestions(e, c)
        } else {
            var d = this;
            $.getJSON(SCC.SERVER.COMPANIES + "?callback=?", {
                suggest: e,
                limit: this.maxResults
            },
            function(h) {
                if (typeof h === "object" && h.companies && h.companies.length > 0) {
                    d.cache[e] = h;
                    d.showSuggestions(e, h)
                } else {
                    d.destroySuggestions()
                }
            })
        }
    },
    showSuggestions: function(j, k) {
        var l = "<table class='suggestions' cellspacing='0' cellpadding='2'>";
        if (k.companies && k.companies.length > 0) {
            if (k.companies.length == this.maxResults && !this.isExpandDown) {
                l += "<tr class='suggestion'><td class='more' colspan='3'>More Results For \"" + j.toUpperCase() + '" ...</td></tr>'
            }
            for (var e = 0; e < k.companies.length; e++) {
                var g = (this.isExpandDown) ? e: k.companies.length - 1 - e;
                var d = k.companies[g].symbol;
                var b = k.companies[g].name;
                var h = k.companies[g].exchange;
                l += "<tr class='suggestion'>";
                l += "<td class='symbol'>" + this.hilite(d, j) + "</td>";
                l += "<td class='company'>" + this.hilite(b, j) + "</td>";
                l += "<td class='exchange' align='right'>" + h + "</td>";
                l += "</tr>"
            }
            if (k.companies.length == this.maxResults && this.isExpandDown) {
                l += "<tr class='suggestion'><td class='more' colspan='3'>More Results For \"" + j.toUpperCase() + '" ...</td></tr>'
            }
        } else {
            this.destroySuggestions();
            return
        }
        l += "</table>";
        if (this.$suggestionbox == null) {
            this.initSuggestions(l)
        } else {
            this.$suggestionbox.html(l);
            this.$suggestionbox.css("width", "")
        }
        var f = this;
        $(".suggestion", this.$suggestionbox).hover(function() {
            $(this).addClass("hovered")
        },
        function() {
            $(this).removeClass("hovered")
        }).click(function() {
            $(this).nextAll().removeClass("selected hovered");
            $(this).addClass("selected");
            f.fillSearchBox();
            f.submit()
        });
        $(".suggestion:even", this.$suggestionbox).addClass("even");
        $(".suggestion:odd", this.$suggestionbox).addClass("odd");
        var c = this.$suggestionbox.width();
        var a = this.$searchbox.outerWidth();
        if (c < a) {
            this.$suggestionbox.width(a);
            $("table", this.$suggestionbox).width(a)
        }
        this.hideApplet()
    },
    initSuggestions: function(a) {
        this.$suggestionbox = $("<div>").addClass("autosuggest").html(a).css({
            position: "absolute",
            zIndex: SCC.util.getTopZIndex(document.body) + 1
        }).appendTo(document.body);
        this.positionSuggestions()
    },
    positionSuggestions: function() {
        if (this.$suggestionbox == null) {
            return
        }
        var b = this.$searchbox.offset().left;
        var h = this.$searchbox.offset().top;
        var f = this.$searchbox.outerHeight();
        var c = this.$searchbox.outerWidth();
        var d = $(window).height();
        var a = $(window).width();
        var e = this.$suggestionbox.outerHeight();
        var g = this.$suggestionbox.outerWidth();
        this.isExpandDown = (d - f - h) > e || h < e;
        this.isExpandRight = (a - c - b) > g || b < g;
        if (this.isExpandDown) {
            this.$suggestionbox.css({
                top: h + f,
                bottom: ""
            })
        } else {
            this.$suggestionbox.css({
                top: "",
                bottom: d - h
            })
        }
        if (this.isExpandRight) {
            this.$suggestionbox.css({
                left: b,
                right: ""
            })
        } else {
            this.$suggestionbox.css({
                right: a - c - b,
                left: ""
            })
        }
    },
    destroySuggestions: function() {
        if (this.$suggestionbox != null) {
            this.$suggestionbox.remove();
            this.$suggestionbox = null;
            this.showApplet()
        }
    },
    submit: function(b) {
        if (b != null) {
            b.preventDefault()
        }
        var d = this.$searchbox.val().toLowerCase().trim();
        if (d.length == 0) {
            return
        }
        var a = $(".suggestion.selected", this.$suggestionbox);
        var c = d && a.children().hasClass(".more");
        this.destroySuggestions();
        if (c) {
            this.onMore(d)
        } else {
            this.onSelect(d)
        }
    },
    hilite: function(e, c) {
        if (c.indexOf(" ") != -1) {
            var a = e.toLowerCase().indexOf(c.toLowerCase());
            if (a != -1) {
                e = e.substring(0, a) + "<b>" + e.substring(a, a + c.length) + "</b>" + e.substring(a + c.length)
            }
            return e
        } else {
            var d = e.split(/\s+/);
            for (var b = 0; b < d.length; b++) {
                if (d[b].startsWithIgnoreCase(c)) {
                    d[b] = "<b>" + d[b].substring(0, c.length) + "</b>" + d[b].substring(c.length)
                }
            }
            return d.join(" ")
        }
    },
    isOverlap: function(l, k) {
        var b = l.offset().left;
        var i = l.offset().top;
        var e = l.outerWidth();
        var g = l.outerHeight();
        var a = k.offset().left;
        var h = k.offset().top;
        var d = k.outerWidth();
        var f = k.outerHeight();
        var c = b + e > a && b < a + d;
        var j = i + g > h && i < h + f;
        return c && j
    },
    logSize: function(d) {
        var b = d.offset().left;
        var f = d.offset().top;
        var c = d.outerWidth();
        var e = d.outerHeight();
        var a = d.is(":visible");
        if (console) {
            console.log("size: " + b + "," + f + " " + c + "x" + e + " " + (a ? "visible": "invisible"))
        }
    },
    hideApplet: function() {
        var a = $("object, applet, #testdiv");
        if (a.size() == 0) {
            return
        }
        var b = this;
        a.each(function() {
            var e = $(this);
            var d = b.$suggestionbox;
            var c = b.isOverlap(d, e);
            if (c) {
                e.css({
                    position: "relative",
                    left: -999
                })
            }
        })
    },
    showApplet: function() {
        var a = $("object, applet, #testdiv");
        if (a.size() == 0) {
            return
        }
        a.each(function() {
            var b = $(this);
            if (b.position().left == -999) {
                b.css({
                    position: "",
                    left: 0
                })
            }
        })
    },
    next: function() {
        var a = $(".suggestion.selected", this.$suggestionbox);
        if (a.size() > 0) {
            a.removeClass("selected");
            a.next().addClass("selected")
        } else {
            $(".suggestion:first", this.$suggestionbox).addClass("selected")
        }
        this.fillSearchBox()
    },
    prev: function() {
        var a = $(".suggestion.selected", this.$suggestionbox);
        if (a.size() > 0) {
            a.removeClass("selected");
            a.prev().addClass("selected")
        } else {
            $(".suggestion:last", this.$suggestionbox).addClass("selected")
        }
        this.fillSearchBox()
    },
    fillSearchBox: function() {
        var d = $(".suggestion.selected", this.$suggestionbox);
        if (d.size() > 0) {
            var h = $(".symbol", d);
            if (h.size() > 0) {
                var e = this.$searchbox.val().trim();
                var a = e.lastIndexOf(",");
                var g = e.lastIndexOf(":");
                var f = a != -1 && a > g;
                var c = g != -1 && g > a;
                if (f) {
                    var b = e.split(",");
                    b.pop();
                    b.push(h.text());
                    this.$searchbox.val(b.join(","))
                } else {
                    if (c) {
                        b = e.split(":");
                        b.pop();
                        b.push(h.text());
                        this.$searchbox.val(b.join(":"))
                    } else {
                        this.$searchbox.val(h.text())
                    }
                }
            } else {
                this.$searchbox.val(this.lastTyped)
            }
        } else {
            this.$searchbox.val(this.lastTyped)
        }
    }
};
