/** \file
 * Funktionen für die AJAX/Live Suche.
 *
 * @author "$Author: rr@nci.ch $"
 * @version "$Id: Konstanten.php 741 2010-06-01 07:51:55Z rr@nci.ch $"
 */

var activetimeout=0;
var hidetimeout=0;
var lastvalue='unmöglichdassdieserwertbereitsexistiert';

function startLiveSearch(e, field)
{
	//resultdiv=document.getElementById('livesearch_' + field);
	//if(resultdiv) resultdiv.style.display='block';
	clivesearch_volltext.eInput=e;
	window.clearTimeout(activetimeout);
	if(lastvalue == e.value)
	{
		// Wert hat sich nicht geändert
		clivesearch_volltext.eRoot.style.display='block';
	} else
	{
		// Neuer Wert
		clivesearch_volltext.eRoot.style.display='none';
		if(e && e.value && (e.value.replace(/^\s+/, '').replace (/\s+$/, '') != ''))
			activetimeout=window.setTimeout("doLiveSearch('" + field + "', '" + e.value + "')", 500);
	}
}


function doLiveSearch(field, query)
{
	lastvalue=query;
	var sURL='index.php/REST/livesearch/' + field + '/' + query;
	clivesearch_volltext.reload(sURL, 'block');
}

function setFocus(eid, caretPos)
{
	if(!caretPos) caretPos=1000;
	var elem=document.getElementById(eid);
	
	elem.focus();
	
	if(elem.createTextRange)
	{
		var range = elem.createTextRange();
		range.move('character', caretPos);
		range.select();
	} else
	{
		if(elem.selectionStart)
		{
			elem.focus();
			elem.setSelectionRange(caretPos, caretPos);
		} else
			elem.focus();
	}
}

function ehLiveSearch(e)
{
	// event-Object holen
	if(!e) var e=window.event || arguments.callee.caller.arguments[0];
	// target-Element holen
	var target;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) // defeat Safari bug
		target = target.parentNode;

	if( (e.type == 'mouseout') )
	{
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

		if( (target.id == 'livesearch_volltext') && (e.offsetY > 0) )
			hidetimeout=window.setTimeout("clivesearch_volltext.eRoot.style.display='none'", 500)

		//if( (target.id == 'livesearch_volltext_container') && (e.offsetY <= target.clientHeight) )
		//	hidetimeout=window.setTimeout("clivesearch_volltext.eRoot.style.display='none'", 500)

	}
	
	
	if(e.type == 'mousedown')
	{
		clivesearch_volltext.eRoot.style.display='none';
		clivesearch_volltext.eInput.value=target.innerText ? target.innerText : target.textContent;
		clivesearch_volltext.eInput.onclick();
		window.setTimeout('setFocus(clivesearch_volltext.eInput.id)', 500);
	}

	//window.setTimeout("clivesearch_volltext.eInput.focus();", 500);
	//clivesearch_volltext.eInput.form.submit();
}


