function getPos (o){

    var pos = { x:0, y:0 };
    if(typeof o.offsetTop != 'undefined')    {
         while (o && o.tagName != 'BODY')         {
              pos.y += parseInt( o.offsetTop );
              pos.x += parseInt( o.offsetLeft );
              o = o.offsetParent;
         }
    }
    return pos;
}

var Search = function() {
    this.input = null;
    this.preloader = null;
    this.xhr   = new XMLHttpRequest();
    this.container = null;
    this.current = -1;
    this.lastPhrase = null;
    this.delay = null;
}

Search.prototype.setInput = function (tag) {
    tag.focus();
    this.input = tag;
    this.input.autocomplete = "off";
    var that = this;
    if (this.input.addEventListener) {
        this.input.addEventListener("keyup", function (event) {that.keyUp(event); }, false);
        this.input.addEventListener("keydown", function (event) {that.keyDown(event); }, false);
        this.input.addEventListener("click", function (event) {that.close() }, false);
    } else {
        this.input.attachEvent("onkeyup",  function (event) {that.keyUp(event); });
        this.input.attachEvent("onkeydown",   function (event) {that.keyDown(event); });
        this.input.attachEvent("click", function (event) {that.close() });
    }
}

Search.prototype.close = function () {
    if (this.container != null) {
        this.container.parentNode.removeChild(this.container);
        this.container = null;
    }
}

Search.prototype.keyDown = function (event) {
    var key = event.keyCode;
    switch (key) {
        case 40: // down
            this.current++;
            if (this.current >= this.container.childNodes.length) {
                this.current = 0;
            }
            this.markCurrent();
            break;
        case 38: // up
            this.current--;
            if (this.current < 0) {
                this.current = this.container.childNodes.length - 1;
            }
            this.markCurrent();
            break;
        case 27: // esc
            this.close();
            break;
        case 13: // return
            if (this.container == null) {
                return;
            }
            document.location.href = this.container.childNodes[this.current].link;
            event.returnValue = false;
            event.preventDefault();
    }
}

Search.prototype.markCurrent = function () {
    for (var i=0; i<this.container.childNodes.length; i++) {
        this.container.childNodes[i].className = "suggest_hit";
    }
    this.container.childNodes[this.current].className = "suggest_hit_current";
}

Search.prototype.keyUp = function (event) {
    if (this.delay != null) {
        return;
    }
    var that = this;
    this.delay = window.setTimeout(function () { that.checkPhrase() }, 500);
}

Search.prototype.checkPhrase = function() {
//    this.input.style.color = "yellow";
    this.delay = null;
    var phrase = this.input.value;
    if (phrase == this.phrase) {
        return;
    }
    this.input.style.color = "black";
     this.phrase = phrase;
    if (phrase.length < 4) {
        this.close();
        return;
    }

    if (this.preloader != null) {
        this.preloader.style.display = "block";
    }
    var that = this;
    this.xhr.onreadystatechange = function (response) {if (that.xhr.readyState == 4) { that.handleXhr(); }};
//    this.xhr.open("GET", "typo3conf/ext/weka_articles/xhr/search.php?phrase="+escape(phrase), true);
    this.xhr.open("GET", "typo3conf/ext/weka_articles/xhr/search.php?phrase="+phrase, true);
    this.xhr.send(null);
}

Search.prototype.setPreloader = function (tag) {
    this.preloader = tag;
    this.preloader.style.display = "none";
}

Search.prototype.setContainer = function () {
    var pos = getPos(this.input);
    var container = document.createElement("div");
    container.className = "suggest_container";
    container.style.top = (pos.y+20)+"px";
    container.style.left = (pos.x+1)+"px";

    document.getElementsByTagName("body")[0].appendChild(container);
    
    this.container = container;
}

Search.prototype.getHitbox = function (data) {
    var box = document.createElement("div");
    box.className = "suggest_hit";
    box.link = "http://www.elektroniknet.de/redirect?cat="+data.cat+"&id="+data.id;
    box.onclick = function () {
        document.location.href = "http://www.elektroniknet.de/redirect?cat="+data.cat+"&id="+data.id;
    };
    box.innerHTML = "<nobr><strong>"+data.cat+":</strong> "+data.text+" <em>"+data.date+"</em></nobr>";
    return box;
}

Search.prototype.handleXhr = function() {
//this.input.style.color = "lightgreen";
    if (this.preloader != null) {
        this.preloader.style.display = "none";
    }
    var data = eval("("+this.xhr.responseText+")");
    this.close();
    if (data.length < 1) {
        this.input.style.color = "red";
        return;
    }
    this.setContainer();
    this.current = -1;

    this.container.style.display = "block";
    for (var i=0; i<data.length; i++) {
        this.container.appendChild(this.getHitbox(data[i]));
    }
}


