/*
* ui.js : OpenSearch PHP Samples UI javascript ÆÄÀÏ
*
* ÀÌ ÆÄÀÏÀº ¾Æ¿ô¸µÅ©(http://www.outlink.co.kr) OpenSearch PHP SamplesÀÇ ÀÏºÎÀÔ´Ï´Ù.
* (c)2008 Geonjisoft, All Rights Reserved.
*
* º» OpenSearch SamplesÀº ¾Æ¿ô¸µÅ© ¼­ºñ½º¿¡ °¡ÀÔÇÏ½Å È¸¿ø»ç¿¡°Ô Á¦°øµÇ¸ç
* Samples¿¡ ´ëÇÑ ¹«´Ü ¹èÆ÷³ª Àç¹èÆ÷´Â Çã°¡µÇÁö ¾Ê½À´Ï´Ù.
*
* Contact Information :
* searchq@geonji.co.kr
* http://www.geonji.co.kr
*/

/*
* Âü°í:
* 1) proxy ¼³Ä¡ °æ·Î°¡ º¯°æµÈ °æ¿ì °æ·Î ¼öÁ¤ ÇÊ¿ä
*/

var outlink;

if (!outlink)
	outlink = {};
	
outlink.UI = function(selfName, onloadName)
{
	this.selfName = selfName;
	this.dir = 0;
	this.enable = true;
	this.maxCount = 0;
	this.curSel = -1;
	this.selColor = "#EFEFEF";
	this.noneSelColor = "#ffffff";
	this.wordiemId = "worditem";
	this.forFirefoxDB = "";
	this.isFocusSearchName = false;
	this.resultCountPostfix = "_result";
	this.onloadName = onloadName;

}

outlink.UI.prototype.init = function()
{
	var search = document.getElementById(this.searchName);	
	var setFocus = this.selfName + ".isFocusSearchName = true;";
	var setBlur = this.selfName + ".isFocusSearchName = false;";

	search.onfocus = function()
	{	
		eval(setFocus);
	}
	
	search.onblur = function()
	{	
		eval(setBlur);
	}
}

outlink.UI.prototype.setIsFocusSearchName = function(isFocusSearchName)
{
	this.isFocusSearchName = isFocusSearchName;
}

outlink.UI.prototype.setSearchformName = function(searchformName)
{
	this.searchformName = searchformName;
}

outlink.UI.prototype.setSearchName = function(searchName)
{
	this.searchName = searchName;	
}

outlink.UI.prototype.setSearchformBottomName = function(searchformbottomName)
{
	this.searchformbottomName = searchformbottomName;
}

outlink.UI.prototype.setSearchBottomName = function(searchbottomName)
{
	this.searchbottomName = searchbottomName;	
}

outlink.UI.prototype.setAction = function(action)
{
	this.action = action;
}

outlink.UI.prototype.setWhereName = function(whereName)
{
	this.whereName = whereName;
}

outlink.UI.prototype.setAutocompleteName = function(autocompleteName)
{
	this.autocompleteName = autocompleteName;
}

outlink.UI.prototype.setToggleshowName = function(toggleshowName)
{
	this.toggleshowName = toggleshowName;
}

outlink.UI.prototype.setToggleDownImgPath = function(toggleDownImgPath)
{
	this.toggleDownImgPath = toggleDownImgPath;
}

outlink.UI.prototype.setToggleUpImgPath = function(toggleUpImgPath)
{
	this.toggleUpImgPath = toggleUpImgPath;
}

outlink.UI.prototype.setEnableAutoName = function(enableautoName)
{
	this.enableautoName = enableautoName;
}

outlink.UI.prototype.setAutoForwardName = function(autoForwardName)
{
	this.autoForwardName = autoForwardName;
}

outlink.UI.prototype.setAutoBackwardName = function(autoBackwardName)
{
	this.autoBackwardName = autoBackwardName;
}

outlink.UI.prototype.setToggledirName = function(toggledirName)
{
	this.toggledirName = toggledirName;
}

