if(typeof(Prototype) != 'undefined')
{
	var selectedIndex = -1;
	var linescount;
	var textSearchTimer;
	
	document.observe('dom:loaded', function()
	{
		var container = new Element('div', { id : 'suggestions' });
		container.hide();
		$('searchFormSubmit').insert( { after: container } );

		$('inputSearch').setAttribute("autocomplete", "off");

		var originalSearchText = $('inputSearch').value;
		
		$('inputSearch').observe("focus", function(e)
		{
			if($('inputSearch').value == originalSearchText)
			{
				$('inputSearch').value = '';
			}
		});

		$('inputSearch').observe("blur", function(e)
		{
			if($('inputSearch').value == '')
			{
				$('inputSearch').value = originalSearchText;
			}
		});

		$('searchForm').observe('submit', function(e)
		{
			e.stop();
		});

		$('searchFormSubmit').observe('click', function()
		{
			if(!$('suggestions').visible()) 
			{ 
				$('searchForm').submit(); 
			}
		});

		$('inputSearch').observe('keyup', function(e)
		{
			if(textSearchTimer)
			{
				clearTimeout(textSearchTimer);
			}
			
			textSearchTimer = textSearch.delay(0.75, e);
		});
	});

	if($('suggestions'))
	{
		document.observe('click', function()
		{
			$('suggestions').hide();
		});
	}
}

function textSearch(e)
{
	if(e.keyCode == Event.KEY_ESC)
	{
		$('suggestions').hide();
		selectedIndex = -1;
	}
	else if(e.keyCode == Event.KEY_DOWN) 
	{
		if(selectedIndex >= 0)
		{
			$('suggestions').childElements()[selectedIndex].removeClassName('selected');
		}
		
		++selectedIndex;
		
		selectedIndex %= linescount;
	}
	else if(e.keyCode == Event.KEY_UP)
	{
		if(selectedIndex >= 0)
		{
			$('suggestions').childElements()[selectedIndex].removeClassName('selected');
		}
		else
		{
			selectedIndex = 0;
		}
		
		--selectedIndex;
		selectedIndex += linescount;
		selectedIndex %= linescount;
	}
	else if(e.keyCode == Event.KEY_RETURN)
	{
		if(selectedIndex != -1)
		{
			document.location = '/viewProduct.php?id=' + $('suggestions').childElements()[selectedIndex].firstChild.id.replace('suggestedItem', '');
			$('inputSearch').value = $('suggestions').childElements()[selectedIndex].getInnerText();
			$('suggestions').hide();
			e.stop();
			selectedIndex = -1;
		}
		else
		{
			$('searchForm').submit();
		}
	}
	else
	{		
		selectedIndex = -1;
		
		new Ajax.Request('/ajax/suggest.php', 
		{
			method : 'post',
			
			parameters: 
			{ 
				q: $('inputSearch').value
			},
		
			onSuccess: function(transport)
			{
				var response = transport.responseText;
				
				if(response.length > 0)
				{
					$('suggestions').show();
					$('suggestions').update();
					
					var lines = response.split('\n');
					var maxlen = 0;
					
					linescount = lines.length;
					
					for(i = 0; i < linescount; ++i)
					{
						suggestedItemId = lines[i].substr(0, lines[i].indexOf('=')); 
						suggestedItemText = unescape(decodeURIComponent(lines[i].substr(lines[i].indexOf('=') + 1)));
						newitem = new Element('span', { id: 'suggestedItem' + suggestedItemId });
													
						newitem.observe('mouseover', function(event)
						{
							if(selectedIndex != -1)
							{
								$('suggestions').childElements()[selectedIndex].removeClassName('selected');
							}
							
							var obj = event.element();
							
							while(obj.tagName != 'DIV')
							{
								obj = obj.parentNode;	
							}
							
							selectedIndex = obj.previousSiblings().length;
							$('suggestions').childElements()[selectedIndex].addClassName('selected');
						});

						newitem.update(suggestedItemText);
						
						$('suggestions').insert(new Element('div').update(newitem));
	
						maxlen = Math.max(maxlen, newitem.getWidth());

						newitem.observe('click', function(event)
						{
							document.location = '/viewProduct.php?id=' + $('suggestions').childElements()[selectedIndex].firstChild.id.replace('suggestedItem', '');
							$('inputSearch').value = this.getInnerText();
							$('suggestions').hide();
							selectedIndex = -1;
						});
						
					}
					
					maxlen = Math.min(maxlen, 500);
					
					for(i = 0; i < $('suggestions').childNodes.length; ++i)
					{
						$('suggestions').childNodes[i].style.width = maxlen + 'px';
					}
					
					$('suggestions').style.width = maxlen + 'px';
					$('suggestions').style.left = $('inputSearch').cumulativeOffset().left + $('inputSearch').getWidth() - $('suggestions').getWidth() + 'px';
					$('suggestions').style.top = $('inputSearch').cumulativeOffset().top + $('inputSearch').getHeight() - 1 + 'px';
				}
				else
				{
					$('suggestions').hide();
				}

				if(selectedIndex != -1 && $('suggestions').childElements().length > 0)
				{
					$('suggestions').childElements()[selectedIndex].addClassName('selected');
				}
			}
		});
	}
	
	if(selectedIndex >= 0)
	{
		$('suggestions').childElements()[selectedIndex].addClassName('selected');
	}
}

