// #################
// ### VARIABLES ###
// #################

var itemHeight = 20;
var oneFontWidth = 5;
var freeSpace = 8;
var arrowWidth = 15;
var itemBorderColor = "#000000";
var itemBgC = "#000000";
var itemBgCo = "#7e3a6e";
var mainLevelSpacing = 0;
var menuHoffset = -5;
var menuVoffset = 5;
var keepAlive = 1000;

// ########################
// ### END OF VARIABLES ###
// ########################

// #############
// ### TOOLS ###
// #############

function sBrowserTest() {
	this.IE = false;
	this.GECKO = false;
	this.NS4 = false;
	this.OPERA = false;

	if(document.getElementById && document.all) this.IE = true;
		else if(document.getElementById && !document.all) this.GECKO = true;
			else if(document.layers) this.NS4 = true;
				else if(window.opera) this.OPERA = true;
}

sBr = new sBrowserTest;

function initsLay(t, l) {
	if(sBr.IE || sBr.GECKO)	{
		container = document.createElement("DIV");
		container.style.position = "absolute";
		container.w = sWidth;
		container.h = sHeight;
	}

	if(sBr.NS4) {
		container = new Layer(10000);
		container.w = sWidth;
		container.w(10000);
		container.h = sHeight;
		container.h(10000);
	}

	container.t = sTop;
	container.t(t);
	container.l = sLeft;
	container.l(l)
	container.v = sVis;
	container.v(1);

	if(sBr.IE || sBr.GECKO) document.body.appendChild(container);	
}

sL = new Array();

function sMakeLay(ID, top, left, width, height, vis, bgc) {

	if(sBr.IE || sBr.GECKO)	{
		sL[ID] = document.createElement("DIV");
		sL[ID].style.position = "absolute";
	}

	if(sBr.NS4) {
		sL[ID] = new Layer(width, container);
	}

	sL[ID].t = sTop;
	sL[ID].t(top);
	sL[ID].l = sLeft;
	sL[ID].l(left)
	sL[ID].w = sWidth;
	sL[ID].w(width);
	sL[ID].h = sHeight;
	sL[ID].h(height);
	sL[ID].v = sVis;
	sL[ID].v(vis);
	sL[ID].bgc = sBgc;
	sL[ID].bgc(bgc);
	sL[ID].txt = sTxt;

	if(sBr.IE || sBr.GECKO) container.appendChild(sL[ID]);
}

function sTop(pxs) {
	if(sBr.IE || sBr.GECKO) {
		if(!isNaN(pxs)) this.style.top = pxs;
			else return parseInt(this.style.top);
	}

	if(sBr.NS4) {
		if(!isNaN(pxs)) this.top = pxs;
			else return parseInt(this.top);
	}
}

function sLeft(pxs) {
	if(sBr.IE || sBr.GECKO) {
		if(!isNaN(pxs)) this.style.left = pxs;
			else return parseInt(this.style.left);
	}

	if(sBr.NS4) {
		if(!isNaN(pxs)) this.left = pxs;
			else return parseInt(this.left);
	}
}

function sWidth(pxs) {
	if(sBr.IE || sBr.GECKO) {
		if(!isNaN(pxs)) this.style.width = pxs;
			else return parseInt(this.style.width);
	}

	if(sBr.NS4) {
		if(!isNaN(pxs)) {
			this.width = pxs;
			this.clip.width = pxs;
		}
		else return parseInt(this.width);
	}
}

function sHeight(pxs) {
	if(sBr.IE || sBr.GECKO) {
		if(!isNaN(pxs)) this.style.height = pxs;
			else return parseInt(this.style.height);
	}

	if(sBr.NS4) {
		if(!isNaN(pxs)) {
			this.height = pxs;
			this.clip.height = pxs;
		}
		else return parseInt(this.height);
	}
}

