var BufferedRequest =
{
	bufferText: false,
	bufferTime: 500,

	request: function(id, functionToCall)
	{
		setTimeout('BufferedRequest.bufferedRequest("' + id + '", "' + xajax.$(id).value + '", "' + functionToCall + '");', this.bufferTime);
	},

	bufferedRequest: function(id, string, functionToCall)
	{
		if (string == xajax.$(id).value && string != this.bufferString)
		{
			this.bufferString = string;
			BufferedRequest.doRequest(id, functionToCall);
		}
	},

	doRequest: function(id, functionToCall)
	{
		switch(functionToCall)
		{
			case 'livesearch':
				xajax_livesearch(xajax.$(id).value);
			break;
		}
	}
}

if (typeof(Livesearch) == 'undefined') Livesearch = {};

Livesearch = function()
{
	this.highlightActive = false;
	this.highlighted     = 0;

	var pointer = this;
	$('#livesearch_string').keyup(function(ev) {
        return pointer.keyup(ev);
    });
	$('#livesearch_string').keypress(function(ev) {
        return pointer.keypress(ev);
    });
	$('#livesearch_suggestions').click(function() {
        return pointer.click();
    });
	$('#livesearch_string').blur(function() {
        window.setTimeout('Livesearch.prototype.blur()', 500);
    });
	$('#livesearch_button').click(function() {
        return pointer.click();
    });
}

Livesearch.prototype.click = function()
{
	var list = $('#livesearch_suggestions li');
	if (!list) {
        return false;
    }

	if (!this.highlightActive) {
		redirect('index.php?ota=internalSearchResults&searchstring=' + $('#livesearch_string').val());
	} else {
		var tmp = $(list[this.highlighted]).attr('id').split('_');
		switch (tmp[0]) {
			case 'user':
                showUserProfile(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.blur = function()
{
	$('#livesearch_suggestions').hide();
}

Livesearch.prototype.keyup = 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]).attr('id').split('_');
			switch (tmp[0]) {
				case 'user':
                    showUserProfile(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').val());
		}
	break
	default:
		BufferedRequest.request('livesearch_string', 'livesearch');
		this.highlightActive = false;
		this.highlighted = 0;
	break;
	}
}

Livesearch.prototype.keypress = 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;
	}
}