outlink.UI.prototype.setLeftOffset = function(offset)
{
	this.leftOffset = offset;
}

outlink.UI.prototype.setTopOffset = function(offset)
{
	this.topOffset = offset;
}

outlink.UI.prototype.getAbsoluteLeft = function(node)
{
	var curNode = node;
	var left = 0;

	while(curNode.tagName != "BODY")
	{
		left += curNode.offsetLeft;
		curNode = curNode.offsetParent;
	}
	
	return left;
}

outlink.UI.prototype.getAbsoluteTop = function(node)
{
	var curNode = node;
	var top = 0;

	while(curNode.tagName != "BODY")
	{
		top += curNode.offsetTop;
		curNode = curNode.offsetParent;
	}
	
	return top;
}

outlink.UI.prototype.setDocumentOnClick = function()
{
	var toggleshowName = this.toggleshowName;
	var hideAutoComplete = this.selfName + ".hideAutoComplete()";

	document.onclick = function(e)
	{
		if (!e)
			e = window.event;	
			
		var target = e.target || e.srcElement		
			
		if (target.id == toggleshowName)
			return;
		
		eval(hideAutoComplete);
	}
	
}	

outlink.UI.prototype.setDocumentKeydown = function()
{
	var search = this.searchName;
	var searchbottom = this.searchbottomName;
	var isFocus = this.selfName + ".isFocusSearchName;";
	
	document.onkeydown = function(e)
	{
		if (!e)
			e = window.event;	
			
		var target = e.target || e.srcElement		
			
		if (target.id == search || target.id == searchbottom)
			return;

		var s = document.getElementById(search);
		
		if (!eval(isFocus))
		{
			if (e.altKey || e.ctrlKey || e.shiftKey)//alt ctrl shiftÅ°°¡ ´­·ÈÀ¸¸é Æ÷Ä¿½º Ã³¸®¸¦ ¾ÈÇÑ´Ù.
				return;
				
			var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode
			
			if (keyCode == 21)//ÇÑ¿µÅ°
			{
				if (s.style.imeMode == 'active' || s.style.imeMode == '')
				{
					s.style.imeMode = 'inactive';
				}
				else
				{
					s.style.imeMode = 'active';
				}
				
			}
			else
			{			
				if (s.style.imeMode == 'active' || s.style.imeMode == '')
				{
					s.style.imeMode = 'inactive';
				}
			}
			
			s.value = '';
			s.focus();
		}	
		
	}
	
}

outlink.UI.prototype.setWindowOnResize = function()
{
	window.onresize = this.getAutoCompletePositionFunc();
}

outlink.UI.prototype.getAutoCompletePositionFunc = function() 
{
	var autocompleteName = this.autocompleteName;
	var searchName = this.searchName;
	var leftoffset = this.leftOffset;
	var topoffset = this.topOffset;
	var getAbsoluteLeft = this.getAbsoluteLeft;
	var getAbsoluteTop = this.getAbsoluteTop;

	return function()
	{
		var obj = document.getElementById(autocompleteName);

		obj.style.left = (getAbsoluteLeft(document.getElementById(searchName)) + leftoffset) + 'px';//5;
		obj.style.top = (getAbsoluteTop(document.getElementById(searchName)) + topoffset) + 'px';//5;
	}
}

outlink.UI.prototype.setAutoCompletePosition = function()
{
	var func = this.getAutoCompletePositionFunc();
	
	func();
}

outlink.UI.prototype.toggleEnable = function()
{
	var enableauto = document.getElementById(this.enableautoName);
	var autoForward = document.getElementById(this.autoForwardName);
	var autoBackward = document.getElementById(this.autoBackwardName);
	
	if (this.enable)
	{
		this.enable = false;
		enableauto.innerText = "±â´ÉÄÑ±â";
		
		autoForward.innerHTML = "±â´ÉÄÑ±â¸¦ ´­·¯ ÁÖ½Ê½Ã¿À.";
	    autoBackward.innerHTML = "";
	}
	else
	{
		this.enable = true;
		enableauto.innerText = "±â´É²ô±â";
		
		autoForward.innerHTML = "";
	    autoBackward.innerHTML = "";
	    
	    this.sendQuery();
	}
	
}

