if(typeof(Prototype) != 'undefined')
{
	var selectedIndex = -1;
	var linescount;
	
	document.observe('dom:loaded', function()
	{
		var container = new Element('div', { id : 'suggestions' });
		container.hide();
		$('inputSearch').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();
		});

		$('filterProductSex').observe('change', function(e)
		{
			$('filterForm').submit();
		});
				
		$('filterPriceRange').observe('change', function(e)
		{
			$('filterForm').submit();
		});
				
		$('filterCategory').observe('change', function(e)
		{
			$('filterForm').submit();
		});

		$('filterBrand').observe('change', function(e)
		{
			$('filterForm').submit();
		});

		if($('filterName'))
		{
			$('filterName').observe('change', function(e)
			{
				$('filterForm').submit();
			});
		}
		
		$('searchFormSubmit').observe('click', function()
		{
			if(!$('suggestions').visible()) 
			{ 
				$('searchForm').submit(); 
			}
		});

		$('inputSearch').observe('keyup', function(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?q=' + escape($('inputSearch').value), 
				{
					method : 'get',
					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();
						}
	
						$('suggestions').childElements()[selectedIndex].addClassName('selected');
					}
				});
			}
			
			if(selectedIndex >= 0)
			{
				$('suggestions').childElements()[selectedIndex].addClassName('selected');
			}
		});
	});

	document.observe('click', function()
	{
		$('suggestions').hide();
	});
	
}