﻿
//Instanz der Klasse Ajax erzeugen und mit der Datenuebertragung starten
var key_navi = null;

function suggest(suchbegriff, div_id, txt_id, e)
{
  var eingabe  = suchbegriff.value;
  if(eingabe.substr(0,1) == ".") { eingabe = eingabe.substr(1); var point = "."; } else { var point = ""; }
  
  var taste = e.keyCode? e.keyCode : e.charCode
  if(!(taste == 37 || taste == 38 || taste == 39 || taste == 40 || taste == 190))
  {
  	new Ajax.Request("http://www.spanien-andalusien.com/scripte/suggest.php",
    { method:"POST", parameters:"eingabe="+eingabe+"&div_id="+div_id+"&txt_id="+txt_id , onSuccess:successHandler } );
  }

  //Den Text in die Seite einfuegen
  function successHandler(txt,xml)
  {
    $(div_id).innerHTML=txt.responseText;
    var suggest_div = $(div_id);
    var txt_div = $(txt_id);
    var lis = $(div_id).getElementsByTagName('li').length;
    var suggest_list = $('suggest_list');

    if(!key_navi)
	{
		key_navi = new keynavigation(suggest_list, div_id, txt_id, point);
	}
    else
    {
		key_navi.updateList(suggest_list);
	}

    if (lis == 0 || eingabe == "" || eingabe == "suchen")
    {
      suggest_div.style.visibility = 'hidden';    
    }
    else if (suggest_div.innerHTML != "")
    {
      suggest_div.style.visibility = 'visible';
    }
  }
}

function insert_eingabe(begriff, txt_id, div_id)
{
  var txt = $(txt_id);
  var div = $(div_id);
  
  if (txt && div)
  {
    txt.value = begriff;
    div.style.visibility = 'hidden';
  }
}

function schliessen(div_id)
{
  $(div_id).style.visibility = 'hidden';
}

function eingabe(eingabe_id)
{
  var eingabe = $(eingabe_id);
  eingabe.focus();
}

function div_close(div_fast, div)
{
  if($(div_fast))
  {
    $(div_fast).style.visibility = 'hidden';
  }

  if($(div))
  {
    $(div).style.visibility = 'hidden';
  }
}


// Event Handling Funktion
function eventHandling(element,type,callBack,returnParams)
{
  var returnParams = returnParams;
  if(document.addEventListener)
  {
	if(type.match(/^on/)) type = type.replace(/^on/,"");
    element.addEventListener(type,handleEvent,false);
  }
  else
  { // IE
    if(!type.match(/^on/))  type = "on"+type;
    element.attachEvent(type,handleEvent);
  }

  function handleEvent (evt)
  {
    var event  = (evt)?evt:(window.event)?window.event:'';
    if(event.stopPropagation)
    {
      event.stopPropagation();
    }
    else
    {
      event.cancelBubble = true;
    }
    var target = event.srcElement || event.currentTarget;
    callBack.call(callBack,event,target,(returnParams)?returnParams:null);
  }
}

function keynavigation (obj, div_id, txt_id, point)
{
  var curElement  = null;
  obj.innerHTML   = clearSpaces(obj.innerHTML)
  var naviElement = obj;

  eventHandling(document,"keydown",keyPressed);

  function clearSpaces (strOut)
  {
    strOut = strOut.replace(/li>\s+/gm,'li>');
    strOut = strOut.replace(/\s+<li/gm,'<li');
    strOut = strOut.replace(/li>\s+<li/gm,'li><li');
    return strOut;
  };

  function keyPressed (evt)
  {
    var curKey = evt.keyCode;
    switch (curKey)
    {
      case 38: // hoch
      if(curElement && !curElement.previousSibling)
      {
        curElement = naviElement.lastChild.previousSibling;
        $(txt_id).value = point + curElement.firstChild.id;
        curElement.style.backgroundColor = '#6990b6';
        curElement.style.color = '#FFFFFF';

        firstElement = naviElement.firstChild;
        firstElement.style.backgroundColor = '';
        firstElement.style.color = '#000000';
      }
      else if(curElement && curElement.previousSibling)
      {
        curElement = curElement.previousSibling;
        $(txt_id).value = point + curElement.firstChild.id;
        curElement.style.backgroundColor = '#6990b6';
        curElement.style.color = '#FFFFFF';

        if(curElement.nextSibling)
        {
          curElement.nextSibling.style.color = '#000000';
          curElement.nextSibling.style.backgroundColor = '';
        }
      }
      else if(!curElement)
      {
        curElement = naviElement.lastChild.previousSibling;
        $(txt_id).value = point + curElement.firstChild.id;
        curElement.style.backgroundColor = '#6990b6';
        curElement.style.color = '#FFFFFF';
      }
      break;

      case 40: // runter
      if(curElement)
      { 
        if(!curElement.nextSibling.nextSibling)
        {
          curElement = naviElement.firstChild;
          $(txt_id).value = point + curElement.firstChild.id;
          curElement.style.backgroundColor = '#6990b6';
          curElement.style.color = '#FFFFFF';

          lastElement = naviElement.lastChild.previousSibling;
          lastElement.style.backgroundColor = '';
          lastElement.style.color = '#000000';

        }
        else if(curElement.nextSibling)
        {

          curElement = curElement.nextSibling;
          $(txt_id).value = point + curElement.firstChild.id;
          curElement.style.backgroundColor = '#6990b6';
          curElement.style.color = '#FFFFFF';

          if(curElement.previousSibling)
          {
            curElement.previousSibling.style.color = '#000000';
            curElement.previousSibling.style.backgroundColor = '';
          }
        }
      }
      else
      {
        curElement = naviElement.firstChild;
        $(txt_id).value = point + curElement.firstChild.id;
        curElement.style.backgroundColor = '#6990b6';
      }
      break;

      case 13: // Enter Taste
      if(curElement)
      {
        $(div_id).style.visibility = 'hidden';
      }
      break;

      case 9: // Tabulator
      if(curElement)
      {
		$(div_id).style.visibility = 'hidden';
	  }

      default:  return;
    }
  };

  this.updateList = function (obj_new_list)
  {
    obj_new_list.innerHTML = clearSpaces(obj_new_list.innerHTML)
    naviElement = obj_new_list;
    curElement = null;
  };
}