(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();

});

//---------------------------------------------------------
//
// Property Searching
//
//---------------------------------------------------------
/* 
function suggest(text, e)
{ 
	textlength = text.length;
	var key = "";
	
	if(e==null)
	{
		e = window.event;
	}
	
	if(e.keyCode)
	{
		key = e.keyCode;	// Other
	}
	else
	{
		key = String.fromCharCode(e.which);	// IE
	}
	
	if((key > 48 && key < 90) || key == 8)
	{
		xmlHttp=GetXmlHttpObject()
		
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return
		}
		
		if(text == "" || text.length < 2)
		{
			document.getElementById("search_suggest").style.display = "none";
			document.getElementById("search_suggest").innerHTML = "";
			idcount = 0;
		}
		else
		{
			var url="/tabs_generic/includes/suggest/suggest.php";
			url = url + "?text=" + text;
			url = url + "&rand=" + Math.floor(Math.random()*10000000);
			
			xmlHttp.onreadystatechange=stateSuggest;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
	}
}

function stateSuggest() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var responsearray = xmlHttp.responseText.split("|");
		if(responsearray[0] == "")
		{
			document.getElementById("search_suggest").style.display = "none";
		}
		else
		{
			document.getElementById("search_suggest").innerHTML = responsearray[0];
			totalitems = parseInt(responsearray[1]);
			document.getElementById("search_suggest").style.display = "block";
		}
	} 
}

function SetValue(text)
{
	text = text.replace("<strong>", "");
	text = text.replace("</strong>", "");
	document.getElementById("suggestbox").value = text;
}

function GoTo(value)
{
	window.location = value;
}

function listener(e, text)
{
	var key = "";
	highlightSuggestDiv(idcount, "#FFFFFF");
	if(e==null)
	{
		e = window.event;
	}
	
	if(e.keyCode)
	{
		key = e.keyCode;	// Other
	}
	else
	{
		key = String.fromCharCode(e.which);	// IE
	}
	
	try
	{
		if(key == "40")
		{
			idcount = (idcount < totalitems) ? (idcount+1) : 1;
			highlightSuggestDiv(idcount, "#CCCCCC");
			SetValue(document.getElementById("suggest_item_" + idcount).getAttribute('propertyname'));
		}
		else if(key == "38")
		{
			idcount = (idcount <= 1) ? totalitems : idcount-1;
			highlightSuggestDiv(idcount, "#CCCCCC");
			SetValue(document.getElementById("suggest_item_" + idcount).getAttribute('propertyname'));
		}
		else if(key == "13")
		{
			SetValue(document.getElementById("suggest_item_" + idcount).getAttribute('propertyname'));
		}
	}
	catch(e)
	{
	
	}
	
}

function highlightSuggestDiv(id, colour)
{
	try
	{
		var itemname = "suggest_item_"+id;
		document.getElementById("suggest_item_" + id).style.backgroundColor = colour;
	}
	catch(e)
	{
		//alert(e);
	}
} */