	/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+

*/
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() {
     searchInput = document.getElementById('livesearch');
     if (searchInput == null || searchInput == undefined) return
 //  Only keypress catches repeats in moz/FF but keydown is needed for
 //  khtml based browsers.
     if (navigator.userAgent.indexOf("KHTML") > 0) {
         searchInput.addEventListener("keydown",liveSearchKeyPress,false);
         searchInput.addEventListener("focus",liveSearchDoSearch,false);
         searchInput.addEventListener("keydown",liveSearchStart, false);
     } else if (searchInput.addEventListener) {
         searchInput.addEventListener("keypress",liveSearchKeyPress,false);
         searchInput.addEventListener("blur",liveSearchHideDelayed,false);
         searchInput.addEventListener("keypress",liveSearchStart, false);
     } else {
         searchInput.attachEvent("onkeydown",liveSearchKeyPress);
         searchInput.attachEvent("onkeydown",liveSearchStart);
 //      searchInput.attachEvent("onblur",liveSearchHide);
         isIE = true;
     }

 //  Why doesn't this work in konq, setting it inline does.
     searchInput.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 getFirstHighlight() {
 var set = getHits();
 return set[0];
}

function getLastHighlight() {
 var set = getHits();
 return set[set.length-1];
}

function getHits() {
 var res = document.getElementById("LSShadow");
 var set = res.getElementsByTagName('li');
 return set
}


function findChild(object, specifier) {
 var cur = object.firstChild;
 try {
 while (cur != undefined) {
     cur = cur.nextSibling;
     if (specifier(cur) == true) return cur;
 }
 } catch(e) {};
 return null;

}

function findNext(object, specifier) {
var cur = object;
try {
while (cur != undefined) {

 cur = cur.nextSibling;
 if (cur.nodeType==3) cur=cur.nextSibling;

 if (cur != undefined) {
     if (specifier(cur) == true) return cur;
 } else { break }
}
} catch(e) {};
return null;
}

function findPrev(object, specifier) {
var cur = object;
try {
     cur = cur.previousSibling;
     if (cur.nodeType==3) cur=cur.previousSibling;
     if (cur!=undefined) {
         if (specifier(cur) == true)
             return cur;
     }
} catch(e) {};
return null;
}

function liveSearchKeyPress(event) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			//highlight = document.getElementById("LSShadow").firstChild.firstChild;
			highlight = getFirstHighlight();
		} else {
	         highlight.removeAttribute("id");
	         highlight = findNext(highlight, function (o) {return o.className =="LSRow";});			
			//highlight.removeAttribute("id");
			//highlight = highlight.nextSibling;
		}
		if (highlight) {
        	highlight.setAttribute("id","LSHighlight");
			highlight.scrollIntoView(false);
     	}
     	if (!isIE) { event.preventDefault(); }
	}
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			 //highlight = document.getElementById("LSResult").firstChild.firstChild.lastChild;
	         highlight = getLastHighlight();
		}
		else {
	         highlight.removeAttribute("id");
			//highlight = highlight.previousSibling;
	         highlight = findPrev(highlight, function (o) {return o.className=='LSRow';});
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
			highlight.scrollIntoView(false);
		}
		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";
	}
	//BACKSPACE - required for IE
	else if (event.keyCode == 8 && isIE) {
		liveSearchStart();
	}
}
function liveSearchStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()",200);
}

function liveSearchDoSearch() {

	if (typeof liveSearchRoot == "undefined") {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
		liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams == "undefined") {
		liveSearchParams2 = "";
	} else {
		liveSearchParams2 = "&" + liveSearchParams;
	}
	if (liveSearchLast != document.forms.searchform.q.value) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if ( document.forms.searchform.q.value == "") {
		liveSearchHide();
		liveSearchLast = "";
		return false;
	}
	if (window.XMLHttpRequest) {
		// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
	liveSearchReq.open("GET", "./worldsearch.php?q=" + document.forms.searchform.q.value + liveSearchParams2);
	liveSearchLast = document.forms.searchform.q.value;
	liveSearchReq.send(null);
	}
}

String.prototype.trim=function(){
    var str=this.replace(/(\s+$)/g,"");
    return str.replace(/(^\s*)/g,"");
}
function liveSearchProcessReqChange() {

	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("LSResult");
		var  sh = document.getElementById("LSShadow");

		if(liveSearchReq.responseText.trim() != ''){
			res.style.display = "block";
			sh.innerHTML = liveSearchReq.responseText;
		}else{
			res.style.display = "none";
		}
	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = liveSearchRoot + liveSearchRootSubDir + highlight.firstChild.getAttribute("href");
		return false;
	}
	else {
		//return true;
		if(!document.forms.searchform.q.value){
			alert('µµ½Ã¸íÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä');
			return false;
		}else{
			window.location = "./worldsearch.php?q2=" + document.forms.searchform.q.value;
			return false;
		}
	}
}

function closeResults(){
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult").style.display = "none";
}
function liveSearchHover(el) {
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		el.setAttribute("id","LSHighlight");
}

function liveSearchClicked(el) {
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		el.setAttribute("id","LSHighlight");
}