(function($) {
	
									
								
	$.fn.searchbloxAutoComplete = function(options) {
		return this.each(function() {
			callback = 0;
			t = setTimeout(function() {
			}, 10);
			$this = $(this);
			
			options.suggestionsDiv.data("input", $this);
			$(options.suggestionsDiv).html('<ul class="suggestionsHolder"></ul>');

			/*
			 * Create a listener for losing focus
			 */
			$(this).blur(function() {
				$(options.suggestionsDiv).find(".suggestionsHolder").css('display', 'none');
			});
			
			/*
			 * The key press action listener for the search box
			 */
			$(this).keyup(function(e) {
				callback = 0;
				// get the value
				var searchQuery = $(this).val();
				var code = (e.keyCode ? e.keyCode : e.which);
				var listitem = '';
				if (code === 13) {
					// then return is pressed, so do the search
					clearTimeout(t);
					$this.blur();
					$this.submit();
				} else if (code === 40) {
					// then the down arrow is pressed
					// check if any of the autocompletes are selected
					if ($("li.suggestion a.selected").length === 0) {
						// add it to the first one
						$("li.suggestion:first a").addClass('selected');
					} else {
						// move it down
						listitem = $("li.suggestion a.selected");
						listitem.parent().next().find('a').addClass('selected');
						listitem.removeClass('selected');
					}

					if ($("li.suggestion a.selected").text().length > 0) {
						// then update the query
						$this.val($("li.suggestion a.selected").text());
					}
				} else if (code === 38) {
					// then the up arrow is pressed
					if ($("li.suggestion a.selected").length === 0) {
						// add it to the last one
						$("li.suggestion:last a").addClass('selected');
					} else {
						// move it down
						listitem = $("li.suggestion a.selected");
						listitem.parent().prev().find('a').addClass('selected');
						listitem.removeClass('selected');
					}

					if ($("li.suggestion a.selected").text().length > 0) {
						// then update the query
						$this.val($("li.suggestion a.selected").text());
					}
				} else if (searchQuery === '') {
					$(options.suggestionsDiv).html('');
				} else {
					// autocomplete ajax
					$.ajax({
						type : "GET",
						url : options.autoCompleteURL,
						data : "q=" + encodeURI(searchQuery) + options.params + options.site,
						dataType : 'jsonp',
						success : function(data) {
							$(options.suggestionsDiv).html('<ul class="suggestionsHolder"></ul>');
							if (data.length > 0) {
								$.each(data, function(i) {
									$(options.suggestionsDiv).find(".suggestionsHolder").append(
											'<li class="suggestion" id="suggestion' + i + '">' +
												'<a class="suggestionAnchor">' + this + '</a>' +
											'</li>');
								});

								$(options.suggestionsDiv).find(".suggestionsHolder").css('display', 'block');

								$(".suggestionAnchor").hover(function() {
									var input = $(this).parent().parent().parent().data('input');
									$(input).val($(this).text());
									$("suggestionAnchor").removeClass('selected');
								});	

								$(".suggestionAnchor").mousedown(function(){
									var form = $(this).closest("form");
									$(form).submit();
								})								
							}
						}
					}); // end autocomplete ajax
				}
			}); // end keyup
		});
	};
})(jQuery);