function sVis(val) {
	if(sBr.IE || sBr.GECKO) {
		if(val == 1) this.style.visibility = "visible";
			else if(val == 0) this.style.visibility = "hidden";
				else return this.style.visibility;
	}

	if(sBr.NS4) {
		if(val == 1) this.visibility = "show";
			else if(val == 0) this.visibility = "hide";
				else return this.visibility;
	}
}

function sBgc(bgc) {
	if(sBr.IE || sBr.GECKO) {
		if(bgc) this.style.backgroundColor = bgc;
			else return this.style.backgroundColor;
	}

	if(sBr.NS4) {
		if(bgc) this.bgColor = bgc;
			else return this.bgColor;
	}
}

function sTxt(str) {
	if(sBr.IE || sBr.GECKO) {
		if(str) this.innerHTML = str;
			else return this.innerHTML;
	}

	if(sBr.NS4) {
		if(str) {
			this.document.open();
			this.document.write(str);
			this.document.close();
		}
	}
}

function sContToElm(elmID) {
	if(sBr.GECKO) {
		var xpos = eval('document.' + elmID + '.offsetLeft');
		var ypos = eval('document.' + elmID + '.offsetTop');
	}

	if(sBr.NS4) {
		var xpos = eval('document.' + elmID + '.x');
		var ypos = eval('document.' + elmID + '.y');
	}

	if (sBr.IE) {
		var xpos = 0;
		var ypos = 0;

		offObj = eval('document.' + elmID);
		while(1) {
			if (offObj.nodeName == "BODY") {
				break;				
			}
			else {
				if(offObj.nodeName != "TR") {
					xpos += offObj.offsetLeft;
					ypos += offObj.offsetTop;
				}
				offObj = offObj.parentElement;
			}
		}
	}

	container.l(xpos);
	container.t(ypos);
}

// ####################
// ### END OF TOOLS ###
// ####################

var killall;
var clkDisallow;
var actObj = 0;

var innerbStrArrow = '<table cellspacing=0 cellpadding=0 border=0 width=100%><tr><td class=mItemCl>&nbsp;" + sMenu[i][2] + "</td><td align=right><img src=/gfx/sMoreArrow.gif width=" + arrowWidth + " height=" + arrowWidth + "></td></tr></table>';
var innerbStr = '&nbsp;" + sMenu[i][2] + "';

var bStr0a = '"<table cellspacing=0 cellpadding=0 border=0 width=" + iWidth +"><tr><td rowspan=2 width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td><td height=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td><td rowspan=2 width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td></tr><tr><td height=" + (itemHeight-1) + " width=" + (iWidth-2) + " class=mItemCl>';
var bStr0b = '</td></tr></table>"';
var bStr1a = '"<table cellspacing=0 cellpadding=0 border=0 width=" + iWidth +"><tr><td width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td><td height=" + itemHeight + " width=" + (iWidth-2) + " class=mItemCl>';
var bStr1b = '</td><td width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td></tr></table>"';
var bStr2a = '"<table cellspacing=0 cellpadding=0 border=0 width=" + iWidth +"><tr><td rowspan=2 width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td><td height=" + (itemHeight-1) + " width=" + (iWidth-2) + " class=mItemCl>';
var bStr2b = '</td><td rowspan=2 width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td></tr><tr><td height=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td></tr></table>"';
var bStr3a = '"<table cellspacing=0 cellpadding=0 border=0 width=" + iWidth +"><tr><td rowspan=3 width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td><td height=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td><td rowspan=3 width=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td></tr><tr><td height=" + (itemHeight-2) + " width=" + (iWidth-2) + " class=mItemCl>';
var bStr3b = '</td></tr><tr><td height=1 bgcolor=" + itemBorderColor + "><img src=/gfx/t_dot.gif border=0 width=1 height=1></td></tr></table>"';
var bStr4a = '"<table cellspacing=0 cellpadding=0 border=0 width=" + iWidth +"><tr><td height=" + itemHeight + " width=" + iWidth + " class=mItemCl>';
var bStr4b = '</td></tr></table>"';

