/***
 * AutoComplete - Brian Wright - 9/2/2009
 * 
 * Requisites: position.js, ajax.js, lna.js
 * 
 * Usage: AutoComplete.setup(textElement, ajaxURL, onEnterPressedFunction);
 * 
 * textElement
 *  -->The object that you want to add autocomplete to
 * ajaxURL
 *  -->The url of the ajax query (minus the element value) 
 *     such as: "http://www.url.com/blah.jsp?elem="
 * onEnterPressedFunction
 *  -->The function that gets called when a focused suggestion 
 *     is triggered by an enter press. Can be either
 *     the function itself, or a string representing
 *     the function's name 
 */
var AutoComplete = (new function(){
    var k = {};
    k.setup = function(f,g,h){
        f.hideHelper = function(){
            if(f.helper){
                document.body.removeChild(f.helper);
                f.helper = null
            }
        };
        var j = f.onkeyup, z = document.onmouseup;
        document.onmouseup = function(){
            if(z)
                z();
            f.hideHelper();
        };
        f.onkeyup = function(e){
            var b = this;
            if(j)
                j(e);
            var c = getKeyCode(e);
            if(c == 40){
                if(b.helper){
                    if(b.helper.childNodes.length > 0){
                        b.helper.childNodes[0].focus()
                    }
                }
            }else if(c != 38 && c != 13){
                var q = f.value.replace(/\\/g,"");
                if(q != f.value)
                    f.value = q;
                if(b.value.length > 2){
                    AJAX.load(g + this.value.replaceAll("%","") + "&time=" + new Date().getTime(),function(x){
                        try{
                            var a = eval(x.responseText.split("\n")[1]), l = a.length, i = 0;
                            if(l > 0){
                                var d = document.createElement("div"), pos = Position.get(b), o = new StringBuffer(), pts;
                                with(d.style){
                                    position = "absolute";
                                    zIndex = "100";
                                    top = (pos.top + b.offsetHeight) + "px";
                                    left = pos.left + "px";
                                    width = "250px"
                                }
                                d.className = "acc";
                                for(i; i < l; i = i + 1){
                                    pts = a[i].split("::");
                                    o.append("<a href='javascript:;' id='" + pts[1] + "'>" + pts[0] + "</a>")
                                }
                                d.innerHTML = o.toString();
                                b.hideHelper();
                                document.body.appendChild(d);
                                b.helper = d;
                                a = d.childNodes;
                                l = a.length;
                                for(i = 0; i < l; i = i + 1){
                                    a[i].index = i;
                                    a[i].size = l;
                                    a[i].onkeyup = function(e){
                                        var c = getKeyCode(e);
                                        if(c == 40 && (this.index < this.size)){
                                            if(this.parentNode.childNodes[this.index + 1])
                                                this.parentNode.childNodes[this.index + 1].focus()
                                        }else if(c == 38 && this.index > 0){
                                            if(this.parentNode.childNodes[this.index - 1])
                                                this.parentNode.childNodes[this.index - 1].focus()
                                        }else if(c == 38 && this.index == 0){
                                            b.focus();
                                            b.hideHelper()
                                        }else if(c == 13){
                                        	/*
                                            b.value = $eit(this);
                                            b.hideHelper();
                                            if(typeof (h) == "function"){
                                                h(e)
                                            }else if(typeof (h) == "string"){
                                                eval(h)
                                            }
                                            b.focus();
                                            return false;
                                            */
                                        	b.focus();
                                            b.value = $eit(this);
                                            b.hideHelper();
                                        	document.location.href="/lna/main.do?actionSource=documentClicked&selectedDocument="+this.id +"&q=" + $(this).getText();
                                        }
                                    };
                                    a[i].onfocus = function(){
                                        this.style.backgroundColor = "#E3F2FC";
                                    };
                                    a[i].onblur = function(){
                                        this.style.backgroundColor = "#ffffff";
                                    };
                                    a[i].onmousedown = function(){                                        
                                        b.focus();
                                        b.value = $eit(this);
                                        b.hideHelper();
                                        document.location.href="/lna/main.do?actionSource=documentClicked&selectedDocument="+this.id +"&q=" + $(this).getText();
                                    };
                                }
                            }else{
                                b.hideHelper()
                            }
                        }catch(e){
                            b.hideHelper()
                        }
                    })
                }else{
                    b.hideHelper()
                }
            }
        }
    };
    return k
});
