if (typeof(Livesearch) == 'undefined') Livesearch = {};

Livesearch = function()
{
	this.highlightActive = false;
	this.highlighted     = 0;
	
	var pointer = this;
	$('livesearch_string').onkeyup      = function(ev) { return pointer.onKeyUp(ev) }
	$('livesearch_string').onkeypress   = function(ev) { return pointer.onKeyPress(ev) }
	$('livesearch_suggestions').onclick = function() { return pointer.onClick() }
	$('livesearch_string').onblur       = function() { window.setTimeout('Livesearch.prototype.onBlur()', 500); }
	$('livesearch_button').onclick      = function() { return pointer.onClick() }
}

Livesearch.prototype.onClick = function()
{
	var list = $$('#livesearch_suggestions li');
	if (!list) return false;

	if (!this.highlightActive)
	{
		redirect('index.php?ota=internalSearchResults&searchstring=' + $('livesearch_string').value);
	}
	else
	{
		var tmp = list[this.highlighted].id.split('_');
		switch (tmp[0])
		{
			case 'user':
				redirect('index.php?ota=internalProfile&profileuser=' + tmp[1]);
			break;
			case 'tournament':
				redirect('index.php?ota=internalTournament&nummer=' + tmp[1]);
			break;
			case 'team':
				redirect('index.php?ota=internalTeamProfile&teamID=' + tmp[1]);
			break;
			case 'country':
				redirect('index.php?ota=internalNation&nid=' + tmp[1]);
			break;
		}
	}
}

Livesearch.prototype.onBlur = function()
{
	$('livesearch_suggestions').hide();
}

Livesearch.prototype.onKeyUp = function(ev)
{
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var list = $$('#livesearch_suggestions li');
	if (!list) return false;
	
	switch (key)
	{
	case 38: // arrow up
		this.changeHighlight(key);
	break;
	case 40: // arrow down
		this.changeHighlight(key);
	break;
	case 27: // escape
		// do not show the suggestions again when ESC was pressed
	break;
	case 13: // enter
		if (this.highlightActive)
		{
			var tmp = list[this.highlighted].id.split('_');
			switch (tmp[0])
			{
				case 'user':
					redirect('index.php?ota=internalProfile&profileuser=' + tmp[1]);
				break;
				case 'tournament':
					redirect('index.php?ota=internalTournament&nummer=' + tmp[1]);
				break;
				case 'team':
					redirect('index.php?ota=internalTeamProfile&teamID=' + tmp[1]);
				break;
				case 'country':
					redirect('index.php?ota=internalNation&nid=' + tmp[1]);
				break;
			}
		}
		else
		{
			redirect('index.php?ota=internalSearchResults&searchstring=' + $('livesearch_string').value);
		}
	break
	default:
		BufferedRequest.request('livesearch_string', 'livesearch');
		this.highlightActive = false;
		this.highlighted = 0;
	break;
	}
}

Livesearch.prototype.onKeyPress = function(ev)
{
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	
	switch (key)
	{
		case 27:
			$('livesearch_suggestions').hide();
		break;
	}
}


Livesearch.prototype.changeHighlight = function(key)
{
	var list = $$('#livesearch_suggestions li');
	if (!list) return false;
	
	var n;
	
	if (key == 40)
	{
		if (this.highlightActive)
		{
			n = this.highlighted + 1;
			if (n > list.length - 1) n = 0;
		}
		else
		{
			n = 0;
		}
	}
	if (key == 38)
	{
		if (this.highlightActive)
		{
			n = this.highlighted - 1;
			if (n < 0) n = list.length - 1;
		}
		else
		{
			n = list.length - 1;
		}
	}
	
	this.setHighlight(n);
}

Livesearch.prototype.setHighlight = function(i)
{
	var list = $$('#livesearch_suggestions li');
	if (!list) return false;
	
	if (this.highlightActive) this.clearHighlight();
	
	this.highlighted = Number(i);
	
	$('livesearch_string').value = list[this.highlighted].lastChild.innerHTML.replace(/<b>/gi, '').replace(/<\/b>/gi, '');

	list[this.highlighted].className = 'highlight';
	this.highlightActive = true;
}

Livesearch.prototype.clearHighlight = function()
{
	var list = $$('#livesearch_suggestions li');
	if (!list) return false;
	
	if (this.highlightActive)
	{
		list[this.highlighted].className = '';
		this.highlightActive = false;
	}
}