var pause = false;
var query = "";
var active_row = null;

function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        var prevalue = input.value;
        input.value += ' ';
        input.setSelectionRange(selectionStart, selectionEnd);
        input.value = prevalue;
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function setCaretToEnd (input) {
  setSelectionRange(input, input.value.length, input.value.length);
}

var completion_counter=0;
var typingTimer=null;
var myLanguage = 'de';

function autoComplete(searchfield, dontSetTimer)
{
	// intelligent timing routine, marko 20070327. reduces the amount of ajax requests considerably. only if one types slower than 500ms per char, more than one request get fired.
	if(typingTimer) {
		clearTimeout(typingTimer);
		typingTimer=null;
	}
	if(!dontSetTimer) {
		if(!typingTimer) {
			typingTimer=setTimeout("autoComplete('" + searchfield + "', true);",500);
			return;
		}
	}
	var temp = $(searchfield);
	query = temp.value;
	if(query.length==0) { // don't fire ajax if the string is empty, marko 20070327
		$('search_auto_complete').style.display="none";
		return;
	}
	active_row = null;
	++completion_counter;
	// completion counter is needed so that if more requests are fired (which is unprobable enough), only the latest gets evaluated. marko 20070312
	var old_completion_counter=completion_counter; // seems senseless maybe (because of the if(completion_counter==old_completion_counter) below, but do not change that. one var is local, the other not, thats the difference.
	new Ajax.Request('search_auto_complete.php', {
		method: 'get',
		parameters: 'search=' + escape(query),
		onSuccess: function(ajaxRequest) {
			if(completion_counter==old_completion_counter) {
				autoComplete_handleSuccess(ajaxRequest, searchfield);
			}
		},
		onFailure: autoComplete_handleFailure,
		asynchronous: true
	});
}

function autoComplete_handleSuccess(ajaxRequest, searchfield)
{
	var hit = '';
	var about = '';
	if (myLanguage != 'fr')
	{
		hit = "Treffer";
		about = "ca.";
	}
	else
	{
		about = "env.";
		hit = "r&eacute;sultats";
	}
	var completions = ajaxRequest.responseXML.getElementsByTagName('completion');
	var layer = $('search_auto_complete');
	if (completions.length)
	{
		layer.style.display = "block";
		layer.innerHTML = "";
		var content = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		for (var i = 0; i < completions.length; i++)
		{
			var entry = completions[i].getElementsByTagName('entry')[0].childNodes[0].nodeValue;
			var count = completions[i].getElementsByTagName('count')[0].childNodes[0].nodeValue;
			content += '<tr id="row'+i+'" onmouseover="autoComplete_highlight('+i+', \''+searchfield+'\')" onclick="autoComplete_select('+i+', true, \''+searchfield+'\')"><td id="entry'+i+'" class="autocompleteLeft">'+entry+'</td><td id="count'+i+'" class="autocompleteRight">'+about+'&nbsp;'+count+'&nbsp;'+hit+'</td></tr>';
		}
		content += '</table>';
		layer.innerHTML = content;
	}
	else
	{
		layer.style.display = "none";
	}
}

function autoComplete_handleFailure(ajaxRequest)
{
	/* autoComplete_handleFailure */
}

function autoComplete_select(row, close, searchfield)
{
	var field = $(searchfield);
	field.value = field.previousvalue = $("entry" + row).innerHTML;
	if (close)
	{
		$('search_auto_complete').style.display = 'none';
		field.focus();
		// $('ad_search').submit(); // don't fire if klicked on an autocomplete item
	}
}

function autoComplete_highlight(row, searchfield)
{
	//alert(active_row);
	if($('search_auto_complete').style.display != 'none')
	{
		if(active_row != null)
		{
			$("entry" + active_row).className = 'autocompleteLeft';
			$("count" + active_row).className = 'autocompleteRight';
		}
		$("entry" + row).className = 'autocompleteLeftHover';
		$("count" + row).className = 'autocompleteRightHover';

		var field = $(searchfield);
		active_row = row;
		setCaretToEnd(field);
	}
}

function autoComplete_scroll(field, key, lang)
{
	myLanguage = lang;

	field.ffspecialhack = "yes";
	var active = active_row;

	if (!(key == 38 || key == 40))
	{
		return;
	}

	if (38 == key)
	{
		// up
		if (active == 0 || active == null)
		{
			active = 9;
		}
		else
		{
			active -= 1;
		}
		autoComplete_highlight(active, field.id);
	}
	else if (40 == key)
	{
		// down
		if(active == 9 || active == null)
		{
			active = 0;
		}
		else
		{
			active += 1;
		}
		autoComplete_highlight(active, field.id);
	}

	setCaretToEnd(field);
	setTimeout('setCaretToEnd($("'+field.id+'"))', 20);
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getElementPosition(element)
{
	var x=0,y=0;
	
	if (element.offsetParent)
	{
		while (element.offsetParent)
		{
			x += element.offsetLeft; // - element.scrollLeft;
			y += element.offsetTop; //  - element.scrollTop;
			element = element.offsetParent;
		}
	}
	else
	{
		if (element.x)
		{
			x += element.x;
		}
		if (element.y)
		{
			y += element.y;
		}
	}
	return {x:x,y:y};
}

var mouse = false;
function mouseStatus(status) {
	mouse = status;
}

function hideIfMouseIsNotOver(element) {
	if(!mouse) {
		$(element).style.display = 'none';
	}
}