function sParseMenu() {
	var actID = 0;
	var actREF = 0;
	var lastID = 0;
	var lastREF = 0;

	var maxLen = new Array();
	var refCount = new Array();
	var maxLevel = 0;

	for(var i=0; i<sMenu.length; i++) {
		actREF = sMenu[i][1];

		if(isNaN(maxLen[actREF]) || maxLen[actREF] < sMenu[i][2].length) maxLen[actREF] = sMenu[i][2].length;

		if(isNaN(refCount[actREF])) refCount[actREF] = 1;
			else refCount[actREF] += 1;
	}

	for(var i=0; i<sMenu.length; i++) {
		actID = sMenu[i][0];
		sMakeLay(actID, 0, 0, 0, itemHeight, 0, itemBgC);
		sL[actID].ID = actID;
		sL[actID].LINK = sMenu[i][3];
		sL[actID].MORE = false;
		sL[actID].onmouseover = mover;
		sL[actID].onmouseout = mout;
	}

	for(var i=0; i<sMenu.length; i++) {
		actREF = sMenu[i][1];		
		actID = sMenu[i][0];
		if(i != 0) {
			lastREF = sMenu[i-1][1];		
			lastID = sMenu[i-1][0];
		}

		if(actREF == 0) {
			sL[actID].REF = 0;
			sL[actID].LEVEL = 0;
			sL[actID].POS = 4;
		}

		if(actREF != 0) {
			sL[actID].REF = actREF;
			sL[actID].LEVEL = sL[actREF].LEVEL + 1;

			if(actREF != lastREF) {
				if(lastREF == 0) sL[actID].POS = 0;

				if(lastREF != 0) {
					if(refCount[lastREF] != 1) {
						sL[actID].POS = 0;
						sL[lastID].POS = 2;
					}

					if(refCount[lastREF] == 1) {
						sL[actID].POS = 0;
						sL[lastID].POS = 3;
					}

					if(i == sMenu.length-1) {
						if(refCount[actREF] == 1) sL[actID].POS = 3;
							else sL[actID].POS = 2;
					}
				}
			}
			else {
				if(i == sMenu.length-1) {
					if(refCount[actREF] != 1) sL[actID].POS = 2;
				}
				else sL[actID].POS = 1;
			}
		}
	}

	for(var i=0; i<sMenu.length; i++) {
		actID = sMenu[i][0];

		for(var l=0; l<sMenu.length; l++) {
			if(sMenu[l][1] == actID) {
				sL[actID].MORE = true;
				l = sMenu.length;
			}
		}

		if(sL[actID].LEVEL > maxLevel) maxLevel = sL[actID].LEVEL;

	}

	var iWidth,leftPos;
	var itemCount = 0;

	for(var i=0; i<sMenu.length; i++) {
		actID = sMenu[i][0];
		actREF = sL[actID].REF;
		if(i != 0) lastID = sMenu[i-1][0];

		if(sL[actID].LEVEL == 0) {
			iWidth = parseInt((sMenu[i][2].length*oneFontWidth) + freeSpace + arrowWidth);
			sL[actID].w(iWidth);

			if(i == 0) leftPos = 0;
				else leftPos = sL[lastID].l() + sL[lastID].w() + mainLevelSpacing;

			sL[actID].l(leftPos);
			if(sL[actID].MORE) sL[actID].txt(eval(bStr4a+innerbStrArrow+bStr4b));
				else sL[actID].txt(eval(bStr4a+innerbStr+bStr4b));
		}
	}

	for(var i=0; i<sMenu.length; i++) {
		actID = sMenu[i][0];
		actREF = sMenu[i][1];
		if(i != 0) {
			lastID = sMenu[i-1][0];
			lastREF = sMenu[i-1][1];
		}

		if(sL[actID].LEVEL == 1) {
			if(actREF != lastREF) itemCount = 1;
				else itemCount ++;

			iWidth = parseInt((maxLen[actREF]*oneFontWidth) + freeSpace + arrowWidth);
			sL[actID].w(iWidth);
			sL[actID].l(sL[actREF].l());
			sL[actID].t(itemHeight * itemCount);

			var bStrA = eval("bStr" + sL[actID].POS + "a");
			var bStrB = eval("bStr" + sL[actID].POS + "b");

			if(sL[actID].MORE) sL[actID].txt(eval(bStrA+innerbStrArrow+bStrB));
				else sL[actID].txt(eval(bStrA+innerbStr+bStrB));
		}
	}

	itemCount = 0;

	for(var l=2; l<=maxLevel; l++) {
		var actLEVEL = l;

		for(var i=0; i<sMenu.length; i++) {
			actID = sMenu[i][0];
			actREF = sMenu[i][1];
			if(i != 0) {
				lastID = sMenu[i-1][0];
				lastREF = sMenu[i-1][1];
			}

			if(sL[actID].LEVEL == actLEVEL) {
				if(actREF != lastREF) itemCount = 0;
					else itemCount ++;

				iWidth = parseInt((maxLen[actREF]*oneFontWidth) + freeSpace + arrowWidth);
				sL[actID].w(iWidth);
				sL[actID].l(sL[actREF].l() + sL[actREF].w() + menuHoffset);
				sL[actID].t(sL[actREF].t() + (itemHeight * itemCount) + menuVoffset);

			var bStrA = eval("bStr" + sL[actID].POS + "a");
			var bStrB = eval("bStr" + sL[actID].POS + "b");

			if(sL[actID].MORE) sL[actID].txt(eval(bStrA+innerbStrArrow+bStrB));
				else sL[actID].txt(eval(bStrA+innerbStr+bStrB));
			}
		}
	}
}

