//-- CopyRight 2009 Paul Davis ---------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
		
function menuClose(tomenu, alevel) {
	var a;
	var m;
	for (var i = menuBranches.length - 1; i >= alevel; i--) {
		if ((menuBranches[i] != tomenu) && (menuBranches[i] != '0') && (menuBranches[i] != 'none')) {
			m = document.getElementById(menuBranches[i]);
			m.style.visibilty = 'hidden';
			m.style.top = "-20000px";
			m.style.left = "-20000px";
			a = document.getElementById("link" + menuBranches[i].slice(4));
			if (a)
				a.className = a.className.replace(/ trail/g, "");
			menuBranches[i] = 'none';
		}
	}
}

//--------------------------------------------------------------------------------------------------------

function menuGo (frommenu, tomenu, alevel, horizontal, xoff, yoff) {
	if (alevel > 0)
		menuStartTimeOut = setTimeout('menuShow(\'' + frommenu + '\', \'' + tomenu + '\', ' + alevel + ', ' + horizontal + ', ' + xoff + ', ' + yoff + ')', 100);
	else
		menuStartTimeOut = setTimeout('menuShow(\'' + frommenu + '\', \'' + tomenu + '\', ' + alevel + ', ' + horizontal + ', ' + xoff + ', ' + yoff + ')', 200);
}

//--------------------------------------------------------------------------------------------------------

function menuStop () {
	clearTimeout(menuStartTimeOut);
}

//--------------------------------------------------------------------------------------------------------

function menuShow (frommenu, tomenu, alevel, horizontal, xoff, yoff) {
	menuClose(tomenu, alevel);
	clearTimeout(menuTimeOut);
	menuTimeOut = setTimeout('menuClose(\'0\', 0)', 60000);
	menuBranches[alevel] = tomenu;
	if (document.getElementById(tomenu)) {
		var a = document.getElementById(frommenu);
		a.className = a.className.replace(/ trail/g, "") + " trail";
		var b = document.getElementById(tomenu);
		var x = findPos(a, 'x');
		var y = findPos(a, 'y');
		var w = Math.max(a.clientWidth, a.offsetWidth);
		var h = Math.max(a.clientHeight, a.offsetHeight);
		var bw = b.offsetWidth;
		var bh = b.offsetHeight;
		var sx = window.innerWidth;
		var sy = window.innerHeight;
		var dx = 0;
		if (horizontal) {
			if (x + 2 + bw > sx)
				dx = -(x + 2 + bw - sx);						
		} else {
			if (x + w + 2 + bw > sx)
				dx = -(w + bw + (2 * xoff));
		}
		var dy = y;
		if (y + bh > sy)
			dy = sy - bh - 1;
		b.style.zIndex = 400;
		if (horizontal) {
			b.style.top = (y + h) + 'px';
			b.style.left = (x + dx) + 'px';
		} else {
			b.style.top = (dy + yoff) + 'px';
			b.style.left = (x + w + xoff + dx) + 'px';
		}
		if (bh > sy) {
			b.style.top = '1px';
			b.style.height = (sy - 2) + 'px';
		}	
		b.style.visibility = 'visible';
	}
}

//--------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------
// globals -----------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------

var menuBranches = new Array();
menuBranches[0] = '0';
menuStartTimeOut = 0;
menuTimeOut = 0;

document.onclick = function() { 
	menuClose('0', 0); 
	if (document.getElementById('calendarpopup'))
		destroy('calendarpopup');
};
	
