(function($){

	// Applies a change select to the select of the class
	$.fn.suggest = function(options) {
	
		var defaults = { target: "#search_suggest", suggestitem: "div.suggestitem" , suggestbox: "#suggestbox" }
		var options = $.extend(defaults, options);
		var globalPosition = -1;
		$input = $(this);
		
		$input.each(function() {
			// Check for the value of the input element, only search for 2 characters or more
			$input.keyup(processKey);
			
			$input.closest("form").bind("keypress", function(e) {
				if(e.keyCode) {
					key = e.keyCode; // Normal browsers
				}
				else {
					key = String.fromCharCode(e.which);	// IE
				}
				if(key == 13) {
					SubmitForm(e);
					return false;
				}
			});
		});
		
		
		function processKey(e) {
			if($input.val().length > 2) {
				// Test for keycodes
				if(e.keyCode) {
					key = e.keyCode; // Normal browsers
				}
				else {
					key = String.fromCharCode(e.which);	// IE
				}
				
				if(key == 40) { // Down
					highlightSuggestItem(e, "down");
				}
				else if(key == 38) { // Up
					highlightSuggestItem(e, "up");
				}
				else if(key == 13) { // Enter
					SubmitForm(e);
					return false;
				}
				else {
					suggestitem(e);
				}
			}
			else {
				if(key == 13) {
					SubmitForm(e);
					return false;
				}
				else {
					clearSuggest(e);
				}
			}
		}
		
		function SubmitForm(e) {
			if(globalPosition > -1) {
				window.location = "/cottage-details/" + $(options.target + " " + options.suggestitem).eq(globalPosition).attr("propertyref");
			}
			else {
				$(options.suggestbox).closest("form").submit();
			}
		}
		
		function suggestitem(e) {
			if($input.val().length > 2) {
				$.get("/includes/custom/suggest.php", {text: $input.val()}, function(suggestdata) {
					if(suggestdata != "") {
						// Reset global position
						globalPosition = -1;
						$(options.target).css({'display': 'block'});
						$(options.target).html(suggestdata);
						
						// Add mouse bind
						$(options.target + " " + options.suggestitem).each(function(index) {
							// Bind mouse function
							$(options.target + " " + options.suggestitem).eq(index).bind("mouseover", function() {
								highlightItem(index);
							});
						});
					}
					else {
						clearSuggest(e);
					}
				}); 
			}
			else {
				clearSuggest(e);
			}
		}
		
		function clearSuggest(e) {
			$(options.target).css({'display': 'none'});
			$(options.target).html("");
			globalPosition = -1;
		}
		
		function highlightSuggestItem(e, direction) {
			// Remove css
			$(options.target + " " + options.suggestitem).removeClass("suggestselected");
			
			if(direction == "down") {
				globalPosition++;
			}
			else {
				globalPosition--;
			}
			
			// Reset if out of bounds
			if(globalPosition >= $(options.target + " " + options.suggestitem).length) {
				globalPosition = 0;
			}
			else if(globalPosition < 0) {
				globalPosition = $(options.target + " " + options.suggestitem).length - 1;
			}
			
			// Add css based on index
			$(options.target + " " + options.suggestitem).each(function(index) {
				if(index == globalPosition) {
					$(options.target + " " + options.suggestitem).eq(index).addClass("suggestselected");
				}
			});
		}
		
		// Overide function
		function highlightItem(index) {
			// Remove css
			$(options.target + " " + options.suggestitem).removeClass("suggestselected");
			// Overide global position
			globalPosition = index;
			// Add Class
			$(options.target + " " + options.suggestitem).eq(index).addClass("suggestselected");
		}
	}
	
})(jQuery);


$(document).ready(function(){
	
	$("#suggestbox").suggest();

});