function showLevel(ID, LEVEL) {
	clearTimeout(killall);

	for(var i=0; i<sMenu.length; i++) {
		var actID = sMenu[i][0];

		if(sL[actID].REF == ID) {
			sL[actID].v(1);
		}
		else if(sL[actID].REF != ID && sL[actID].LEVEL > LEVEL) {
			sL[actID].v(0);
		}
	}
}

function hideAll() {
	for(var i=0; i<sMenu.length; i++) {
		var actID = sMenu[i][0];

		if(sL[actID].LEVEL != 0) sL[actID].v(0);
	}
}

function mover(e) {
	if(sBr.GECKO) {
		obj = e.target;
		while(1) {
			if(obj.nodeName == "DIV") break;
				else obj = obj.parentNode;
		}
	}

	if(sBr.IE) {
		obj = event.srcElement;
		while(1) {
			if(obj.nodeName == "DIV") break;
				else obj = obj.parentNode;
		}
	}

	if(sBr.NS4) {
		obj = e.target;
	}

	clearTimeout(clkDisallow);
	actObj = obj.LINK;
	obj.bgc(itemBgCo);
	showLevel(obj.ID, obj.LEVEL);
}

function mout(e) {
	if(sBr.GECKO) {
		obj = e.target;
		while(1) {
			if(obj.nodeName == "DIV") break;
				else obj = obj.parentNode;
		}
	}

	if(sBr.IE) {
		obj = event.srcElement;
		while(1) {
			if(obj.nodeName == "DIV") break;
				else obj = obj.parentNode;
		}
	}

	if(sBr.NS4) {
		obj = e.target;
	}

	obj.bgc(itemBgC);
	killall = setTimeout('hideAll()', keepAlive);
	clkDisallow = setTimeout('actObj=0',50);
}

if(sBr.NS4) document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = mclick;

function mclick(e) {
	if(sBr.NS4 || sBr.GECKO) {
		if(e.which == 1 && actObj) self.location = actObj;
	}

	if(sBr.IE) {
		if(event.button == 1 && actObj) self.location = actObj;
	}
}

function init() {
	initsLay(0,0);
	sParseMenu();
	sContToElm("tracer");

	for(var i=0; i<sMenu.length; i++) {
		var actID = sMenu[i][0];
		if(sL[actID].LEVEL == 0) sL[actID].v(1);
	}
}

