var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}

function liveSearchInit() {
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		if (document.getElementById('livesearch'))
			document.getElementById('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
	} else if (navigator.product == "Gecko") {
		if (document.getElementById('livesearch')) 
			document.getElementById('livesearch').addEventListener("keypress",liveSearchKeyPress,false);
		
	} else {
		if (document.getElementById('livesearch'))
			document.getElementById('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
			isIE = true;
	}

}

function liveSearchKeyPress(event) {
	
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("LSResult").firstChild.firstChild.firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("LSResult").firstChild.firstChild.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","LSHighlight");
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult").style.display = "none";
	} 
	else {
		document.getElementById("livesearch_results_text_base").style.display = "none";
		document.getElementById("livesearch_results_text").style.display = "block";
		liveSearchStart(); 
	}
}

function liveSearchStart() {

	//document.getElementById("livesearch_results_text").innerHTML = 'Keresés...';
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()", 200);
}

function liveSearchDoSearch() {

	// if (liveSearchLast != document.forms.livesearch_form.searchtext.value) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if (document.forms.livesearch_form.searchtext.value == "") {
		document.getElementById("LSResult").style.display = "none";
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("livesearch_results_text_base").style.display = "block";
		document.getElementById("livesearch_results_text").style.display = "none";
		return false;
	}
	else {
		// document.getElementById("livesearch_results_text").innerHTML = 'Keresés...';
	}
	
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (liveSearchReq) {
		liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
		liveSearchReq.open("GET", "index.ftx?module=livesearch&s=" + document.forms.livesearch_form.searchtext.value);
		liveSearchLast = document.forms.livesearch_form.searchtext.value;
		liveSearchReq.send(null);
		// document.getElementById("livesearch_results_text").innerHTML = 'Keresés...';
	}
	// }
}

function liveSearchProcessReqChange() {
	
	if (liveSearchReq.readyState == 4) {
		// alert('done!');
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		res.firstChild.innerHTML = liveSearchReq.responseText;
		 
	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = highlight.firstChild.getAttribute("href");
		return false;
	} 
	else {
		return true;
	}
}

function close_livesearch_field() {
	visibility(false, 'livesearch_main_bottom');
	visibility(false, 'livesearch_field');
	// document.getElementById('livesearch_results_text').innerHTML = '&nbsp;';
	document.getElementById("LSResult").style.display = "none";
}

function open_livesearch_field() {
	visibility('change', 'livesearch_field');
	visibility('change', 'livesearch_main_bottom');
	document.livesearch_form.searchtext.focus();
	// document.getElementById('livesearch_results_text').innerHTML = '&nbsp;';
}

function ls_mouseover(item) {

	highlight = document.getElementById('LSHighlight');
	if (highlight) highlight.removeAttribute('id');
	item.setAttribute('id','LSHighlight');
	
}