outlink.UI.prototype.toggleDir = function()
{
	var toggledir = document.getElementById(this.toggledirName);
	
	if (this.dir == 0)
	{
		toggledir.innerText = "¾Õ´Ü¾î´õº¸±â";
		this.dir = 1;
	}
	else
	{
		toggledir.innerText = "³¡´Ü¾î´õº¸±â";
		this.dir = 0;
	}
		
	this.sendQuery();
}

outlink.UI.prototype.getAjaxHandler = function() 
{
	var ui = this;
	
	return function(keywordXML) 
	{
		var words = keywordXML.getElementsByTagName("words");
		var org = "";
		var forwardContent = "";
		var backwardContent = "";

		if (ui.enable)
		{
			if (words != null && words.length > 0)
			{
				var forward = words[0].getElementsByTagName("forward");
				var backward = words[0].getElementsByTagName("backward");
				
				org = words[0].getAttribute("org");
				
				ui.maxCount = 0;
				ui.curSel = -1;
	
				if (forward != null && forward.length > 0)
				{
					var len = forward[0].childNodes.length;
	        
	                forwardContent += "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
					
					for (var i = 0; i < len; i++)
					{
			    	 	var item = forward[0].childNodes[i];
					    
					    if (typeof(item.getAttribute) == 'undefined')
					    	continue;
					    	
   					    var keyword = item.getAttribute("keyword");	
					    var resultCount = (new Number(item.getAttribute("resultCount"))).toLocaleString();
					    var resultDotIndex = resultCount.indexOf(".");
					    
					    if (resultDotIndex != -1)
						    resultCount = resultCount.substring(0, resultDotIndex);
					    
					    forwardContent += "<tr width='100%'>";
					    forwardContent += "<td id='" + ui.wordiemId + ui.maxCount + "' onclick=\"" + ui.selfName + ".autoSearch('" + keyword + "')\" onmouseover='" + ui.selfName + ".cellMouseOver(event)' onmouseout='" + ui.selfName + ".cellMouseOut(event)'>";
				     	forwardContent += "<a style='color : #000000' href='#' onclick=\"" + ui.selfName + ".autoSearch('" + keyword + "')\">";
					    
					    if (ui.dir == 0)
					    {
						    forwardContent += "<span class=\"org_9\">" + keyword.substring(0, org.length) + "</span>";
						    forwardContent += keyword.substring(org.length);
					    }
					    else
					    {
					     	forwardContent += keyword.substring(0, keyword.length-org.length);
					     	forwardContent += "<span class=\"org_9\">" + keyword.substring(keyword.length-org.length) + "</span>";
					    }
						
					    forwardContent += "</a></td>";
					    forwardContent += "<td id='" + ui.wordiemId + ui.maxCount + ui.resultCountPostfix + "' align='right' onclick=\"" + ui.selfName + ".autoSearch('" + keyword + "')\" onmouseover='" + ui.selfName + ".cellMouseOver(event)' onmouseout='" + ui.selfName + ".cellMouseOut(event)'><span class=\"green_9\">¾à " + resultCount +" °á°ú</span></td>"					    
					    forwardContent += "</tr>";
					    
				    	ui.maxCount++;
				
				    }
				    
				    forwardContent += "</table>";
			    }    
			    			
				if (backward != null && backward.length > 0)
				{
					var len = backward[0].childNodes.length;
					
					backwardContent += "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
					
					for (var i = 0; i < len; i++)
					{
			    	 	var item = backward[0].childNodes[i];
					    
					    if (typeof(item.getAttribute) == 'undefined')
					    	continue;
					    	
						var keyword = item.getAttribute("keyword");	
					    var resultCount = (new Number(item.getAttribute("resultCount"))).toLocaleString();
					    var resultDotIndex = resultCount.indexOf(".");
					    
					    if (resultDotIndex != -1)
						    resultCount = resultCount.substring(0, resultDotIndex);
					    
					    backwardContent += "<tr width='100%'>";
					    backwardContent += "<td id='" + ui.wordiemId + ui.maxCount + "' onclick=\"" + ui.selfName + ".autoSearch('" + keyword + "')\" onmouseover='" + ui.selfName + ".cellMouseOver(event)' onmouseout='" + ui.selfName + ".cellMouseOut(event)'>";
				     	backwardContent += "<a style='color : #000000' href='#' onclick=\"" + ui.selfName + ".autoSearch('" + keyword + "')\">";
					    
					    if (ui.dir == 0)
					    {
						    backwardContent += keyword.substring(0, keyword.length-org.length);
						    backwardContent += "<span class=\"org_9\">" + keyword.substring(keyword.length-org.length) + "</span>";
						}
						else
						{
							backwardContent += "<span class=\"org_9\">" + keyword.substring(0, org.length) + "</span>";
							backwardContent += keyword.substring(org.length);
					    }
						
					  	backwardContent += "</a></td>";
					    backwardContent += "<td id='" + ui.wordiemId + ui.maxCount + ui.resultCountPostfix + "' align='right' onclick=\"" + ui.selfName + ".autoSearch('" + keyword + "')\" onmouseover='" + ui.selfName + ".cellMouseOver(event)' onmouseout='" + ui.selfName + ".cellMouseOut(event)'><span class=\"green_9\">¾à " + resultCount +" °á°ú</span></td>"					    
					    backwardContent += "</tr>";
				    	
				    	ui.maxCount++;
				    }
				    
				    backwardContent += "</table>";
			   
			    }
			}
		
			var autoForward = document.getElementById(ui.autoForwardName);
		    var autoBackward = document.getElementById(ui.autoBackwardName);
				
			if (forwardContent == "")
			    forwardContent = "°Ë»öµÈ ´Ü¾î°¡ ¾ø½À´Ï´Ù.";
				
			if (backwardContent == "")
			    backwardContent = "°Ë»öµÈ ´Ü¾î°¡ ¾ø½À´Ï´Ù.";
				
		    autoForward.innerHTML = forwardContent;
		    autoBackward.innerHTML = backwardContent;
			
		    ui.showAutoComplete();
		
		}
		
	}	 
}

