/********************************************************************/
/* XMLMenu */

function XMLMenu(base, current)
{
	this.xml = createXMLDocument(base + "Menu.xml");
	this.xsl = createXMLDocument(base + "Menu.xsl");

	var node = node = selectSingleNode(this.xsl,
		'/xsl:stylesheet/xsl:param[@name = "base"]');

	node.setAttribute("select", "'" + base + "'");

	node = selectSingleNode(this.xsl,
		'/xsl:stylesheet/xsl:param[@name = "current"]');

	node.setAttribute("select", "'" + current + "'");
}

XMLMenu.prototype.update = function()
{
	var node = document.getElementById("xmlMenu");

	transformXMLToTarget(this.xml, this.xsl, node);
}

/********************************************************************/
/* XMLBody */

function XMLBody(xml, xsl)
{
	this.xml = createXMLDocument(xml);
	this.xsl = createXMLDocument(xsl);

	var labelNode = selectSingleNode(this.xsl,
		"/xsl:stylesheet/xsl:param[@name = 'label']");

	if (labelNode)
	{
		var label = labelNode.getAttribute("select");

		this.node = selectSingleNode(this.xsl,
			"//xsl:apply-templates[@select = " + label + "]");

		if (this.node)
			this.name = this.node.getAttribute("select");

		this.prevNode = null;
	}
}

XMLBody.prototype.update = function()
{
	var node = document.getElementById("xmlBody");

	transformXMLToTarget(this.xml, this.xsl, node);
}

XMLBody.prototype.updateTable = function()
{
	var node = document.getElementById("xmlTable");

	transformXMLToTarget(this.xml.documentElement, this.xsl, node);
}

XMLBody.prototype.sort = function(tag, key, type)
{
	var descending = false;

	if (document.all && event.ctrlKey)
		descending = !descending;

	this.sortXSL(tag, key, type, descending);
	this.updateTable();

	return false;
}

XMLBody.prototype.sortXSL = function(tag, key, type, descending)
{
	var sortNodes = selectNodes(this.node, "xsl:sort");

	var pos = sortNodes.length - 1;

	{
		// キーの現在位置を取得する.

		for (var i = 0; i < sortNodes.length; i++)
		{
			var sortNode = sortNodes.item(i);

			if (sortNode.getAttribute("select") == key)
			{
				pos = i;

				break;
			}
		}
	}

	{
		// 前にあるキーを後ろに下げる.

		for (var i = pos; i > 0; i--)
		{
			var sortNode0 = sortNodes.item(i);
			var sortNode1 = sortNodes.item(i - 1);

			sortNode0.setAttribute("select", sortNode1.getAttribute("select"));
			sortNode0.setAttribute("data-type", sortNode1.getAttribute("data-type"));
			sortNode0.setAttribute("order", sortNode1.getAttribute("order"));
		}
	}

	var order = descending ? "descending" : "ascending";

	{
		// 先頭にキーを追加する.

		sortNodes.item(0).setAttribute("select", key);
		sortNodes.item(0).setAttribute("data-type", type);
		sortNodes.item(0).setAttribute("order", order);
	}

	var className = descending ?
		"xmlSortDescending" : "xmlSortAscending";

	var node = selectSingleNode(
		this.xsl, "//a[@id = '" + tag.id + "']");

	if (node)
		node.setAttribute("class", className);

	if (this.prevNode)
		this.prevNode.setAttribute("class", "xmlSort");

	this.prevNode = node;
}

XMLBody.prototype.setFilter = function(filter)
{
	if (filter != "")
	{
		this.node.setAttribute("select",
			this.name + "[" + filter + "]");
	}
	else
	{
		this.node.setAttribute("select", this.name);
	}

	this.updateTable();
}

/********************************************************************/

function init(base, current, xml, xsl)
{
	createPage();
	createMenu(base, current);
	createBody(xml, xsl);
}

function createPage()
{
	var node = document.getElementById("xmlPage");

	node.innerHTML =

		'<table class="maxWidth" border="0" cellspacing="4" cellpadding="4">' +
			'<tr>' +
				'<td class="nowrap" valign="top">' +
					'<div id="xmlMenu"></div>' +
				'</td>' +
				'<td class="maxWidth nowrap" valign="top">' +
					'<div id="xmlBody"></div>' +
				'</td>' +
			'</tr>' +
		'</table>';
}

var xmlMenu = null;

function createMenu(base, current)
{
	xmlMenu = new XMLMenu(base, current);
	xmlMenu.update();
}

var xmlBody = null;

function createBody(xml, xsl)
{
	xmlBody = new XMLBody(xml, xsl);
	xmlBody.update();
}

/********************************************************************/

function switchVisibility(id)
{
	var obj = document.getElementById(id);

	if (obj.style.display == "none")
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

/********************************************************************/
