$(document).ready(function() {
	$("input[name=search-query]").typeahead({
		minLength: 2,
		source: function (query,process) {
			search (query,process);
		},

		matcher: function () { return true; },

		updater: function (item) {
			window.parent.location.href = site_url+users[item];
		},
	});
});

var search = _.debounce(function( query, process ){
	$.get(site_url+"/u/search/ajaxlist", {term: query}, function (data) {
		users = {};
		userLabels = [];

		_.each( data, function( item, ix, list ){
			if ( _.contains( users, item.label ) ){
				item.label = item.label + ' #' + item.value;
			}

			userLabels.push( item.label );
			users[ item.label ] = item.value;
		});

		process( userLabels );
	})
}, 300);