outlink.UI.prototype.hideAutoComplete = function()
{
	var autocomplete = document.getElementById(this.autocompleteName);
	var toggleshow = document.getElementById(this.toggleshowName);
	
	autocomplete.style.display = "none";	
	
	toggleshow.src = this.toggleDownImgPath;
	toggleshow.alt = "º¸ÀÌ±â";
	
	this.curSel = -1;	
	
}

outlink.UI.prototype.showAutoComplete = function()
{
	var autocomplete = document.getElementById(this.autocompleteName);
	var toggleshow = document.getElementById(this.toggleshowName);	
	
	autocomplete.style.display = "block";	
	
	toggleshow.src = this.toggleUpImgPath;
	toggleshow.alt = "°¨Ãß±â";
				
}	

outlink.UI.prototype.toggleShow = function()
{		
	var autocomplete = document.getElementById(this.autocompleteName);
	
	if (autocomplete.style.display == "block")
		this.hideAutoComplete();
	else
		this.showAutoComplete();
	
}	

outlink.UI.prototype.doSearch = function()
{	
	var searchform = document.getElementById(this.searchformName);
	var search = document.getElementById(this.searchName);
	var keyword = null;

	if (this.curSel != -1)
	{
		var worditem = document.getElementById(this.wordiemId + this.curSel);
		
		this.curSel = -1;
		worditem.onclick();

	}
	else
	{
		if (search.value == '')
		{
			alert('°Ë»ö¾î¸¦ ÀÔ·ÂÇÏ½Ê½Ã¿À.');
			search.focus();
			return false;
		}
		
		var where = document.getElementById(this.whereName);
		
		if (this.action.indexOf("?") > 0)
		{
			this.action += "&";
		}
		else
		{
			this.action += "?";
			
		}
		
		if (where != null)
		{
			this.action += this.whereName + "=" + where.value;
			
		}else{
			this.action += this.whereName + "=localmore";
		}
		
		keyword = search.value;
	
		searchform.method = 'POST';
		searchform.action = this.action + "&" + this.searchName + "=" + keyword;
		searchform.submit();
	}	
				
	return false;
}

