/**
 *
 * Autocompletebox
 * (c) 2008 for Atrahor.de
 * by Jenutan [at] ist-einmalig [dot] de
 * 
 */

// Some ideas copied by:

// Cross-browser implementation of element.addEventListener()
// http://snipplr.com/view/561/add-event-listener/

// http://www.matts411.com/webdev/width_and_height_getter_functions_for_html_elements



function addListener(element,type,expression,bubbling)
{
  bubbling=bubbling||false;
  if(window.addEventListener){
    element.addEventListener(type,expression,bubbling);
    return true;
    }else if(window.attachEvent){
      element.attachEvent('on'+type,expression);
      return true;
      }else
      return false;
}

var getHeight=function(el,includePadding,includeBorder){
  var height;
  el=(typeof(el)==="string")?document.getElementById(el):el;
  if(window.getComputedStyle){
    var style=document.defaultView.getComputedStyle(el,null);
    if(style.getPropertyValue("display")==="none")
    return 0;
    height=parseInt(style.getPropertyValue("height"));

    if(window.opera&&!document.getElementsByClassName){
      height-=parseInt(style.getPropertyValue("padding-top"));
      height-=parseInt(style.getPropertyValue("padding-bottom"));
      height-=parseInt(style.getPropertyValue("border-top-width"));
      height-=parseInt(style.getPropertyValue("border-bottom-width"));
    }
    if(includePadding){
      height+=parseInt(style.getPropertyValue("padding-top"));
      height+=parseInt(style.getPropertyValue("padding-bottom"));
    }

    if(includeBorder){
      height+=parseInt(style.getPropertyValue("border-top-width"));
      height+=parseInt(style.getPropertyValue("border-bottom-width"));
    }
    }else{

      if(el.currentStyle["display"]==="none")
      return 0;
      var bRegex=/thin|medium|thick/;height=el.offsetHeight;

      if(!includeBorder){
        var borderTopCSS=el.currentStyle["borderTopWidth"];
        var borderBottomCSS=el.currentStyle["borderBottomWidth"];
        var temp=document.createElement("DIV");

        if(el.offsetHeight>el.clientHeight&&el.currentStyle["borderTopStyle"]!=="none"){
          if(!bRegex.test(borderTopCSS)){
            temp.style.width=borderTopCSS;el.parentNode.appendChild(temp);
            height-=temp.offsetWidth;el.parentNode.removeChild(temp);
            }else if(bRegex.test(borderTopCSS)){
              temp.style.width="10px";
              temp.style.border=borderTopCSS+" "+el.currentStyle["borderTopStyle"]+" #000000";
              el.parentNode.appendChild(temp);
              height-=Math.round((temp.offsetWidth-10)/2);
              el.parentNode.removeChild(temp);
            }
          }

          if(el.offsetHeight>el.clientHeight&&el.currentStyle["borderBottomStyle"]!=="none"){
            if(!bRegex.test(borderBottomCSS)){
              temp.style.width=borderBottomCSS;
              el.parentNode.appendChild(temp);
              height-=temp.offsetWidth;el.parentNode.removeChild(temp);
              }else if(bRegex.test(borderBottomCSS)){
                temp.style.width="10px";
                temp.style.border=borderBottomCSS+" "+el.currentStyle["borderBottomStyle"]+" #000000";
                el.parentNode.appendChild(temp);
                height-=Math.round((temp.offsetWidth-10)/2);
                el.parentNode.removeChild(temp);
              }
            }
          }

          if(!includePadding){
            var paddingTopCSS=el.currentStyle["paddingTop"];
            var paddingBottomCSS=el.currentStyle["paddingBottom"];
            var temp=document.createElement("DIV");
            temp.style.width=paddingTopCSS;
            el.parentNode.appendChild(temp);
            height-=temp.offsetWidth;temp.style.width=paddingBottomCSS;
            height-=temp.offsetWidth;
            el.parentNode.removeChild(temp);
          }
        }
        return height;
        };

        function getPosition(obj){
          var pos={x:0,y:0};
          do{pos.x+=obj.offsetLeft;pos.y+=obj.offsetTop;
          }while(obj=obj.offsetParent);
          return pos;
        }

        function setPositionX(obj,val){
          if(document.all){
            obj.style.posLeft=val;
            }else{
              obj.style.left=val+'px';
            }
          }

          function setPositionY(obj,val){
            if(document.all){
              obj.style.posTop=val;
            }else{
              obj.style.top=val+'px';
            }
          }

          function $(string){
            return document.getElementById(string);
          }

          var ac_req=(window.XMLHttpRequest?new XMLHttpRequest():(window.ActiveXObject?new ActiveXObject('Microsoft.XMLHTTP'):document.write('Browser inkompatibel:P')));
          var ac_input=$('txtSearch');
          var ac_suggest=$('search_suggest');
          var ac_delay={'ID':null,'time':500};
          var ac_div_sel=null;
          var ac_css={'link':'suggest_link','over':'suggest_link_over'};

          function ac_clearList(){
            ac_suggest.innerHTML='';
            ac_input.focus();
            ac_div_sel=null;
            var pos_input=getPosition(ac_input);
            setPositionX(ac_suggest,pos_input.x);
            setPositionY(ac_suggest,pos_input.y+getHeight(ac_input,true,true));
          }

          function ac_keynav(e){
            if(!ac_input.value)return false;
            switch(e.keyCode){
              case 9:
              case 13:
              ac_setCurrentSelect();
              break;

              case 27:
              ac_clearList();
              break;

              case 38:ac_selectPrevResult();
              break;

              case 40:
              if(!ac_suggest.innerHTML){
                ac_sendRequest();
              }
              ac_selectNextResult();
              break;

              default:
              ac_clearList();
              if(ac_delay.ID){
                clearTimeout(ac_delay.ID);
                ac_delay.ID=null;
              }

              if(ac_input.value.length>=3){
                ac_delay.ID=setTimeout(ac_sendRequest,ac_delay.time);
                ac_suggest.innerHTML='<div class="suggest_link"><img src="./jslib/wait.gif" alt="Laden..."  /></div>';
              }
              break;
            }
          };

          function ac_sendRequest(){
            ac_req.open('GET','httpreq_autocomplete_name.php?look='+encodeURIComponent(ac_input.value),true);
            ac_req.onreadystatechange=ac_handleSearch;
            ac_req.send(null);
          }

          function ac_handleSearch(){
            if(ac_req.readyState!=4)
            return;
            ac_clearList();
            var str=eval(ac_req.responseText);
            for(var i=0;i<str.length;i++)
            ac_suggest.innerHTML+='<div onmouseover="javascript:ac_selectResult(this.id, true);" onmouseout="javascript:ac_div_out(this);" onclick="javascript:ac_setCurrentSelect();" class="'+ac_css.link+'" id="suggest_'+i+'">'+str[i].name+'</div>';
            if(ac_suggest.innerHTML)
            ac_suggest.innerHTML+='<div class="'+ac_css.link+'"><div style="float:right"><a href="javascript:ac_clearList();">Schließen</a></div>&nbsp;</div>';
          }

          function ac_div_out(div){
            div.className=ac_css.link;
            ac_div_sel=null;
          }

          function ac_setCurrentSelect(){
            if(!ac_div_sel)
            return false;
            ac_input.value=$(ac_div_sel).innerHTML;
            ac_clearList();
          }
          function ac_selectNextResult(){
            if(ac_div_sel){
              var next=ac_div_sel.substr(8);
              next=parseInt(next);
              next++;
              if($('suggest_'+next)){
                ac_selectResult(next);
              }
              }else{
                ac_selectResult(0);
              }
            }

            function ac_selectPrevResult(){
              if(ac_div_sel){
                var prev=ac_div_sel.substr(8);
                prev=parseInt(prev);
                if(prev>=1){
                  prev--;
                  if($('suggest_'+prev)){
                    ac_selectResult(prev);
                  }
                }
              }
            }
            function ac_selectResult(id,select){
              if(!isNaN(id)){
                id='suggest_'+id;
              }
              id=$(id);
              if(!id)return;
              for(var i=0;$('suggest_'+i);i++){
                $('suggest_'+i).className=ac_css.link;
              }
              id.className=ac_css.over;
              ac_div_sel=id.id;
              if(select)
              return;
              ac_input.value=id.innerHTML;
            }
            addListener(document,'keyup',ac_keynav);
