(function($){
 
   $.fn.searchbloxAutoComplete = function(options){
      return this.each(function(){
		instantSearch = false;
		autoComplete = true;
		callback = 0;
		var SearchmouseOver = false;
		t = setTimeout(function() {}, 10);
		
		$('#seachAutoComplete').html('<ul id="suggestionsHolder"></ul>');
		 
		 /* Create the listener for the search button */
		 $('#searchButton').click(function(){
		 	document.location.href=options.searchDisplayURL+'?query='+$('#query').val();
		 });
		 
		 /*
		  * Create a listner for losing focus - depending on focus is lost to a click on item from autocomplete or something else
		  */
		$('#seachAutoComplete').mouseover(function() { SearchmouseOver = true; }).mouseout(function() { SearchmouseOver = false; });
		
		$(this).blur(function() {
			if (SearchmouseOver) {
				document.location.href=options.searchDisplayURL+'?query='+$('#query').val();
			}
			$('#suggestionsHolder').css('display', 'none');
		});


		 $(this).focus(function() {
		 	if ( $('#query').val().length > 0 ) {
				$('#suggestionsHolder').css('display', 'block');
			}
		 });
		 
		 /*
		  * 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);
				 $('#query').blur();
			 	 document.location.href=options.searchDisplayURL+'?query='+$('#query').val();
			 }
			 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
					 $("#query").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
					 $("#query").val($("li.suggestion a.selected").text());
				 }
			 }
			 else if(searchQuery === '')
			 {
				 $('#suggestionsHolder').html('');
			 }
			 else
			 {
				 //if auto complete is on
				 if(autoComplete)
				 {
					//autocomplete ajax
					$.ajax({
						  type: "GET",
						  url: options.autoCompleteURL,
						  data: encodeURI("col="+options.searchCol+"&q="+searchQuery),
						  dataType: 'jsonp',
						  success: function(data) {
							$('#suggestionsHolder').html('');
							if(data.length > 0)
							{
								$.each(data, function(i){
									 $("#suggestionsHolder").append('<li class="suggestion" id="suggestion'+i+'"><a class="suggestionAnchor">'+this+'</a></li>');
								});
								
								$("#suggestionsHolder").css('display', 'block');
								
								$(".suggestionAnchor").hover(function() {
									$('#query').val($(this).text());
									$("suggestionAnchor").removeClass('selected');				
								});
								
							}
						  }
					 }); // end autocomplete ajax
				 }
				 
			 }
		 }); // end keyup
		 
      });
   };
	
})(jQuery);


$(document).ready(function() {
  var options = {
    // The url that the autocomplete array comes from. 
    // Note that you leave the query varaible's value out of the url, this is added on by the plugin
    autoCompleteURL: 'http://87.116.46.148:8080/searchblox/servlet/AutoSuggest',
    // The url that the updated search results come from
    // Note that you leave the query varaible's value out of the url, this is added on by the plugin
    searchResultsURL: 'http://87.116.46.148:8080/searchblox/servlet/SearchServlet',
    searchDisplayURL: 'http://www.provector.dk/soegning.aspx',
    searchCol: 2,
    searchResultsDivID: 'searchResults'
  };
  
  $('#query').searchbloxAutoComplete(options);

});

