                                                 /**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/*********************************************
* Autocomplete f&uuml;r Suche Input Felder und Popup Menu Verwaltung
*********************************************/
jQuery.fn.searchSuggest = function(userOptions)
{
  var options = {
    input: $(this)
  }
  $.extend(options, userOptions);
  
  var ENTER = 13;
  var UP = 38;
  var DOWN = 40;
  var ESC = 27;
  var timeout;
  
  function searchSubmit() {
  	if($("#addParam").val().search(/entry_id/) > -1) {
  		tmpArr = $("#addParam").val().split("_");
  		id = tmpArr[tmpArr.length-1];
  		document.location.href=$("#targetAction").val() + id;
  	} else if($("#addParam").val().search(/city_id/) > -1) {
  		tmpArr = $("#addParam").val().split("_");
  		id = tmpArr[tmpArr.length-1];
  		document.location.href=$("#targetAction").val() + id;
  	} else if($("#addParam").val().search(/geolocation_id/) > -1) {
  		tmpArr = $("#addParam").val().split("_");
  		id = tmpArr[tmpArr.length-1];
  		document.location.href=$("#targetAction").val() + id;
  	} else {
	  	$("#header_searchform").unbind("submit");
	  	$("#header_searchform").submit();
  	}
  }

  function lookup(inputString)
  {
    $('input[name=filter]').val('a');
    $('input[name=isExact]').val('0');
    if(inputString.length == 0) {
      $('#suggestions').hide();
      $('#outer').hide();
	  $('#fade1').css("z-index",1);
    } else if (inputString.length > 0){
      $.post("/applications/toiWeb/index.cfm?do=sea.searchSuggest&searchPattern="+encodeURIComponent($("#searchPhrase").val()), {autocomplete: ""+inputString+""}, function(data){
          if(trim(data).length > 0) {
              $('#suggestions').html(trim(data));
			  $('#fade1').css("z-index",5);
              $('#suggestions').show();
              $('#outer').show();
              decorate();
          } else {
            $('#suggestions').hide();
            $('#outer').hide();
			$('#fade1').css("z-index",1);
          }
      });
    }
  }

  function menuOver($item)
  {
    $item.addClass('iehover');
  }

  function menuOut($item)
  {
    $item.removeClass('iehover');
  }

  function decorate()
  {
    $('ul#hmenu li.acWert').
      mouseover(function(){
        menuOver($(this));
      }).
      mouseout(function(){
        menuOut($(this));
      }).
      click(function(){
  		if($(this).hasClass('filterHot')) {
          filter = '/katalog/tagungseinrichtung.cfm?do=ent.overview&entry_id=';
        } else if($(this).hasClass('filterEvl')) {
          filter = '/katalog/tagungseinrichtung.cfm?do=ent.overview&entry_id=';
        } else if($(this).hasClass('filterCim')) {
          filter = '/katalog/ortsinformation.cfm?do=ent.overview&entry_id=';
        } else if($(this).hasClass('filterEva')) {
          filter = '/katalog/info_partner.cfm?do=ent.overview&entry_id=';
        } else if($(this).hasClass('filterCit')) {
        	filter = '/suche/schnellsuche_neu.cfm?do=sea.citSearch&city_id='
        } else if($(this).hasClass('filterMap')) {
        	filter='/suche/umkreissuche.cfm?do=sea.radiusSearch&geolocation_id='       
        } else 
          filter = '';
        fill($(this).attr('title'), filter, $(this).attr('name'));
        $('input[name=isExact]').val('1');
        if(options.input.attr('id') == 'searchAdvanced') {
          if(checkSearchErw()) {
            searchSubmit();
          }
        } else {
          searchSubmit();
        }
     });
  }

  function fill(thisValue, filter, addParam)
  {
  	addParam = addParam || "";
    $('#targetAction').val(filter);
    $('#searchPhrase').val(thisValue);
    $('#addParam').val(addParam);
    $('#suggestions').hide();
    $('#outer').hide();
	$('#fade1').css("z-index",1);
  }

    $(this).
      unbind('keyup').
      keyup(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        switch(code) {
          case DOWN:
          case UP:
            liSelected = $('#hmenu .acWert').index($('#hmenu .acWert.iehover'));
            liCount = $('#hmenu .acWert').size();
            //alert(e.which + ' ' + liSelected + ' ' + liCount);
            if(e.which == DOWN) {
              index = liSelected + 1;
              if(index >= liCount) {
                index = liCount - 1;
              }
              //alert('key: ' + e.which + ' li: ' + liSelected + ' i: ' + index + ' count: ' + liCount);
            } else {
              index = liSelected - 1;
              if(index < 0) {
                index = 0;
              }
              //alert('key: ' + e.which + ' li: ' + liSelected + ' i: ' + index + ' count: ' + liCount);
            }
            $('#hmenu .acWert').removeClass('iehover');
            $('#hmenu .acWert').eq(index).addClass('iehover');
            //alert('li: ' + liSelected + ' title: ' + $('#hmenu .acWert').eq(index).attr('title'));
            break;
          case ENTER:
            var filter = 'a';
            if($('#hmenu .iehover').hasClass('filterHot')) {
	        	filter = '/katalog/tagungseinrichtung.cfm?do=ent.overview&entry_id=';
	        } else if($('#hmenu .iehover').hasClass('filterEvl')) {
	          filter = '/katalog/tagungseinrichtung.cfm?do=ent.overview&entry_id=';
	        } else if($('#hmenu .iehover').hasClass('filterCim')) {
	          filter = '/katalog/ortsinformation.cfm?do=ent.overview&entry_id=';
	        } else if($('#hmenu .iehover').hasClass('filterEva')) {
	          filter = '/katalog/info_partner.cfm?do=ent.overview&entry_id=';
	        } else if($('#hmenu .iehover').hasClass('filterCit')) {
	        	filter = '/suche/schnellsuche_neu.cfm?do=sea.citSearch&city_id='
	        } else if($('#hmenu .iehover').hasClass('filterMap')) {
        		filter='/suche/umkreissuche.cfm?do=sea.radiusSearch&geolocation_id='       
        	}
            value = $('#hmenu .iehover').attr('title');
            addParam = $('#hmenu .iehover').attr('name');
            if(value) {
              fill(value, filter, addParam);
              $('input[name=isExact]').val('1');
            }
            if(options.input.attr('id') == 'searchAdvanced') {
              if(checkSearchErw()) {
                searchSubmit();
                //ajaxGetExhibListParam(this.form, 'nav=search');
              }
            } else {
              searchSubmit();
              //ajaxGetExhibListParam(this.form, 'nav=search');
            }
            break;
          case ESC:
            $('#hmenu .acWert').removeClass('iehover');
            $('#suggestions').hide();
            $('#outer').hide();
			$('#fade1').css("z-index",1);
            break;
         default:
            $('#outer').remove();
            var outerDiv = $("<div id='outer'>").append($("<div id='suggestions'>"));
            var pos = $(this).position();
            var leftOffset = parseInt($(this).css('margin-left'));
            var topOffset = parseInt($(this).css('margin-top'));
            var height = $(this).height();
            //alert("pos.top: " + pos.top +"\nheight: " + height + "\ntopOffset: "+topOffset+"\npos.left: "+pos.left+"\nleftOffset: "+leftOffset);
            outerDiv.css({
              'top': pos.top + height + topOffset + 4,
              'left': pos.left + leftOffset
            });
            $(this).after(outerDiv);
            //lookup(this.value);
            sp = this.value;
     		clearTimeout(timeout);
            timeout = setTimeout(function() {lookup(sp);},350);
            break;
         }
      });

}

