function Suggester()
{
	this.Initialize = function(Options)
	{
		this.Options =
		{
			TargetElement: '',
			DataSource: '',
			LettersToSuggestion: 2,
			Suggestions: 10,
			DivClass: '',
			ListItemClass: '',
			ListItemHover: '',
			ListItemCategory: '',
			Timeout: 200,
			GameId: 0
		}

		Object.extend(this.Options, Options || {});

		if (!$(this.Options.TargetElement))
		{
			return;
		}

		this.ObserverId = Event.observe($(this.Options.TargetElement), 'keydown', this.CheckStatus.bindAsEventListener(this), false);

		this.DivId = 'suggesterdiv';

		this.SuggestTimeout = 0;

		if (!$(this.DivId))
		{
			var Div = document.createElement('div');

			Div.style.position = 'absolute';
			Div.id = this.DivId;
			Div.style.display = 'none';

			Div.className = this.Options.DivClass;

			document.body.appendChild(Div);
		}
	}

	this.CheckStatus = function(evt)
	{
		KeyHit = false;
		switch (evt.keyCode)
		{
			case 40:
			//key down

				this.SelectNextElement();
				return;

			break;

			case 38:
			//key up

				this.SelectPreviousElement();
				return;

			break;

			case 13:
				this.EnterHandler();
				evt.cancelBubble = true;
				if(evt.stopPropagation) evt.stopPropagation();
				evt.stop();
				return;

			break;

			case 27:
			//escape key
				this.HidePane();
				return;

			break;
			case 37:
			//escape key
				return;

			break;
			case 39:
			//escape key
				return;

			break;
		}
		
//37/39 -left/rigth
		if (evt.keyCode == 8) //backspace
		{
			if ($(this.Options.TargetElement).value.replace(/\?/g, '').length >= this.Options.LettersToSuggestion + 1)
			{/*
				if (!this.SuggestTimeout)
				{
					this.SuggestTimeout = setTimeout(this.RequestData.bindAsEventListener(this), this.Options.Timeout);
				}*/
				
				clearTimeout(this.SuggestTimeout);
				this.SuggestTimeout = setTimeout(this.RequestData.bindAsEventListener(this), this.Options.Timeout);
			}
			else
			{
				this.HidePane();
			}
		}
		else
		{
			if ($(this.Options.TargetElement).value.replace(/\?/g, '').length >= this.Options.LettersToSuggestion - 1)
			{/*
				if (!this.SuggestTimeout)
				{
					this.SuggestTimeout = setTimeout(this.RequestData.bindAsEventListener(this), this.Options.Timeout);
				}*/
				
				clearTimeout(this.SuggestTimeout);
				this.SuggestTimeout = setTimeout(this.RequestData.bindAsEventListener(this), this.Options.Timeout);
			}
			else
			{
				this.HidePane();
			}
		}
	}

	this.ShowPane = function()
	{
		var Coordinates = Position.cumulativeOffset($(this.Options.TargetElement));

		var TargetSize = Element.getDimensions($(this.Options.TargetElement))

		$(this.DivId).style.left = Coordinates[0] + 'px';
		$(this.DivId).style.top = Coordinates[1] + TargetSize.height + 'px';

		$(this.DivId).style.width = TargetSize.width + 'px';

		if ($(this.DivId))
		{
			$(this.DivId).style.display = 'block';
		}

		this.ClickObserver = Event.observe(document, 'click', this.ClickHandler.bindAsEventListener(this), false);
	}

	this.HidePane = function()
	{
		clearTimeout(this.SuggestTimeout);
		
		Event.stopObserving(document, 'click', this.ClickObserver);

		if ($(this.DivId))
		{
			$(this.DivId).style.display = 'none';
		}

		this.ClearSuggestions();
	}

	this.RequestData = function()
	{
		this.SuggestTimeout = 0;

		var Keywords = $(this.Options.TargetElement).value.replace(/\?/g, '');

		var MyAjax = new Ajax.Request
		(
			this.Options.DataSource,
			{
				method: 'post',
				parameters: 'keyword=' + Keywords +
							'&limit=' + this.Options.Suggestions +
							'&game=' + this.Options.GameId,
				onSuccess: this.Populate.bindAsEventListener('', this),
				onFailure: this.HandleError.bindAsEventListener()
			}
		);
	}

	this.Populate = function(Response, Object)
	{
		var Result = Response.responseText.evalJSON();

		var SuggestionsLengthPlaygrounds 	= Result.PlaygroundsSuggestions.length;

		Object.ClearSuggestions();

		if ( (SuggestionsLengthPlaygrounds > 0))
		{
			var ServerText		= document.createElement('span');

			var ListContainerPlaygrounds = document.createElement('ul');
			for (i = 0;i < SuggestionsLengthPlaygrounds ;i++)
			{
				var Item = document.createElement('li');

				Item.className = Object.Options.ListItemClass;

				Item.onmouseover = Object.MouseOverItem.bindAsEventListener('', Object);

				Item.onclick = Object.ItemSelected.bindAsEventListener('', Object);
				
				Item.id = 'Server_' + Result.PlaygroundsSuggestions[i].Id;

				var Text = document.createTextNode(Result.PlaygroundsSuggestions[i].Name);

				Item.appendChild(Text);

				$(ListContainerPlaygrounds).appendChild(Item);
			}

			//TdPlaygrounds.appendChild(ListContainerPlaygrounds);
			ServerText.innerHTML = 'Servers';
			ServerText.className = Object.Options.ListItemCategory;
			$(Object.DivId).appendChild(ListContainerPlaygrounds);

			Object.ShowPane();
		}
		else
		{
			this.HidePane();
		}
	}

	this.ClickHandler = function()
	{
		this.HidePane();
	}

	this.EnterHandler = function()
	{
		var UlItems = $(this.DivId).getElementsByTagName('UL');
		var UlItemsLength = UlItems.length;
//alert(UlItemsLength)
		if (UlItemsLength > 0)
		{
			for (i = 0; i < UlItemsLength; i++)
			{
				ListItems = UlItems[i].getElementsByTagName('LI');
				//alert(ListItems)
				if (ListItems.length > 0)
				{
					for (k=0; k < ListItems.length; k++)
					{
						if (ListItems[k].className == this.Options.ListItemHover)
						{
							$(this.Options.TargetElement).value = ListItems[k].innerHTML;
							LoadPrice(1, 0);
							/*if (ListItems[k].id.search('Server_') == 0)
							{
								window.location = 'BuyCurrency.php?PlaygroundId=' + ListItems[k].id.replace('Server_', '');
							}
							else
							{
								window.location = 'BuyServers.php?GameId=' + ListItems[k].id.replace('Game_', '');
							}*/
						}
					}
				}	
			}
		}
		
		this.HidePane();
	}

	this.ItemSelected = function(evt, Object)
	{
		$(Object.Options.TargetElement).value = Event.element(evt).innerHTML;
		$(Object.Options.TargetElement).focus();
		LoadPrice(1, 0);
		
		/*if (Event.element(evt).id.search('Server_') == 0)
		{
			window.location = 'BuyCurrency.php?PlaygroundId=' + Event.element(evt).id.replace('Server_', '');
		}
		else
		{
			//alert(Event.element(evt).id.replace('Game_', ''));
			window.location = 'BuyServers.php?GameId=' + Event.element(evt).id.replace('Game_', '');
		}*/

		Object.HidePane();
	}

	this.SelectNextElement = function()
	{
		
		var ListItems = $(this.DivId).getElementsByTagName('li');
		var ListItemsLength = ListItems.length;

		if (ListItemsLength > 0)
		{
			
			for (i = 0; i < ListItemsLength; i++)
			{
				if ((ListItems[i].className == this.Options.ListItemHover) && ((i + 1) < ListItemsLength))
				{
					var Target = ListItems[i+1];
				}

				ListItems[i].className = this.Options.ListItemClass;
			}
			
			if (Target)
			{
				Target.className = this.Options.ListItemHover;
			}
			else
			{
				ListItems[0].className = this.Options.ListItemHover;
			}
			
			
		}
	}

	this.SelectPreviousElement = function()
	{
		var ListItems = $(this.DivId).getElementsByTagName('li');
		var ListItemsLength = ListItems.length;

		if (ListItemsLength > 0)
		{

			for (i = 0; i < ListItemsLength; i++)
			{
				if ((ListItems[i].className == this.Options.ListItemHover) && ((i-1) >= 0))
				{
					var Target = ListItems[i-1];
				}

				ListItems[i].className = this.Options.ListItemClass;
			}
			
			if (Target)
			{
				Target.className = this.Options.ListItemHover;
			}
			else
			{
				ListItems[ListItemsLength - 1].className = this.Options.ListItemHover;
			}
		}
	}

	this.MouseOverItem = function(evt, Object)
	{
		var ListItems = $(Object.DivId).getElementsByTagName('li');
		var ListItemsLength = ListItems.length;

		if (ListItemsLength > 0)
		{
			for (i = 0; i < ListItemsLength; i++)
			{
				ListItems[i].className = Object.Options.ListItemClass;
			}
		}

		Event.element(evt).className = Object.Options.ListItemHover;
	}

	this.ClearSuggestions = function()
	{		
		var ObjChilds = $(this.DivId).childNodes;
		var ChildsLength = ObjChilds.length;
		//alert(ObjChilds[3]);
		for (i=0; i < ChildsLength;i++)
		{
			$(this.DivId).removeChild($(this.DivId).firstChild);
		}
	}

	this.HandleError = function()
	{
		this.HidePane();
	}
}

