var liveSearchField = 'liveTextField';
var liveSearchDivParent = 'liveSearchParentDiv';
var liveSearchFieldValue = '';
var liveSearchRunning = false;
var liveSearchInited = false;

function getLiveSearch() {
	if (!liveSearchInited) {
		IncludeJavaScript('/dwr/util.js');
		IncludeJavaScript('/dwr/engine.js');
		IncludeJavaScript('/dwr/interface/LiveSearchAccess.js');
		liveSearchInited = true;
	}

	var lValField = document.getElementById(liveSearchField);
	if (lValField.value != liveSearchFieldValue && !liveSearchRunning) {
		if (window.LiveSearchAccess === undefined) {
		} else {
			liveSearchFieldValue = lValField.value;
			liveSearchRunning = true;
			LiveSearchAccess.liveSearchList(lValField.value, sLiveSearch);
		}
	}
}

function sLiveSearch(result) {
	if (document.getElementById('liveSearchDiv') == null) {
		createLiveSearchDiv();
	}
	var lDiv = document.getElementById('liveSearchDiv');
	var iText = "";
	if (result != null){
		for (i = 0; i < result.length; i++) {
			var enLink = new String(result[i]);
			enLink = encodeURI(enLink);
			var rlink = "<a href='"
					+ encodeURI("/wyszukaj/?q="
							+ enLink) + "' >" + result[i] + "</a><br/>";
			iText += rlink;
		}
	}
	lDiv.innerHTML = iText;
	liveSearchRunning = false;
	if (result != null && result.length == 0){	
		//lDiv.style = "display:none;";
		//lDiv.visible = false;
	} else {
		//lDiv.style = "display:block;";
		//lDiv.visible = true;
	}
}

function IncludeJavaScript(jsFile) {
	scriptNode = document.createElement('script');
	scriptNode.type = 'text/javascript';
	scriptNode.src = jsFile;
	document.getElementsByTagName("head")[0].appendChild(scriptNode);

}

function createLiveSearchDiv() {
	var parent = document.getElementById(liveSearchDivParent);
	if (parent == null) {
		alert('parent div not found! ' + liveSearchDivParent);
	} else {
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id', 'liveSearchDiv');
		newdiv.innerHTML = 'Element';
		parent.appendChild(newdiv);
	}
}

