var TabGroupList = new Array;
var nTapGroups = 0;

function createTabGroupIndex()
{
	this.groupID = nTapGroups;

  	TabGroupList[nTapGroups] = this;
  	nTapGroups++;

}

function TabGroup(groupName, targetFrame, height)
{
	this.groupID = -1;
	this.groupName = trim(groupName);
	this.targetFrame = targetFrame;
	this.menuHieght = height;
	this.nChild = 0;
	this.child = new Array;

	this.initialize = initializeTabGroup;
	this.createIndex = createTabGroupIndex;
	this.addChild = addChild;
	this.draw = drawGroup;

	this.recentMenuID = 0;

	this.initialize();
}

function initializeTabGroup()
{
	this.createIndex();
}

function addChild(childNode)
{
	childNode.menuID = this.nChild;

	this.child[this.nChild] = childNode;
	this.nChild++;

	return childNode;
}

function drawGroup()
{
	var i = 0;

	for (i = 0; i < this.nChild; i++) {
		this.child[i].draw(this, "norm_");
		this.child[i].draw(this, "active_");
	}
}

function addTabGroup(groupName, targetName, height)
{
	var node = new TabGroup(groupName, targetName, height);

	return node;
}

function TabMenu(name, path, func, width)
{
	this.menuID = -1;
	this.menuName = trim(name);
	this.menuPath = trim(path);
	this.funcName = func;
	this.menuWidth = width;
	this.isUrl = false;
	this.isActive = false;
	this.isSync = false;
	this.syncGroupID = -1;
	this.syncMenuID = -1;

	this.initialize = initializeTabMenu;
	this.sync = syncTabMenu;
	this.draw = drawTabMenu;

	this.initialize();
}

function initializeTabMenu()
{
	if (this.menuPath.indexOf(".") >= 0)
		this.isUrl = true;
}

function syncTabMenu(syncGroupID, syncMenuID)
{
	this.isSync = true;
	this.syncGroupID = syncGroupID;
	this.syncMenuID = syncMenuID;
}

function drawTabMenu(parentNode, cssPreName)
{
	var str = "";
	var idName = "tab_" + cssPreName + parentNode.groupID + "_" + this.menuID;
	var cssSpaceName = "space";
	var cssLineName = cssPreName + "line";
	var cssBgName = cssPreName + "bg";
	var cssMenuName = cssPreName + "menu";
	var menuWidth = this.menuWidth;
	var menuHeight = parentNode.menuHieght;
	var cssBottomLine = "active_line";
	var cssBottomSpace = cssBottomLine;

	if (cssPreName == "active_")
	{
		menuHeight += 2;
		cssBottomLine = "active_bg";
		cssBottomSpace = cssSpaceName;
	}

	str = "<table cellpadding=0 cellspacing=0 id=\"" + idName + "\" style=\"display:none;\""
	    + " onClick=\"activeTabPage(" + parentNode.groupID + ", " + this.menuID + ")\">\n"
	    + "	<tr height=1>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssLineName + "\" width=" + (menuWidth - 8) + "></td>\n"
	    + "		<td class=\"" + cssLineName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "	</tr>\n"
	    + "	<tr height=1>\n"
	    + "		<td colspan=2 class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssLineName + "\"></td>\n"
	    + "		<td colspan=2 class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssLineName + "\"></td>\n"
	    + "		<td colspan=3 class=\"" + cssSpaceName + "\"></td>\n"
	    + "	</tr>\n"
	    + "	<tr height=1>\n"
	    + "		<td colspan=1 class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssLineName + "\"></td>\n"
	    + "		<td colspan=2 class=\"" + cssBgName + "\"></td>\n"
	    + "		<td colspan=2 class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssLineName + "\"></td>\n"
	    + "		<td colspan=2 class=\"" + cssSpaceName + "\"></td>\n"
	    + "	</tr>\n"
	    + "	<tr height=1>\n"
	    + "		<td colspan=1 class=\"" + cssLineName + "\"></td>\n"
	    + "		<td colspan=5 class=\"" + cssBgName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssLineName + "\"></td>\n"
	    + "		<td colspan=1 class=\"" + cssSpaceName + "\"></td>\n"
	    + "	</tr>\n"
	    + "	<tr height=" + menuHeight + ">\n"
	    + "		<td colspan=6 class=\"" + cssMenuName + "\">" + this.menuName + "</td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "		<td class=\"" + cssLineName + "\"></td>\n"
	    + "		<td class=\"" + cssSpaceName + "\"></td>\n"
	    + "	</tr>\n"
	    + "	<tr height=1>\n"
	    + "		<td class=\"active_line\"></td>\n"
	    + "		<td colspan=5 class=\"" + cssBottomLine + "\"></td>\n"
	    + "		<td class=\"" + cssBottomSpace + "\"></td>\n"
	    + "		<td colspan=2 class=\"active_line\"></td>\n"
	    + "	</tr>\n"
	    + "</table>\n";

	document.write(str);
}

function addTabMenu(groupNode, name, path, func, width)
{
	var node = groupNode.addChild(new TabMenu(name, path, func, width));

	return node;
}

function activeTabPage(groupID, menuID)
{
	var i = 0;
	var parentNode = TabGroupList[groupID];
	var normIdName = "";
	var activeIdName = "";

	parentNode.recentMenuID = menuID;

	for (i = 0; i < parentNode.nChild; i++)
	{
		normIdName = "tab_norm_" + parentNode.groupID + "_" + i;
		activeIdName = "tab_active_" + parentNode.groupID + "_" + i;

		document.all[activeIdName].style.display = "none";
		document.all[normIdName].style.display = "inline";
		parentNode.child[i].isActive = false;

		if (parentNode.child[i].isUrl == false)
		{
			document.all[parentNode.child[i].menuPath].style.display = "none";
		}

		if (i == menuID)
		{
			document.all[normIdName].style.display = "none";
			document.all[activeIdName].style.display = "inline";
			parentNode.child[menuID].isActive = true;
		}
	}

	if (parentNode.child[menuID].isUrl == false)
	{
		document.all[parentNode.child[menuID].menuPath].style.display = "inline";

		if (parentNode.child[menuID].funcName != "")
		{
			eval(parentNode.child[menuID].funcName);
		}
	}
	else
	{
		var func = eval(parentNode.child[menuID].funcName);

		func(parentNode.child[menuID].menuPath, parentNode.targetFrame);

		if (parentNode.child[menuID].isSync == true)
		{
			var syncGroupID = parentNode.child[menuID].syncGroupID;
			var syncMenuID = parentNode.child[menuID].syncMenuID;

			activeTabPage(syncGroupID, syncMenuID);
		}
	}
}

