/*-------------------------------------------------------------------------------------------------------
	This plugin will return a json object from the specified url.
-------------------------------------------------------------------------------------------------------*/

(function($){
	$.fn.suggestiveSearch = function(options){

		var orig_val = this.val();
		var settings = $.extend({}, $.fn.suggestiveSearch.defaults, options);

		return this.each(function(){
			$(settings.searchBox).keyup(function(event){
				var searchForm = $(this).parents('form');
				if (event.which == 40) {//if the key pressed is the down arrow
					if($('#search_suggestions li.selected').length == 1){
						$('#search_suggestions li.selected').removeClass('selected').next().addClass('selected');
					} else {
						$('#search_suggestions li:first').addClass('selected');
					}

				} else if (event.which == 38) {//if the key pressed is the up arrow
					if($('#search_suggestions li.selected').length == 1){
						$('#search_suggestions li.selected').removeClass('selected').prev().addClass('selected');
					} else {
						$('#search_suggestions li:last').addClass('selected');
					}

				} else if (event.which == 13) {//if the key pressed is enter
					if($('#search_suggestions li.selected a.showing').length == 1 && settings.clickSubmit==true){
						var selectedResultURL = $('#search_suggestions li.selected a.showing').attr('href');
						window.location = escape(selectedResultURL);
					} else if(this.value != '' && orig_val != this.value) {
						//just submit the search
						searchForm.submit();
					}

				} else {

					var searchTerm = settings.searchBox.val().trim();
					var searchResultsBox = $('#search_suggestions');

					if($('#search_suggestions').length == 0){
						//create the search_results box if we don't already have one
						$(settings.searchBox).parent().append('<ul class="suggs" id="search_suggestions"></ul>');
						$('li.all_results').live('click', function() {
							searchForm.submit();
						})
					}
					
					//Empty the searchResultsBox when there is no search term
					if (searchTerm.length === 0){
						$('#search_suggestions').html('');
					}

					// Load the search results
					if (searchTerm.length > 0) {
						var baseSearchUrl = settings.searchUrl + escape(searchTerm);
						$('#search_suggestions').load(baseSearchUrl + '&rows=' + settings.resultLimit, showSearchSuggestions);
					}


				  	if (settings.callback){
				  		setTimeout(settings.callback,300);
				  	}
				}

				//Prevent the form's default submission with the enter/return key
				$(this).keypress(function(event){ return event.which == 13 ? false : true; });
			});

			//show/hide the search box as needed
			$(settings.searchBox).blur(function(){
				setTimeout(hideSearchSuggestions, 500);
			});
			$(settings.searchBox).focus(function(){
				setTimeout(showSearchSuggestions, 500);
			});
		});

		/*Custom functions-----------------------------------------*/
		//Hides the search dropdown
		function hideSearchSuggestions(){
			$('#search_suggestions').fadeOut('400');
		}
		function showSearchSuggestions(){
			$('#search_suggestions').fadeIn('400');
		}
	}

	//these are the default settings
	$.fn.suggestiveSearch.defaults = {
		searchUrl: '',
		searchBox: '',
		jsonObject:'',
		additionalLinks:false,
		resultLimit:5,
		clickSubmit:true,
		callback:''
	}

})(jQuery);