outlink.UI.prototype.autoSearch = function(keyword)
{	
	var search = document.getElementById(this.searchName);
	
	search.value = keyword;
	this.doSearch();
}
/*
outlink.UI.prototype.processQuery = function(e)
{
	if (!e)
		e = window.event;
		
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode
		
	if (!(keyCode == 40 || keyCode == 38))//40 : ¾Æ·¡È­»ìÇ¥, 38 : À­È­»ìÇ¥
	{
		this.sendQuery();
	}
}
*/

outlink.UI.prototype.doSearchBottom = function()
{	
	var searchform = document.getElementById(this.searchformbottomName);
	var search = document.getElementById(this.searchbottomName);
	var keyword = null;	

	if (search.value == '')
	{
		alert('°Ë»ö¾î¸¦ ÀÔ·ÂÇÏ½Ê½Ã¿À.');
		search.focus();
		return false;
	}
	
	if (this.action.indexOf("?") > 0)
	{
		this.action += "&";
	}
	else
	{
		this.action += "?";
		
	}
	
	if (where != null)
	{
		this.action += this.whereName + "=" + where.value;
		
	}		
	
	keyword = search.value;		

	searchform.method = 'POST';
	searchform.action = this.action + "&" + this.searchName + "=" + encodeURIComponent(keyword);
	searchform.submit();
				
	return false;
}

outlink.UI.prototype.processKeyBottom = function(e)
{
	if (!e)
		e = window.event;
		
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode
		
	if (keyCode == 13) 
	{ 
		return this.doSearchBottom(); 
	}
	
	return true;
}

outlink.UI.prototype.processKey = function(e)
{
	if (!e)
		e = window.event;
		
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode
		
	if (keyCode == 13) 
	{ 
		return this.doSearch(); 
	}
	else if (keyCode == 40 || keyCode == 38)//40 : ¾Æ·¡È­»ìÇ¥, 38 : À­È­»ìÇ¥
	{
		this.showAutoComplete();
		
		if (this.maxCount > 0)
		{		
			if (keyCode == 40)
			{
				this.curSel++;
			}
			else
			{
				this.curSel--;
			}
			
			if (this.curSel >= this.maxCount)
			{
				this.curSel = 0;
			}
		
			if (this.curSel < 0)
			{
				this.curSel = this.maxCount - 1;
			}
		
			var worditemCur = document.getElementById(this.wordiemId + this.curSel);
			var worditemCurResult = document.getElementById(this.wordiemId + this.curSel + this.resultCountPostfix);
		
			for (var i = 0; i < this.maxCount; i++)
			{
				var worditemPrev = document.getElementById(this.wordiemId + i);
				var worditemPrevResult = document.getElementById(this.wordiemId + i + this.resultCountPostfix);
				
				worditemPrev.style.backgroundColor = this.noneSelColor;
				worditemPrevResult.style.backgroundColor = this.noneSelColor;
			}
		
			worditemCur.style.backgroundColor = this.selColor;	
			worditemCurResult.style.backgroundColor = this.selColor;	
		}

	}	
	else
	{		
		this.keyHandler();
	}
	
	return true;

}

