if(typeof(Prototype) != 'undefined')
{
	Element.addMethods(
	{  
		getInnerText : function(element) 
		{
			element = $(element);
			return element.innerText && !window.opera ? element.innerText : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
		}
	});

	
	var selectedIndex = -1;
	var linescount;
	
	
	document.observe('dom:loaded', function()
	{
		var container = new Element('div', { id : 'suggestions' });
		container.hide();
		$('suggestionscontainer').update(container);
	
		$('inputsearch').setAttribute("autocomplete", "off");
	
		$('searchForm').observe('submit', function(e)
		{
			e.stop();
		});
	
		$('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)
				{
					$('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('|');
							var maxlen = 0;
							
							linescount = lines.length;
							
							for(i = 0; i < linescount; ++i)
							{
								newitem = new Element('span');
								
								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(unescape(decodeURIComponent(lines[i])));
								
								$('suggestions').insert(new Element('div').update(newitem));
			
								maxlen = Math.max(maxlen, newitem.getWidth());
	
								newitem.observe('click', function(event)
								{
									$('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();
	});
	
}