var mvnAlgoliaPrediction = (function($) {
	var algolia;
	var that;
	var data = {};
	var self = {
		initialize: function() {
			that = this;
			if (typeof AlgoliaSearch !== "undefined") {
				this.initAlgolia();
			}
		},
		initAlgolia: function() {
			algolia = new AlgoliaSearch( mvnAlgSettings.appId, mvnAlgSettings.apiKeySearch ); // public credentials
		},
		searchCallback: function(success, content) {
			if ( success && content.results.length > 0 ) {
				var posts = content.results[0];
				if( posts.hits.length > 0 ){
					for (var i = 0; i < posts.hits.length; ++i) {
						var hit = posts.hits[i];
						data[i] = {
									label: hit.title,
									value: hit.title,
									title: hit._highlightResult.title.value,
									permalink: hit.permalink,
									categories: hit.category,
									tags: hit._tags,
									excerpt: hit._highlightResult.excerpt.value,
									description: hit._highlightResult.content.value,
									date: hit.date,
									featuredImage: hit.featuredImage
									};
					}
				}
			}
		},
		getDisplayPost: function( hit ) {
			var htmlPost = '';
			htmlPost += '			<a href="' + hit.permalink + '" class="mvn-alg-ls-item-title">';
			if( typeof hit.featuredImage !== 'undefined' && hit.featuredImage ){
				html += '	<img src="'+hit.featuredImage.file+'" width="40" height="60" />';
			}
			htmlPost += '				<strong>' + hit.title + '</strong>';

			if( typeof hit.categories !== 'undefined' ){
				htmlPost += '			<br /><span class="mvn-alg-ls-item-cats">' + hit.categories.join() + '</span>';
			}
			if( mvnAlgSearchVars.showExcerpt && typeof hit.excerpt !== 'undefined' && hit.excerpt ){
				htmlPost += '			<br /><span class="mvn-alg-ls-item-desc">' + hit.excerpt + '</span>';
			}
			htmlPost += '			</a>';
			return htmlPost;
		},
		search: function( request, response ) {
			if( typeof algolia !== 'undefined' ){
				algolia.startQueriesBatch();
				algolia.addQueryInBatch( mvnAlgSettings.indexName, request.term, {
					attributesToRetrieve: ['objectID', 'title', 'permalink', 'excerpt', 'content', 'date', 'featuredImage' , 'category', '_tags'],
					hitsPerPage: mvnAlgSearchVars.postsPerPage
				});
				algolia.sendQueriesBatch(that.searchCallback);
			}
			response(data);

		}
	};
	return self;
})(jQuery);

jQuery(document).ready(function($) {
	mvnAlgoliaPrediction.initialize();
	
	// The autocomplete function is called on the input textbox with id input_element
	$("input[name='" + mvnAlgSearchVars.inputSearchName + "']").each(function(index){
			$(this).autocomplete({
			// minLength is the minimal number of input characters before starting showing
			// the autocomplete
			minLength: 1,
			source: mvnAlgoliaPrediction.search,
			// This function is executed when a suggestion is selected
			select: function(event, ui) {
			  // Sets the text of the input textbox to the title of the object referenced
			  // by the selected list item
			  $(this).val(ui.item.label);
			  return false;
			},
			// This function is executed when the users focuses on a item with the mouse
			// or keyboard
			focus: function(event, ui) {
			  // Sets the text of the input textbox to the title of the object referenced
			  // by the focused list item
			  $(this).val(ui.item.label);
			  return false;
			}
		  // Here we alter the standard behavior when rendering items in the list
		  }).data("ui-autocomplete")._renderItem = function(ul, item) {
			// ul is the unordered suggestion list
			// item is a object in the data object that was send to the response function
			// after the JSON request
			// We append a custom formatted list item to the suggestion list
			return $("<li></li>").data("item.autocomplete", item).append(mvnAlgoliaPrediction.getDisplayPost(item)).appendTo(ul);
		  };
	});
});
@@ -64,24 +64,8 @@
 at;%0A
-%09var data = %7B%7D;%0A
 %09var
@@ -74,16 +74,16 @@
 elf = %7B%0A
+
 %09%09initia
@@ -381,16 +381,26 @@
  content
+, response
 ) %7B%0A%09%09%09i
@@ -441,19 +441,124 @@
 gth %3E 0 
-) %7B
+&& that.lastQuery === content.results%5B0%5D.query ) %7B // do not consider outdated answers%0A%09      var data = %5B%5D;
 %0A%09%09%09%09var
@@ -1123,16 +1123,40 @@
 %7D%0A%09%09%09%09%7D%0A
+        response(data);%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2313,54 +2313,232 @@
 tch(
-that.searchCallback);%0A%09%09%09%7D%0A%09%09%09response(data);%0A
+function(success, content) %7B%0A%09%09%09%09%09// forward 'response' to Algolia's callback in order to call it with up-to-date results%0A%09%09%09%09%09that.lastQuery = request.term;%0A%09%09%09%09%09that.searchCallback(success, content, response);%0A%09%09%09%09%7D);%0A%09%09%09%7D
 %0A%09%09%7D

Call autocomplete-ui's reponse callback from Algolia's callback to avoid outdated results.