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

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

function liveSearchHideDelayed() {
	window.setTimeout("liveSearchHide()",400);
}
	
function liveSearchHide() {
	document.getElementById("LSResult").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function liveSearchKeyPress(event) {
	
	if(event.keyCode == 40 ) { // ¾Æ·¡·Î ÀÌµ¿ 
		highlight = document.getElementById("LSHighlight");

		if(!highlight) {

			highlight = document.getElementById("LSShadow").firstChild.firstChild;
		}
		else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}

		if(highlight) {
			highlight.setAttribute("id","LSHighlight");
			liveSearchLast = highlight.getAttribute('value');
			document.forms.searchform.kword.value = highlight.getAttribute('value');
		}
		if (!isIE) {
			event.preventDefault(); 
		}
	}
	else if (event.keyCode == 38 ) { // À§·Î ÀÌµ¿
		
		highlight = document.getElementById("LSHighlight");
      
		if (!highlight) {
			highlight = document.getElementById("LSShadow").firstChild.lastChild;
		}
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
			liveSearchLast =  document.getElementById("LSHighlight").getAttribute('value');
            document.forms.searchform.kword.value = document.getElementById("LSHighlight").getAttribute('value');
		}
		if (!isIE) {
			event.preventDefault();
		}
		
	}
	else if (event.keyCode == 27) {

		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult").style.display = "none";
	}
}
function mouseOverMove(obj) {
	
	highlight = document.getElementById("LSHighlight");

	if (highlight) {
		highlight.removeAttribute("id");
	}

	if (obj) {
		obj.setAttribute("id","LSHighlight");
	}
}

function mouseOutMove(id) {

	highlight = document.getElementById(id);
	highlight.removeAttribute("id");
}

function liveSearchStart() {

	if (typeof liveSearchRoot == "undefined") {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
		liveSearchRootSubDir = "";
	}
	if (liveSearchLast != document.forms.searchform.kword.value) {

        if ( document.forms.searchform.kword.value == "") {
            liveSearchHide();
            return false;
        }

        searchStr = document.forms.searchform.kword.value;
        tjs.src="http://www.u-news.co.kr/se/rtquery.php?q="+searchStr;
        //searchStr = searchStr.replace("'","");
        //searchStr = searchStr.replace(" ","%20");
        window.setTimeout("Dosearch()",200);
    }
}

function Dosearch() 
{
	liveSearchLast = document.forms.searchform.kword.value;
    var  res = document.getElementById("LSResult");
    var  sh = document.getElementById("LSShadow");
    if (qs_ac_list !='')
    {
        res.style.display = "block";
        var qcon ="<ul class=\"LSRes\">";
        var qlen = qs_ac_list.length;
        for (var i=0;i<qlen ;i++ )
        {
            var tcon = qs_ac_list[i];
        	var tcon2 = tcon.replace(" ","%20");
            //alert(searchStr + " - " + tcon);
            var tconview = tcon.replace(searchStr,"<font color=#FF7979>"+searchStr+"</font>");
            qcon = qcon + "<li class=\"LSRow\" onMouseOver='mouseOverMove(this)' value='" 
                    + tcon + "'><a href='/se/search.php?kword=" + tcon2 + "' style='text-decoration:none;'>" + tconview  + "</a></li>";
        }

        sh.innerHTML = qcon;    
    } 
    else {
        sh.innerHTML = "";
        liveSearchHide();
    }

}

function closeResults() {
    document.getElementById("LSResult").style.display = "none";
}