$(document).ready(function() {
    // customize the autocomplete widget
    $.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='ui-autocomplete-category'>" + item.category + "</li>" );
		    currentCategory = item.category;
		}
                self._renderItem( ul, item );
            });
	}
    });
    
    // invoke the search box 
    $("#search-box").catcomplete({
	delay: 0,
	source: '/ajax/autocomplete',
	select: function(event, ui) {
            window.location.href = ui.item.url;
    	}
    });
});
