// JavaScript Document
/************************
Function called when wine
result is selected
************************/
function wine_selected(id)
{
	id.style.backgroundColor="#AAAAAA";
	document.body.style.cursor="pointer";
}

/**************************
Function called when wine is
not selected anymore
***************************/
function wine_unselected(id)
{
	id.style.backgroundColor="#FFFFFF";
	document.body.style.cursor="auto";
}

/*************************
Function called when wine
results are to be displayed
**************************/
function showresults(srch)
{
	url="../wine-categories/search.php?search=" + srch;
	window.location = url;
}

/*************************
Function called when wine
page is to be displayed
**************************/
function showwine(id)
{
	url="../wine-categories/wine-detail.php?wine=" + id + "";
	window.location = url;
}

/**********************
Function to hide search
**********************/
function hide_search(srch)
{
	search_box.style.visibility = "Hidden";
	if (srch == null)
		document.getElementById('search').value="Enter search";
	else
		document.getElementById('search').value=srch;
}

/*************************
Function to handle arrow
keys pressed during search
*************************/
function handle_special_keys(k, e)
{
	//Up key pressed
	if (k == 38)
	{
		if (pos != 0)
		{
			id=document.getElementById("wine_" + pos);
			id.style.backgroundColor="#FFFFFF";
			pos--;
			if (pos != 0)
			{
				id=document.getElementById("wine_" + pos);
				id.style.backgroundColor="#AAAAAA";	
			}
		}
	}
	
	//Down key pressed
	else if (k == 40)
	{
		
		if (pos != 0)
		{
			id=document.getElementById("wine_" + pos);
			id.style.backgroundColor="#FFFFFF";
		}
		
		pos++;
		max_wines=document.getElementById('wines_count').value;
		if (pos > max_wines)
			pos = max_wines;
			
		id=document.getElementById("wine_" + pos);
		id.style.backgroundColor="#AAAAAA";
	}
	
	//Enter key pressed
	else if (k == 13)
	{
		
		srch=document.getElementById('search').value;
		
		//Arrows not yet used for navigation
		if (pos == 0)
		{	
			showresults(srch);
		}
		
		//Arrows have been used for navigation
		else
		{
			wines_arr=document.getElementById('top_wines_array').value;
			wine_pos=0;
			wine_id=0;
			
			while (pos != wine_pos)
			{
				i=0;
				while (wines_arr.charAt(i) != '-')
				{
					i++;
				}
				wine_pos=wines_arr.substr(0,i);
				wines_arr=wines_arr.substr(i+1,wines_arr.length-i);
				i=0;
				while (wines_arr.charAt(i) != ':')
					i++;
				wine_id=wines_arr.substr(0,i);
				wines_arr=wines_arr.substr(i+1,wines_arr.length-i);
			}
			url="../wine-categories/wine-detail.php?wine=" + wine_id + "";
			window.location=url;
		}
	}
	
	//Escape key pressed
	else if (k == 27)
	{
		hide_search("");
	}
}