$(document).ready(function(){
    $.each({
      prevOf: "previousSibling",
      nextOf: "nextSibling"
    }, function( method, traversal ) {
      $.fn[ method ] = function( selector ) {
        return this.pushStack( this.map(function() {
          var ret = this[ traversal ];
          while ( ret && !$( ret ).is( selector ) ) {
            ret = ret[ traversal ];
          }
          return ret;
        }));
      };
    });

    $.extend( $.ui.menu.prototype, {
      next: function() {
        this.move("next", ".ui-menu-item:first");
      },

      previous: function() {
        this.move("prev", ".ui-menu-item:last");
      },

      move: function(direction, edge) {
        if (!this.active) {
          this.activate(this.element.children(edge));
          return;
        }
        var next = this.active[direction + "Of"]('.ui-menu-item');
        if (next.length) {
          this.activate(next);
        } else {
          this.activate(this.element.children(edge));
        }
      }
    });

		$.widget("custom.catcomplete", $.ui.autocomplete, {
				_renderMenu: function( ul, items ) {
            var self = this,
            currentCategory = "";
            $.each( items, function( index, item ) {
                if ( item.category != currentCategory ) {
                    ul.append( "<li class='category'><span>" + item.category + "</span></li>" );
                    currentCategory = item.category;
                }
                self._renderItem( ul, item );
             });
        },
        _renderItem: function( ul, item) {
					return $( "<li></li>" )
						.data( "item.autocomplete", item )
						.append( "<a><strong>" + item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + this.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\\\])/gi, "\\\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "</strong>$1<strong>") + "</strong></a>" )
						.appendTo( ul );
          }
    });

	var suggestUri = '/suche/suggest.php';
	$('.acSearch').catcomplete({
		source: suggestUri, minLength: 2, delay: 300
	});

});

function addClickCount(entUrl, entId) {
	
	$.ajax({
	  url: '/suche/clickcount.php',
	  data: {hit: entId}
	});
}