menus.prototype.m_delay = 2000;
menus.prototype.m_timeout = null;
menus.prototype.m_active = null;

activestack.prototype.count = 0;
activestack.prototype.type = 0;
activestack.prototype.m_firstpopup = 1;

function menus()
{
	this.m_acStack = new activestack();
}
menus.prototype.BeginMenu = function(id_name)
{
	this.m_active = id_name;
	document.write('<div id="'+id_name+'" class="\menus\"><table class="menus" cellspacing="0">');
}
menus.prototype.EndMenu = function()
{
	document.write('</table></div>');
	this.m_active = null;
}
menus.prototype.AddLink = function(id_name,title,url,id_submenu)
{
	document.write('<tr><td class="menus');
	if(id_submenu) document.write(' submenu');
	document.write('"><a href="'+url+'" id="'+id_name+'" class="menus');
	if(id_submenu) document.write(' submenu');
	document.write('" onclick="SubMenuOver(\''+id_name+'\');');
	if(!url) document.write('return false;');
	//document.write('" onmouseover="ShowPreview(\''+id_name+'\',\''+url+'\')">'+title+'</a></td></tr>');
	document.write('">'+title+'</a></td></tr>');

	var e = document.getElementById(id_name);
	e.m_root = this;
	e.m_idchild = id_submenu;
	e.m_menu = this.m_active;
}
function MenuItemOver(id_name,id_sub)
{
	document.getElementById(id_name).m_root = _menu;
	clearInterval(_menu.m_timeout);
	if(id_sub) document.getElementById(id_sub).m_depth = 1;
	_menu.m_active = id_name;
	_menu.m_acStack.push(0,id_name,id_sub);
}
function SubMenuOver(id_name)
{
	var e = document.getElementById(id_name);
	var pm = document.getElementById(e.m_menu);
	if(e.m_idchild) document.getElementById(e.m_idchild).m_depth = pm.m_depth+1;
	clearInterval(e.m_root.m_timeout);
	e.m_root.m_acStack.push(pm.m_depth,id_name,e.m_idchild);
}		
function SubMenuOut(id_name)
{
	var e = document.getElementById(id_name);
	if(e.m_root.m_active) e.m_root.m_timeout = setInterval("_menu.m_active = null",e.m_root.m_delay);
}
function MenuItemOut(id_name)
{
	_menu.m_timeout = setInterval("_menu.m_active = null",_menu.m_delay);
}
function ForceClose(id_name)
{
	var e = document.getElementById(id_name);
	clearInterval(e.m_root.m_timeout);
	e.m_root.m_acStack.clear();
	e.m_root.m_active = null;
}
function activestack()
{
	this.depth = new Array();
	this.element = new Array();
}
activestack.prototype.push = function(depth,idelem,idchild)
{
	while(this.count > 0 && depth <= this.depth[this.count-1]) this.pop();
	if(idelem)
	{
		this.depth[this.count] = depth;
		this.element[this.count] = idelem;
		this.count++;
		document.getElementById(idelem).m_idchild = idchild;
		if(idchild) this.ShowMenu(idchild,idelem,(depth == 0)?true:false);
	}
}
activestack.prototype.pop = function()
{
	with(this)
	{
		if(count > 0)
		{
			count--;
			var idchild = document.getElementById(element[count]).m_idchild;
			if(idchild) document.getElementById(idchild).style.visibility = "hidden";
			depth[count] = element[count] = null;
		}
	}
}
activestack.prototype.clear = function()
{
	while(this.count > 0) this.pop();
}
activestack.prototype.ShowMenu = function(id_name,id_parent,down)
{
	var p = document.getElementById(id_parent);
	var e = document.getElementById(id_name);
	var top = 0;
	var left = 0;
	var c = p;
	while(c)
	{
		top += c.offsetTop;
		left += c.offsetLeft;
		c = c.offsetParent;
	}
	if(this.type == 1 || !down) left += p.offsetWidth - 30;
	else if(e.m_depth == 1) top += p.offsetHeight -10;

	if(top+e.offsetHeight > document.body.scrollTop+document.body.clientHeight) top-= ((top+e.offsetHeight)-(document.body.scrollTop+document.body.clientHeight));
	if(e.m_depth == 1)
	{
		if(this.type == 0)
		{
			if(this.m_firstpopup == 1) left = left + (p.offsetWidth - e.offsetWidth) / 2;
			else if(this.m_firstpopup == 2) left = (left + p.offsetWidth) - e.offsetWidth;
			else if(this.m_firstpopup == 3)
			{
				var iWidth = p.offsetWidth - 1;
				var vAnchors = e.getElementsByTagName(((navigator.userAgent).indexOf("IE") != -1) ? 'td' : 'a');
				for(var n = 0; n < vAnchors.length; ++n) vAnchors[n].style.width = iWidth;
			}
		}
		else
		{
			if(this.m_firstpopup == 1) top = top + (p.offsetHeight - e.offsetHeight) / 2;
			else if(this.m_firstpopup == 2) top = (top + p.offsetHeight) - e.offsetHeight;
		}
	}
	else top -= 30;
	e.style.top = ((top<0)?0:top)+"px";
	e.style.left = ((left<0)?0:left)+"px";
	e.style.visibility = "visible";
	var innerwinsz, scrloffst;
	if((innerwinsz = getInnerWinSize()))
	{
		if(e.offsetHeight > innerwinsz.y)
		{
			e.style.height = innerwinsz.y;
			var wdth = e.offsetWidth + 20;
			e.style.width = wdth+'px';
			e.style.overflowY = 'scroll';
			scrloffst = getScrollOffset();
			e.style.top = scrloffst.y;
		}
	}
}
var lastpreview;
function ShowPreview(id, url)
{
	if(!Previews[id]) return;
	RemovePreview(lastpreview);
	lastpreview = id;
	var pos = getScrollOffset();
	var el = document.createElement('div');
	el.id = 'prev_'+id;
	el.className = 'menu_prev';
	el.style.top = pos.y+'px';
	el.style.left = pos.x+'px';
	var prods = Previews[id].split(',');
	var html = '';
	for(var i in prods)
	{
		var anchorprod = prods[i].toLowerCase().replace(/[^a-z0-9]/gi, '');
		html += '<a class="menus" href="'+url+'#'+anchorprod+'">'+prods[i]+'</a>';
	}
	el.innerHTML = html;
	document.body.appendChild(el);
}
function RemovePreview(id)
{
	var el;
	if((el = document.getElementById('prev_'+id))) document.body.removeChild(el);
}