// JavaScript Document
var active = -1;
var currentValue = '';
var autoCompleteHref = '';
var lis = '';
var rel = '';

$('#topNavSearch input[name=query]').keyup(function(e)
{
	var str = $(this).val();
	var offset = $(this).offset();
	if(str.length > 2)
	{
		$('#topNavAutoComplete').css('display', 'block');
		if(active == -1)
		{
			currentValue = str;
		}
		lastKeyPressCode = e.keyCode;
		switch(e.keyCode) 
		{
		case 38: // up
			e.preventDefault();
		 	moveSelected(-1);
		break;
		case 40: // down
		 	e.preventDefault();
		 	moveSelected(1);
		break;
		case 9: // tab
		case 13: // return
			selectCurrent();
			if(autoCompleteHref.indexOf('timeshare') != -1)
			{
				location.href = autoCompleteHref;
			}
			else
			{
				setTimeout("$(this).parents('form').submit()", 500);
			}
		 break;
		default:
			active = -1;
			$.ajax({
			      url: root+"functions/ajax.php?f=getAutoComplete&str="+str,
			      global: false,
			      type: "POST",
			      dataType: "html",
			      async:true,
			      success: function(data) {
			    	    $('#topNavAutoComplete').html(data);
			    	    $('#topNavAutoComplete').addClass('active');
			    	    $('#topNavAutoComplete').css({left: offset.left, top: (offset.top + 23)});
			    	    lis = $("li", "#topNavAutoComplete");
						//add the hover event
			    	    hoverSelected();
					}
				}
			);			
		break;
		}
	}
	else
	{
		$('#topNavAutoComplete').html('');
		$('#topNavAutoComplete').removeClass('active');
	}	
});
		
function moveSelected(step)
{
	active += step;

	if (!lis) 
	{
		return;
	} 
	if(active < 0)
	{
		active = lis.size();
	}
	if (active >= lis.size()) {
		active = -1;
	}
	lis.removeClass("ac_over");

	$(lis[active]).addClass("ac_over");
	rel = $(lis[active]).attr('rel');	
	selectCurrent();		
}

function hoverSelected()
{	
	if (!lis) 
	{
		return;
	}

	$('body').click(function()
	{
		//$('#topNavAutoComplete').html('');
		$('#topNavAutoComplete').removeClass('active');
		$('#topNavAutoComplete').css('display', 'none');
		$('input[name=query]').focus();
		$('input[name=qf]').val('');
		$('body').unbind('click');
	});
	
	lis.each(function(el){ 
		$(this).hover(
			function()
			{
				lis.removeClass("ac_over");
				active = el;
				
				$(this).addClass("ac_over");
				rel = $(lis[active]).attr('rel');	

				$(this).click( function()
					{
						selectCurrent();
						$(this).parents('form').submit();
					} 
				);				
			},
			function()
			{
				$(this).removeClass("ac_over");
			}
		); 
	}); 	
}

function selectCurrent()
{
	if(active > -1 && active <= lis.size())
	{
		$('input[name=query]').val('');
		var selected = $(lis[active]).children('a:first').text();
		autoCompleteHref = $(lis[active]).children('a:first').attr('href');
		rel = $(lis[active]).attr('rel');	
		$('input[name=query]').val(selected.replace( ' (Region)', ''));
		//$('#exactMatch').attr('checked', true);
		return true;
	} else if(active == -1 || active > lis.size())
	{
		$('input[name=query]').val('');		
		$('input[name=query]').val(currentValue);
		return false;
	}
}
