var parentCategoryId;
//var parentCategoryName;

function loadDataFile(categoryId)
{
	new Ajax.Request('httpCon.php?cid='+categoryId, { method: 'get', onComplete: displayData });
}
function displayData(httpObj)
{
	var resultDiv = document.getElementById("result");
	var div = document.createElement("div");
	with(div.style) {
		width = "100%";
		lineHeight = "140%";
		marginTop = "10";
	}
	
	var xml = httpObj.responseXML;
	var childCategory = xml.getElementsByTagName("childCategory");
	
		//表示
	div.appendChild(createBackDiv("back"+parentCategoryId));
	for (var i=0; i < childCategory.length; i++){
		div.appendChild(createCategoryDownSpan(childCategory[i].getElementsByTagName("categoryName")[0].firstChild.nodeValue, "keywordDownLevel"+i));
		div.appendChild(createKeywordAddSpan("keyword"+i, childCategory[i].getElementsByTagName("categoryId")[0].firstChild.nodeValue));
		div.appendChild(document.createTextNode(" "));
	}
	
	if(resultDiv.childNodes.item(0)) resultDiv.removeChild(resultDiv.childNodes.item(0));
	resultDiv.appendChild(div);
	
		//動作
	actCategoryBack(parentCategoryId);
	for (var i=0; i<childCategory.length; i++){
		actSelectKeyword(i);
		actToDownLevelCategory(i, childCategory[i].getElementsByTagName("categoryId")[0].firstChild.nodeValue);
	}
	
	parentCategoryId = xml.getElementsByTagName("categoryId")[0].firstChild.nodeValue;

}

function createBackDiv(categoryId)
{
	var categoryBackDiv = document.createElement("div");
	categoryBackDiv.id = categoryId;
	with(categoryBackDiv.style){
		cursor = 'pointer';
		textDecoration = 'underline';
	}
	
	categoryBackDiv.appendChild(document.createTextNode('戻る'));
	return categoryBackDiv;
}
function createCategoryDownSpan(keyword, keywordId)
{
	var categoryDownSpan = document.createElement("span");
	categoryDownSpan.id = keywordId;
	with(categoryDownSpan.style){
		cursor = 'pointer';
		textDecoration = 'underline';
	}
	
	keyword = keyword.replace(/、/g, " ");
	categoryDownSpan.appendChild(document.createTextNode(keyword));
	return categoryDownSpan;
}
function createKeywordAddSpan(keywordId, categoryId)
{
	var keywordSpan = document.createElement("span");
	keywordSpan.id = keywordId;
	with(keywordSpan.style){
		cursor = "pointer";
	}
	keywordSpan.appendChild(document.createTextNode('＋'+categoryId));		//+以降のノード要素はdbg
	return keywordSpan;
}

function actSelectKeyword(keywordId)
{
	var objKeyword = document.getElementById('keyword'+keywordId);
	var objDownLevel = document.getElementById('keywordDownLevel'+keywordId);
	
	objKeyword.onmousedown = function()
	{
		addKeywordFormInput(objDownLevel.firstChild.nodeValue);
	};
}
function actToDownLevelCategory(keywordId, categoryId)
{
	var objToDownLevel = document.getElementById('keywordDownLevel'+keywordId);
	
	objToDownLevel.onmousedown = function()
	{
		loadDataFile(categoryId);
	}
}
function actCategoryBack(categoryId)
{
	var objCategoryBack = document.getElementById('back'+categoryId);
	
	objCategoryBack.onmousedown = function()
	{
		loadDataFile(categoryId);
	}
}

function addKeywordFormInput(keywordName)
{
	keywordInput = document.getElementById("keyword");
	keywordInput.value.match(/(\[.+\])(.*)/);
	if (!keywordInput.value)
		keywordInput.value = keywordName;
	else {
		keywordInput.value = keywordInput.value+' '+keywordName;
	}
}

