document.attachEvent("onclick", function(e){HideRecommendKeywordList();});

function ActionRecommendKeyword_OnKeyDown(e, oThis)
{	
	var oListSec = document.getElementById("recommend_keyword_sec");

	var keyword = oThis.value;
	if(keyword == '') 
	{
		oListSec.style.display = "none";
		return false;
	}

	if(oListSec.oInputKeyword == null)	oListSec.oInputKeyword = oThis;

	if (e.keyCode == 38) // UP
	{
		MoveRecommendKeyword("UP", oThis);
	}	
	else if (e.keyCode == 40)	// DOWN
	{
		MoveRecommendKeyword("DOWN", oThis);
	}
}

function ActionRecommendKeyword_OnKeyUp(e, oThis)
{
	var oListSec = document.getElementById("recommend_keyword_sec");

	var keyword = oThis.value;
	if(keyword == '') 
	{
		oListSec.style.display = "none";
		return false;
	}

	if(oListSec.oInputKeyword == null)	oListSec.oInputKeyword = oThis;
	
	if ( (e.keyCode >= 48  && e.keyCode <= 122) || (e.keyCode == 8) )
	{
		GetRecommendKeyword(oThis);
	}
}

function ActionRecommendKeyword_OnClick(oThis)
{
	var oListSec = document.getElementById("recommend_keyword_sec");

	var keyword = oThis.value;
	if(keyword == '') 
	{
		oListSec.style.display = "none";
		oListSec.oSel = null;
		return false;
	}

	if(oListSec.oInputKeyword == null)	oListSec.oInputKeyword = oThis;
	
	GetRecommendKeyword(oThis);
}


function MoveRecommendKeyword(dir, oThis)
{
	var oListSec = document.getElementById("recommend_keyword_sec");

	if (oListSec.style.display != "block")
	{
		return;
	}

	var b_oSel = oListSec.oSel;	
	
	var oSel = null;

	if (dir == "UP")
	{
		if(b_oSel == null)
		{
			return;
		}

		oSel = b_oSel.previousSibling;
	}
	else if (dir == "DOWN")
	{
		if(b_oSel == null)
		{
			oSel = oListSec.firstChild;
		}
		else
		{
			oSel = b_oSel.nextSibling;
		}
	}	

	if(oSel != null)
	{
		if(b_oSel != null)
		{
			ReleaseRecommendKeyword(b_oSel);
		}

		if(oSel.tagName == 'LI')
		{
			oListSec.oSel = oSel;
			SetRecommendKeyword(oSel);
			oThis.value = oSel.keyword;
		}
	}
}

function SetRecommendKeyword(oSel)
{
	oSel.style.backgroundColor = "#f6f6f6";
}

function ReleaseRecommendKeyword(oSel)
{
	oSel.style.backgroundColor = "#ffffff";
}

function GetRecommendKeyword(oThis)
{
	var keyword = oThis.value;
	var args = 'action=recommend_keyword_list&keyword='+keyword;
	var oAJAX = new AJAX();
	oAJAX.Request('/process_recommend_keyword.php', DisRecommendKeywordList, args);
}

function DisRecommendKeywordList(data)
{
	//alert(data);
	//return;

	var oListSec = document.getElementById("recommend_keyword_sec");
	if(oListSec == null) return false;
	oListSec.innerHTML = '';
	oListSec.oSel = null;

	var JSON = eval('(' + data + ')');

	if(JSON.result == 'err')
	{
		alert(JSON.result_str);
		return;
	}
	
	var total_record = JSON.list.length;			
	if(total_record > 0)
	{
		oListSec.style.display = "block";
		var oList = null;
		for(i=0 ; i<total_record ; i++)
		{
			oList = CreateRecommendKeywordList(JSON.list[i]);
			
			oListSec.insertAdjacentElement("beforeEnd",oList);
		}
	}
	else 
	{
		oListSec.style.display = "none";
	}
}

function CreateRecommendKeywordList(oInfo)
{
	var oList = document.createElement('li');
	oList.keyword = oInfo.keyword;
	oList.style.padding = "5px 0 2px 0";
	oList.style.textAlign = "left";
	oList.style.cursor = "pointer";
	oList.attachEvent("onclick", function(e){SeachForRecommendKeyword(oList);} );
	oList.attachEvent("onmouseover", function(e){oList.style.background='#f6f6f6';} );
	oList.attachEvent("onmouseout", function(e){oList.style.background='#FFFFFF';} );

	var oKeywordSec = document.createElement('span');
	oKeywordSec.style.display = "inline-block";
	oKeywordSec.style.verticalAlign = "middle";
	oKeywordSec.style.marginLeft = "5px";
	oKeywordSec.innerHTML = oInfo.keyword;
	oList.insertAdjacentElement("beforeEnd",oKeywordSec);
	
	return oList;	
}

function SeachForRecommendKeyword(oSel)
{
	var oListSec = document.getElementById("recommend_keyword_sec");
	oListSec.oInputKeyword.value = oSel.keyword;

	HideRecommendKeywordList();
	Search();
}

function HideRecommendKeywordList()
{
	try
	{
		var oListSec = document.getElementById("recommend_keyword_sec");
		oListSec.style.display = "none";
	}
	catch (e)
	{
	}
}
