//
// Description:	Tree menu javascript module
// File:		treeMenu.js
// Author:		Guy Fernando (Copyright Informatix Ltd)
// Date:		17/12/2001
//

function TreeMenu(
	name, x, y, width, padding, indent,
	mainNormBgColor, mainNormFgColor, mainOverBgColor, mainOverFgColor,
	mainFontFamily, mainFontStyle, mainFontWeight, mainFontSize, mainTextDecoration,
	itemNormBgColor, itemNormFgColor, itemOverBgColor, itemOverFgColor,
	itemFontFamily, itemFontStyle, itemFontWeight, itemFontSize, itemTextDecoration)
{
	this.name	 = name;
	this.x		 = x;
	this.y		 = y;
	this.width	 = width;
	this.padding = padding;
	this.indent	 = indent;
	this.zindex  = 10;
	
	this.mainNormBgColor = mainNormBgColor;
	this.mainOverBgColor = mainOverBgColor;
	this.itemNormBgColor = itemNormBgColor;
	this.itemOverBgColor = itemOverBgColor;
	
	var mainText =
		"font-family:" + mainFontFamily + ";" +
		"font-size:" + mainFontSize + ";" +
		"font-style:" + mainFontStyle + ";" +
		"font-weight:" + mainFontWeight + ";" +
		"text-decoration:" + mainTextDecoration + ";";
		
	var itemText =
		"font-family:" + itemFontFamily + ";" +
		"font-size:" + itemFontSize + ";" +
		"font-style:" + itemFontStyle + ";" +
		"font-weight:" + itemFontWeight + ";" +
		"text-decoration:" + itemTextDecoration + ";";

	this.mainNormText = "color:" + mainNormFgColor + ";" + mainText;
	this.mainOverText = "color:" + mainOverFgColor + ";" + mainText;
	this.itemNormText = "color:" + itemNormFgColor + ";" + itemText;
	this.itemOverText = "color:" + itemOverFgColor + ";" + itemText;

	this.tblBegin = '<table border="0" cellpadding="' + this.padding + '" cellspacing="0" width="100%"><tr><td>\n';
	this.tblEnd   = '</td></tr></table>\n';

	this.node = new Array();
}
TreeMenu.prototype = new TreeMenu;

TreeMenu.prototype.addItem = function treemenu_addItem(level, text, link, targetWnd)
{
	var idx = this.node.length;
	
	this.node[idx] = new Array();
	this.node[idx].level = level;
	this.node[idx].text = text;
	this.node[idx].link = link;

	if (targetWnd == null || targetWnd == "")
		this.node[idx].targetWnd = window;
	else
		this.node[idx].targetWnd = targetWnd;
}

TreeMenu.prototype.init = function init()
{
	if (isMinIE4 || isMinNS6)
		this.writeHtmlIE();
	else if (isMinNS4)
		this.writeHtmlNS();
	else
		return;

	this.positionLayers();
	this.repositionLayers();
}

TreeMenu.prototype.positionLayers = function treemenu_positionLayers()
{
	var i;
	var j = 0;
	var k = 0;
	var y = 0;
	var height = 0;
	var colapsedHeight = 0;
	var fullHeight = 0;

	var layer;

	for (i = 0; i < this.node.length; i++)
	{
		if (this.node[i].level == 0)
		{
			k = 0;
		
			layer = getLayer(this.name + "_mainNorm_" + j);
			height = getHeight(layer);
			colapsedHeight = fullHeight = height;
			
			moveLayerTo(layer, 0, 0);
			clipLayer(layer, 0, 0, this.width, height);
			
			layer = getLayer(this.name + "_mainOver_" + j);
			moveLayerTo(layer, 0, 0);
			clipLayer(layer, 0, 0, this.width, height);

			layer = getLayer(this.name + "_mainevent_" + j);
			moveLayerTo(layer, 0, 0);
			clipLayer(layer, 0, 0, this.width, height);
			
			layer.onmouseout = this.onmouseoutMain;
			layer.onmouseover = this.onmouseoverMain;
			layer.j = j;
			layer.menuObj = this;
		}
		
		if (this.node[i].level == 1)
		{
			layer = getLayer(this.name + "_itemNorm_" + j + "_" + k);
			height = getHeight(layer);
			fullHeight += height;

			moveLayerTo(layer, this.indent, colapsedHeight + height * k);
			clipLayer(layer, 0, 0, this.width - this.indent, height);
			showLayer(layer);
			
			layer = getLayer(this.name + "_itemOver_" + j + "_" + k);
			moveLayerTo(layer, this.indent, colapsedHeight + height * k);
			clipLayer(layer, 0, 0, this.width - this.indent, height);

			layer = getLayer(this.name + "_itemevent_" + j + "_" + k);
			moveLayerTo(layer, this.indent, colapsedHeight + height * k);
			clipLayer(layer, 0, 0, this.width - this.indent, height);
			
			layer.onmouseout = this.onmouseoutItem;
			layer.onmouseover = this.onmouseoverItem;
			
			if (isMinIE4 || isMinNS6)
			{
				layer.onmousedown = this.onmousedownItem;
				layer.link = this.node[i].link;
				layer.targetWnd = this.node[i].targetWnd;
			}
			else if (isMinNS4)
			{
				layer.document.onmousedown = this.onmousedownItem;
				layer.document.link = this.node[i].link;
				layer.document.targetWnd = this.node[i].targetWnd;
			}

			layer.j = j;
			layer.k = k;
			layer.menuObj = this;

			k++;
		}
		
		if (i == this.node.length-1 || this.node[i+1].level == 0)
		{
			layer = getLayer(this.name + "_block_" + j);
			layer.colapsedHeight = colapsedHeight;
			layer.fullHeight = fullHeight;
			moveLayerTo(layer, this.x, this.y + y);
			clipLayer(layer, 0, 0, this.width, fullHeight);
			setzIndex(layer, this.zindex);
			showLayer(layer);
			
			layer.onmouseout = this.onmouseoutBlock;
			layer.onmouseover = this.onmouseoverBlock;
			layer.j = j;
			layer.menuObj = this;
			layer.colapsed = true;
							
			j++;
			y += fullHeight;
		}
	}
}

