/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH &lt;devel@bitflux.ch&gt;                              |
// +----------------------------------------------------------------------+

*/
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var ishidden=true;
var isIE = false;
var debug=false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
    liveSearchReq = new XMLHttpRequest();
}

function liveSearchInit() {
    
    
    document.getElementById("suchenlayer").style.display = "none";
    ishidden=true;
    
    if (navigator.userAgent.indexOf("Safari") > 0) {
        document.getElementById('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
        document.getElementById('livesearch').addEventListener("blur",liveSearchHide,false);
        } else if (navigator.product == "Gecko") {
        
        document.getElementById('livesearch').addEventListener("keypress",liveSearchKeyPress,false);
        document.getElementById('livesearch').addEventListener("blur",liveSearchHideDelayed,false);
        
        } else {
        document.getElementById('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
        document.getElementById('livesearch').attachEvent("onblur",liveSearchHideDelayed,false);
        isIE = true;
    }
    
    document.getElementById('livesearch').setAttribute("autocomplete","off");
    
    e = document.getElementById('livesearch');
    if (e.addEventListener) {
        e.addEventListener("keypress", liveSearchStart, false);
        } else if (e.attachEvent) {
        e.attachEvent("onkeypress", liveSearchStart, false);
    }
    log("init");
    
    
}

function log(t) {
  	
    if(debug==true && null!=document.debug.debug) {
        var d=document.debug.debug;
        if(d) {
            //alert("log"+d+" "+t+" ("+d.value+")");
            d.value=d.value+t+"\n";
        }
     }
    
}

function liveSearchHideDelayed() {
    window.setTimeout("liveSearchHide()",400);
}

function liveSearchHide() {
    
    document.getElementById("suchenlayer").style.display = "none";
    /*
    var highlight = document.LSResultyId("LSHighlight");
    if (highlight) {
        highlight.removeAttribute("id");
    }
    */
    log("hidden");
    
}

function liveSearchKeyPress(event) {
    if (event.keyCode == 40 )
    //KEY DOWN
    {
        highlight = document.getElementById("LSHighlight");
        if (!highlight) {
            highlight = document.getElementById("LSShadow").firstChild.firstChild;
            } else {
            highlight.removeAttribute("id");
            highlight = highlight.nextSibling;
        }
        if (highlight) {
            highlight.setAttribute("id","LSHighlight");
        }
        if (!isIE) { event.preventDefault(); }
    }
    //KEY UP
    else if (event.keyCode == 38 ) {
        highlight = document.getElementById("LSHighlight");
        if (!highlight) {
            highlight = document.getElementById("LSResult").firstChild.firstChild.lastChild;
        }
        else {
            highlight.removeAttribute("id");
            highlight = highlight.previousSibling;
        }
        if (highlight) {
            highlight.setAttribute("id","LSHighlight");
        }
        if (!isIE) { event.preventDefault(); }
    }
    //ESC
    else if (event.keyCode == 27) {
        highlight = document.getElementById("LSHighlight");
        if (highlight) {
            highlight.removeAttribute("id");
        }
        document.getElementById("suchenlayer").style.display = "none";
        ishidden=true;
    }
    //BACKSPACE - required for IE
    else if (event.keyCode == 8 && isIE) {
        liveSearchStart();
    }
}
function liveSearchStart() {
    if (t) {
        window.clearTimeout(t);
    }
    t = window.setTimeout("liveSearchDoSearch()",200);
}

function liveSearchDoSearch() {
    
    
    
    if (typeof liveSearchRoot == "undefined") {
        liveSearchRoot = "";
    }
    if (typeof liveSearchRootSubDir == "undefined") {
        liveSearchRootSubDir = "";
    }
    if (typeof liveSearchParams == "undefined") {
        liveSearchParams2 = "";
        } else {
        liveSearchParams2 = "&" + liveSearchParams;
    }
    if (liveSearchLast != document.forms.searchform.suchbegriff.value) {
        if (liveSearchReq && liveSearchReq.readyState < 4) {
            liveSearchReq.abort();
        }
        if ( document.forms.searchform.suchbegriff.value == "") {
            liveSearchHide();
            liveSearchLast = "";
            return false;
        }
        if (window.XMLHttpRequest) {
            // branch for IE/Windows ActiveX version
            } else if (window.ActiveXObject) {
            liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
        liveSearchReq = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            liveSearchReq = new XMLHttpRequest();
            if (liveSearchReq.overrideMimeType) {
                liveSearchReq.overrideMimeType('text/xml');
                // See note below about this line
            }
            } else if (window.ActiveXObject) { // IE
            try {
                liveSearchReq = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                try {
                    liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
            }
        }
        
        liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
        liveSearchReq.open("GET", liveSearchRoot + "/winklerssuche/ergebnis.xtp?search=" + encodeURIComponent(document.forms.searchform.suchbegriff.value) +"&egal="+document.forms.searchform.suchbegriff.value+ liveSearchParams2);
        liveSearchLast = document.forms.searchform.suchbegriff.value;
        //alert(liveSearchRoot + "/winklerssuche/ergebnis.xtp?search=" + encodeURIComponent(document.forms.searchform.suchbegriff.value) +"&egal="+document.forms.searchform.suchbegriff.value+ liveSearchParams2);
        liveSearchReq.send(null);
        log("liveSearchDoSearch("+document.forms.searchform.suchbegriff.value+")"+liveSearchReq.readyState);
    }
}

function liveSearchProcessReqChange() {
    
    if (liveSearchReq.readyState == 4) {
        if(liveSearchReq.responseText.length!=1268) {
            var  res = document.getElementById("suchenlayer");
            res.style.display = "block";
            ishidden=false;
            //var  sh = document.getElementById("LSShadow");
            
            res.innerHTML = liveSearchReq.responseText;
            } else {
            liveSearchHide();
        }
        
        
    }
    log("got("+liveSearchReq.readyState+") something back("+liveSearchReq.responseText.length+")");
}

var liveSearchRoot = '';
var liveSearchParams = 'livesearch=do';
try {
	onload=liveSearchInit();
} catch(e) {
// nutty explorer throws errors ... 
}