function Suggestion()
{
	this.contentLength = 0;
	this.selectedId = -1;
	this.suggestDivId = 'suggest';
	this.searchBoxId = 'search';
	this.formName = 'search';
	
	this.query = '/suggest.php?query=';
	
	this.highlightColor = '#F5F5F5';
	this.bgColor = '#FFFFFF';
	this.minLength = 1;
}

Suggestion.prototype.suggest = function(event, _this)
{
	_this = _this || this;
	var searchBox = $(_this.searchBoxId);
	var suggestDiv = $(_this.suggestDivId);
	var v = searchBox.value;
	
	// TODO: make it on prptotype ?
	var k = GeoKBD.event.getKeyCode(event);
	
	if(k == Event.KEY_RETURN)
	{
		return;
	}

	if(v.length > _this.minLength)
	{
		if(_this.contentLength != v.length && k != Event.KEY_DOWN && k != Event.KEY_UP)
		{
			var a = new Ajax.Updater(suggestDiv, _this.query + encodeURIComponent(v),
			{
				method: 'get',
				evalScripts: true,
				onComplete: function(originalRequest)
				{
					if(originalRequest.responseText != '')
					{
						_this.selectedId = -1;
						suggestDiv.style.display = 'block';
					}
				}
			});
		}
	}
	else
	{
		suggestDiv.style.display = 'none';
	}

	if(suggestDiv.style.display == 'block')
	{
		if(k == Event.KEY_DOWN)
		{
			// arrow down
			_this.walkChildNodes(suggestDiv, 'down', _this);
		}
		else if(k == Event.KEY_UP)
		{
			// arrow up
			_this.walkChildNodes(suggestDiv, 'up', _this);
		}
	}
	
	_this.contentLength = v.length;
}

Suggestion.prototype.applySuggest = function(text, _this)
{
	_this = _this || this;
	$(_this.searchBoxId).value = text;
	document.forms[_this.formName].submit();
	return false;
}

Suggestion.prototype.hideSuggest = function(_this)
{
	_this = _this || this;
	try
	{
		$(_this.suggestDivId).style.display = 'none';
		$(_this.suggestDivId).innerHTML = '';
	}
	catch(e){}
}

Suggestion.prototype.highlight = function(obj, isHighlighted, _this)
{
	_this = _this || this;
	if(isHighlighted)
	{
		obj.style.backgroundColor = _this.highlightColor;
	}
	else
	{
		obj.style.backgroundColor = _this.bgColor;
	}
}

Suggestion.prototype.unselectCurrent = function(_this)
{
	_this = _this || this;
	try
	{
		_this.highlight($(_this.suggestDivId).childNodes[_this.selectedId], false);
	}
	catch(e){}
	_this.selectedId = -1;
}

Suggestion.prototype.walkChildNodes = function(obj, direction, _this)
{
	_this = _this || this;
	
	var i, group, gsize;
	
	group = obj.childNodes;
	
	gsize = group.length;
	
	if(direction == 'down')
	{
		_this.selectedId = (_this.selectedId < gsize - 1) ? _this.selectedId + 1 : 0;
		idx = (this.selectedId == 0) ? (gsize - 1) : (this.selectedId - 1);
	}
	else
	{
		_this.selectedId = (_this.selectedId > 0) ? (_this.selectedId - 1) : (gsize - 1);
		idx = (this.selectedId == gsize - 1) ? 0 : (this.selectedId + 1);
	}
	
	var j = 0;
	for(i = 0; i < gsize; i++)
	{
		if(group[i].nodeType == 1)
		{
			if(group[i].tagName == 'DIV')
			{
				if(j == _this.selectedId)
				{
					_this.highlight(group[i], true, _this);
					$(_this.searchBoxId).value = group[i].attributes['qword'].value;
					// this.highlight(group[idx], false);
					// break;
				}
				else
				{
					_this.highlight(group[i], false, _this);
				}
				j++;
			}
		}
	}
}

Suggestion.prototype.init = function()
{
	var _this = this;
	Event.observe(_this.searchBoxId, 'keyup', function(event){_this.suggest(event, _this);});
	Event.observe(document.body, 'click', function(event){_this.hideSuggest(_this);});
}