outlink.UI.prototype.keyHandler = function()
{
	if (navigator.userAgent.indexOf("Firefox") != -1)
	{
		var search = document.getElementById(this.searchName).value;
			
		if (search != this.forFirefoxDB)
		{
			this.forFirefoxDB = search;
			
			this.sendQuery();
		}
		
		window.setTimeout(this.selfName + ".keyHandler()", 1);
	}
	else
	{
		this.sendQuery();
	}
}

outlink.UI.prototype.setSearchFocus = function()
{
	var search = document.getElementById(this.searchName);
	
	search.focus();
	
}	

outlink.UI.prototype.cellMouseOver = function(e)
{
	if (!e)
		e = window.event;
		
	var td = e.target || e.srcElement;
	
	while (td.tagName != "TD")
	{
		td = td.parentElement || td.parentNode;
	}
	
	if (td != null)
	{
		var postfixStart = td.id.indexOf(this.resultCountPostfix);
		var otherTD = null;
		
		if (postfixStart != -1)
		{
			var otherID = td.id.substring(0, postfixStart);

			otherTD = document.getElementById(otherID);
		}
		else
		{
			otherTD = document.getElementById(td.id + this.resultCountPostfix);
		}
		
		otherTD.style.backgroundColor = this.selColor;
		td.style.backgroundColor = this.selColor;
		td.style.cursor = "hand";
		
	}
	
}

outlink.UI.prototype.cellMouseOut = function(e)
{
	if (!e)
		e = window.event;
		
	var td = e.target || e.srcElement;	
	
	while (td.tagName != "TD")
	{
		td = td.parentElement || td.parentNode;
	}
	
	if (td != null)
	{
		var mouseSel = parseInt(td.id.substring(this.wordiemId.length));
		
		if (mouseSel != this.curSel)
		{
			var postfixStart = td.id.indexOf(this.resultCountPostfix);
			var otherTD = null;
		
			if (postfixStart != -1)
			{
				var otherID = td.id.substring(0, postfixStart);
			
				otherTD = document.getElementById(otherID);
			}
			else
			{
				otherTD = document.getElementById(td.id + this.resultCountPostfix);
			}
		
			otherTD.style.backgroundColor = this.noneSelColor;
			td.style.backgroundColor = this.noneSelColor;
		}
	}
}

outlink.UI.prototype.sendQuery = function() 
{	
	window.setTimeout(this.selfName + ".sendQueryInternal()", 50);
		  	
	return true;
}

outlink.UI.prototype.sendQueryInternal = function() 
{	
	var keyword = document.getElementById(this.searchName).value;
	
	if (keyword != '' && this.enable)
	{
		var ajax = new outlink.Ajax(this);
		
		keyword = encodeURIComponent(keyword);

		ajax.sendQuery("/outlink_opensearch/proxy/autocomplete_proxy.php", "dir=" + this.dir + "&keyword=" + keyword);		
	}  	

}

outlink.UI.prototype.attachPlayButton = function(parentName, imgURL, height, url)
{
	var onload = eval(this.onloadName + ".loaded");
	
	if (onload)
	{
		var div = document.createElement("div");
		var img = document.createElement("img");
		var imgHeight = height;
		var parent = document.getElementById(parentName);
		var parent_parent = parent.parentElement || parent.parentNode;
		var leftOffset = 5;
		var topOffset = -5;
	
		div.style.position = "absolute";
		div.style.zIndex = 1;
		div.style.top = (this.getAbsoluteTop(parent) + parent.height - imgHeight + topOffset) + "px";
		div.style.left = (this.getAbsoluteLeft(parent) + leftOffset) + "px";
		div.onclick = function()
		{		
			window.open(url);		
		}
		
		img.style.cursor = 'hand';
		img.src = imgURL;
		img.border = 0;
		
		div.appendChild(img);
		parent_parent.appendChild(div);
	}
	else
	{
		window.setTimeout(this.selfName + ".attachPlayButton('" + parentName + "','" + imgURL + "'," + height + ",'" + url + "')", 300);
		
	}
}