TreeMenu.prototype.repositionLayers = function treemenu_repositionLayers()
{
	var i;
	var j = 0;
	var y = 0;
	var colapsedHeight = 0;
	var fullHeight = 0;

	var layer;
	
	for (i = 0; i < this.node.length; i++)
	{
		if (this.node[i].level == 0)
		{
			layer = getLayer(this.name + "_block_" + j);
			moveLayerTo(layer, this.x, this.y + y);
			
			if (layer.colapsed == true)
			{
				clipLayer(layer, 0, 0, this.width, layer.colapsedHeight);
				y += layer.colapsedHeight;
			}
			else
			{
				clipLayer(layer, 0, 0, this.width, layer.fullHeight);
				y += layer.fullHeight;
			}
			
			layer = getLayer(this.name + "_mainNorm_" + j);
			showLayer(layer);
		}
		
		if (i == this.node.length-1 || this.node[i+1].level == 0)
			j++;
	}
}

TreeMenu.prototype.writeHtmlIE = function treemenu_writeHtmlIE()
{
	var s = "";
	var i;
	var j = 0;
	var k = 0;
	
	for (i = 0; i < this.node.length; i++)
	{
		if (this.node[i].level == 0)
		{
			k = 0;
			s += '<div id="' + this.name + '_block_' + j + '"' +
				 ' style="position:absolute;' +
				 ' z-index:' + this.zindex + ';' +
				 ' width:' + this.width + ';' +
				 ' visibility:hidden;">\n';

			s += '<div id="' + this.name + '_mainNorm_' + j + '"' +
				 ' style="position:absolute;' +
				 ' background-color:' + this.mainNormBgColor + ';' +
				 ' z-index:' + this.zindex + ';' +
				 ' width:' + this.width + ';' +
				 ' visibility:hidden;">' + this.tblBegin +
				 '<span style="' + this.mainNormText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</div>\n';
			s += '<div id="' + this.name + '_mainOver_' + j + '"' +
				 ' style="position:absolute;' +
				 ' background-color:' + this.mainOverBgColor + ';' +
				 ' width:' + this.width + ';' +
				 ' z-index:' + this.zindex + ';' +
				 ' visibility:hidden;">' + this.tblBegin +
				 '<span style="' + this.mainOverText + '">' + this.node[i].text +
				 '</span>\n' + this.tblEnd +
				 '</div>';
			s += '<div id="' + this.name + '_mainevent_' + j + '"' +
				 ' style="position:absolute;' +
				 (isMinIE6 ? ' background-color:white; filter:alpha(opacity=0);' : '') + 
				 ' z-index:' + this.zindex + ';' +
				 ' width:' + this.width + '; height:1024;">' +
				 '</div>\n';
		}

		if (this.node[i].level == 1)
		{
			s += '<div id="' + this.name + '_itemNorm_' + j + '_' + k + '"' +
				 ' style="position:absolute;' +
				 ' background-color:' + this.itemNormBgColor + ';' +
				 ' z-index:' + this.zindex + ';' +
				 ' width:' + this.width + ';' +
				 ' visibility:hidden;">' + this.tblBegin +
				 '<span style="' + this.itemNormText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</div>\n';
			s += '<div id="' + this.name + '_itemOver_' + j + '_' + k + '"' +
				 ' style="position:absolute;' +
				 ' background-color:' + this.itemOverBgColor + ';' +
				 ' z-index:' + this.zindex + ';' +
				 ' width:' + this.width + ';' +
				 ' visibility:hidden;">' + this.tblBegin +
				 '<span style="' + this.itemOverText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</div>\n';
			s += '<div id="' + this.name + '_itemevent_' + j + '_' + k + '"' +
				 ' style="position:absolute; cursor:hand; ' +
				 (isMinIE6 ? ' background-color:white; filter:alpha(opacity=0);' : '') + 
				 ' z-index:' + this.zindex + ';' +
				 ' width:' + this.width + '; height:1024;">' +
				 '</div>\n';
				 
			k++;
		}

		if (i == this.node.length-1 || this.node[i+1].level == 0)
		{
			s += '</div>\n';
			j++;
		}
	}
	
	if (isMinIE4)
		document.body.insertAdjacentHTML("beforeEnd", s);
	else if (isMinNS6)
	{
		var r = document.createRange();
		r.setStartBefore(document.body);
		var html = r.createContextualFragment(s);
		document.body.appendChild(html);
	}
}

