var t = null;
var liveSearchLast = "";
var isIE = false;

function liveSearchInit() {
	
	if (jQuery.browser.safari) {
		jQuery('#livesearch').bind("keydown",liveSearchKeyPress);
	} else if (jQuery.browser.mozilla) {
		jQuery('#livesearch').bind("keypress",liveSearchKeyPress);	
	} else {
		jQuery('#livesearch').bind('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.nextSibling.nextSibling.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.nextSibling.nextSibling.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");
		}
		jQuery("#LSResult").css("display","none");
		jQuery("input#jxlivesearch").attr("value",'');
	} 
}
function liveSearchStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()",200);
}

function liveSearchDoSearch() {
	if (liveSearchLast != jQuery("input#jxlivesearch").val()) {
	if ( jQuery("input#jxlivesearch").val() == "") {
		jQuery("#LSResult").css("display","none");
		highlight = jQuery("#LSHighlight");
		if (highlight) {
			highlight.removeAttr("id");
		}
		return false;
	}

	jQuery.get(_jxcore_live_site+'/templates/iluv/jxlivesearch.php?q='+ jQuery("input#jxlivesearch").val()+'&searchphrase='+_jxcore_searchway+'&ordering='+_jxcore_resultsorder+'&intro='+_jxcore_contentintro+'&tip='+_jxcore_tooltip+'&lmt='+_jxcore_resultslimit,
	function(data){
    //alert("Data Loaded: " + data);
    liveSearchProcessReqChange(data);
    });
	liveSearchLast = jQuery("input#jxlivesearch").val();
	}
	
}

function liveSearchProcessReqChange(jxdata) {
	
		jQuery("#LSResult").css("display","block");
		jQuery("#LSResult").html('<div id="searchcontrols" class="oddresult"><div class="alignleft">'+_jxcore_clickresults+'</div><a href="javascript://" title="Close results" onclick="closeResults()">close (esc)</a></small></div><br /></div><div id="searchheader" style="display: none;"><strong><small>top 10 results</small></strong></div>'+jxdata);

if (jQuery.browser.msie == true || jQuery.browser.safari == true  || jQuery.browser.opera == true) {	
jQuery(document).ready(JT_init);	
}
		
}

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

function closeResults() {
    jQuery("#LSResult").css("display","none");
}