var AjaxReq = null;
var SuggestDiv = null;
var SuggestAktuell = 0;
var SuggestVisible = false;
var SuggestText;
function FFSuggest(divName, rqName, php_script){
	SuggestDiv = document.getElementById(divName);
	PHPscript = php_script;
	rqName = rqName;
	
	this.searchBox = document.quick_find[0][rqName];
	this.query = '';
	this.requestUrl = '';
	this.Result = null;
	this.divMouseOver = false;
  document.quick_find[0].onsubmit = function(){
  	if(SuggestAktuell){
      var row = document.getElementById("SuggestRow"+SuggestAktuell);
      if(row){
        if(row.childNodes[1].textContent){
          SuggestText = row.childNodes[1].textContent;
        } else if(row.childNodes[0].innerText) {
          SuggestText = row.childNodes[0].innerText;  
        }
      }
  	  document.quick_find[0][rqName].value = SuggestText;  
  	}
  }
  this.searchBox.onkeyup = function(event){
    if(!event){
  		var code = window.event.keyCode;
		} else {
			var code = event.keyCode;
		}
		switch(code){
			case 37: // nach links
			case 39: // nach rechts
			case 38: // nach oben
			case 40: // nach unten
			    moveHighlight(code);
			    break;
			case 13: // enter
			    break;
      case 27: // escape
          SuggestDiv.style.display = "none";
			    break;
			default:
			    SendQuery(this.value, PHPscript, rqName);
			    break;
		}
  }
  
  this.searchBox.onfocus = function(){
		if(SuggestDiv.innerHTML != ''){
		  SuggestDiv.style.display = "block";
		  SuggestVisible = true;
		}
	}
	
	this.searchBox.onblur = function(){
		SuggestDiv.style.display = "none";
		SuggestVisible = false;
	}
  
  function SendQuery(value,url,rqName){
		//this.requestUrl = "http://dev1.fact-finder.de/Comic4You/Suggest.ff?query=" + this.query;
		var requestUrl = url+"?" + rqName + "=" + value;
		try {
      if(window.XMLHttpRequest) { 
        AjaxReq = new XMLHttpRequest(); 
      } else if(window.ActiveXObject) {
        AjaxReq = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      if(AjaxReq){
        AjaxReq.open("GET", requestUrl, true);
        AjaxReq.onreadystatechange = ShowResult;
        AjaxReq.send(null);
      } 
    } catch(ex) {
    	alert("Error: " + ex.getmessage());
    }
	}

	ShowResult = function(){
		if(AjaxReq.readyState == 4) {
      if(AjaxReq.status == 200) {
      	SuggestDiv.innerHTML = '';
      	if(AjaxReq.responseText.length > 0){
      	  SuggestDiv.innerHTML = AjaxReq.responseText;
          SuggestDiv.style.display = 'block';
          SuggestVisible = true;
          SuggestAktuell = 0;
      	} else {
      		SuggestDiv.style.display = 'none';
      		SuggestDiv.innerHTML = '';
      		SuggestVisible = false;
      	}
      } 
    }
	}
	
	function moveHighlight(code){
		if(code == 38){
      // Nach oben bewegen
			if(SuggestVisible){
				if(SuggestAktuell > 1){
					setHighlight(SuggestAktuell-1);
				} else {
					for(var n = 1; n <= 20; n++){
						if(!document.getElementById("SuggestRow"+n))
						  break;
					}
					setHighlight(n-1);
				}
				
			}
		} else if(code == 40){
			// Nach unten bewegen
			if(SuggestVisible){
			  if(SuggestAktuell == 0){
				  SuggestAktuell = 1;
				  setHighlight(SuggestAktuell)
			  } else {
			    div = document.getElementById("SuggestRow"+(SuggestAktuell + 1));
			    if(div){
			  	  setHighlight(SuggestAktuell + 1);
			    } else {
			    	setHighlight(1);
			    }
			  }
			}
		}
	}
}

function setHighlight(nr){
	if(SuggestAktuell){
		var tr = document.getElementById("SuggestRow"+SuggestAktuell);
		tr.className = "suggestRow";
	}
	SuggestAktuell = nr;
	if(nr){
	  var newtr = document.getElementById("SuggestRow"+SuggestAktuell);
	  newtr.className = "suggestMouseOver";
  }
}