TreeMenu.prototype.writeHtmlNS = function treemenu_writeHtmlNS()
{
	var s = "";
	var i;
	var j = 0;
	var k = 0;

	s += this.tblBegin + '\n';
				 
	for (i = 0; i < this.node.length; i++)
	{
		if (this.node[i].level == 0)
		{
			k = 0;
				 
			s += '<layer name="' + this.name + '_block_' + j + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width="' + this.width + '"' +
				 ' visibility="hidden">\n' +
				 '<span style="' + this.mainNormText + '"></span>';
				 
			s += '<layer name="' + this.name + '_mainNorm_' + j + '"' +
				 ' bgcolor="' + this.mainNormBgColor + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width="' + this.width + '"' +
				 ' visibility="hidden">' + this.tblBegin +
				 '<span style="' + this.mainNormText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</layer>\n';
			s += '<layer name="' + this.name + '_mainOver_' + j + '"' +
				 ' bgcolor="' + this.mainOverBgColor + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width="' + this.width + '"' +
				 ' visibility="hidden">' + this.tblBegin +
				 '<span style="' + this.mainOverText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</layer>';
			s += '<layer name="' + this.name + '_mainevent_' + j + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width="' + this.width + '">' +
				 '</layer>\n'; 
		}

		if (this.node[i].level == 1)
		{
			s += '<layer name="' + this.name + '_itemNorm_' + j + '_' + k + '"' +
				 ' bgcolor="' + this.itemNormBgColor + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width="' + this.width + '"' +
				 ' visibility="hidden">' + this.tblBegin +
				 '<span style="' + this.itemNormText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</layer>\n';
			s += '<layer name="' + this.name + '_itemOver_' + j + '_' + k + '"' +
				 ' bgcolor="' + this.itemOverBgColor + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width=' + this.width +
				 ' visibility="hidden">' + this.tblBegin +
				 '<span style="' + this.itemOverText + '">' + this.node[i].text +
				 '</span>' + this.tblEnd +
				 '</layer>\n';
			s += '<layer name="' + this.name + '_itemevent_' + j + '_' + k + '"' +
				 ' z-index="' + this.zindex + '"' +
				 ' width="' + this.width + '">' +
				 '</layer>\n';
				 
			k++;
		}

		if (i == this.node.length-1 || this.node[i+1].level == 0)
		{
			s += '</layer>\n';
			j++;
		}
	}
	s += this.tblEnd + '\n';

	document.write(s);
}

TreeMenu.prototype.onmouseoverBlock = function treemenu_onmouseoverBlock()
{
	this.colapsed = false;
	this.menuObj.repositionLayers();
}

TreeMenu.prototype.onmouseoutBlock = function treemenu_onmouseoutBlock()
{
	this.colapsed = true;
	this.menuObj.repositionLayers();
}

TreeMenu.prototype.onmouseoverMain = function treemenu_onmouseoverMain()
{
	showLayer(getLayer(this.menuObj.name + "_mainOver_" + this.j));
	hideLayer(getLayer(this.menuObj.name + "_mainNorm_" + this.j));
}

TreeMenu.prototype.onmouseoutMain = function treemenu_onmouseoutMain()
{
	showLayer(getLayer(this.menuObj.name + "_mainNorm_" + this.j));
	hideLayer(getLayer(this.menuObj.name + "_mainOver_" + this.j));
}

TreeMenu.prototype.onmouseoverItem = function treemenu_onmouseoverItem()
{
	showLayer(getLayer(this.menuObj.name + "_itemOver_" + this.j + "_" + this.k));
	hideLayer(getLayer(this.menuObj.name + "_itemNorm_" + this.j + "_" + this.k));
}

TreeMenu.prototype.onmouseoutItem = function treemenu_onmouseoutItem()
{
	showLayer(getLayer(this.menuObj.name + "_itemNorm_" + this.j + "_" + this.k));
	hideLayer(getLayer(this.menuObj.name + "_itemOver_" + this.j + "_" + this.k));
}

TreeMenu.prototype.onmousedownItem = function treemenu_onmousedownItem()
{
	if (this.link.indexOf("javascript:") == 0)
		eval(this.link);
	else if (this.link.length == 0)
		window.location.href = "#"
	else
		this.targetWnd.location.href = this.link;
}
