vmenu = function (menuPosition) {
	this.menuPosition	 = menuPosition || 'left';
	this.menuScrollTimer  = null;
	this.menuHideTimer = null;
	this.menuPath = new Array();
}

vmenu.prototype.mabstop = function(source)
{
  var ttop = source.offsetTop;
  while (source = source.offsetParent)
    ttop+=source.offsetTop;
  return ttop;
}

vmenu.prototype.mabsleft = function (source)
{
  var lleft = source.offsetLeft;
  while (source = source.offsetParent)
    lleft+=source.offsetLeft;
  return lleft;
}

vmenu.prototype.mbScroll = function (id, scrollDir)
{
	var box = document.getElementById(id);
	var scrollTop 	 = parseInt(box.getAttribute('sct'));
	var scrollBottom = parseInt(box.getAttribute('scb'));

	if (scrollDir=='up')
	{
		if (scrollTop > 1)
		{
			var childs = box.getElementsByTagName('DIV');
			childs[scrollBottom].style.display = 'none';
			scrollTop--;
			scrollBottom--;
			childs[scrollTop].style.display = '';
			box.setAttribute('sct', scrollTop);
			box.setAttribute('scb', scrollBottom);
		}
	} else
	if (scrollDir=='down')
	{
		var childs = box.getElementsByTagName('DIV');
		if (typeof(childs[scrollBottom + 2]) != 'undefined')
		{
			childs[scrollTop].style.display = 'none';
			scrollTop++;
			scrollBottom++;
			childs[scrollBottom].style.display = '';
			box.setAttribute('sct', scrollTop);
			box.setAttribute('scb', scrollBottom);
		}
	}
}

vmenu.prototype.mStopHidding = function ()
{
	if (this.menuHideTimer)
	{
		clearTimeout(this.menuHideTimer);
		this.menuHideTimer = null;
	}
}

// debug function. dhould be box with "log" id somewhere
vmenu.prototype.log = function (text)
{
	var logDate = new Date();
	var logItem = document.getElementById('log');
	logItem.innerHTML = logItem.innerHTML+logDate.getHours()+':'+logDate.getMinutes()+':'+logDate.getSeconds()+'<br />'+text+'<br />';
}

vmenu.prototype.mHideUnusedMenus = function (boxItem)
{
	for (var i=this.menuPath.length-1; i>=0; i--)
 	if (this.menuPath[i]!= boxItem.parentNode)
 		this.menuPath[i].style.visibility = 'hidden';
 	else
	 break;
	this.menuPath.length = i+1;
}

vmenu.prototype.mrOver = function (id, boxItem) /*boxItem for internal usage, id - for external */
{
	var drop;
	var subBoxId;

	this.mStopHidding();
	if (!id)
	{
		this.mHideUnusedMenus(boxItem);

		subBoxId = boxItem.parentNode.id + '_' + boxItem.id.substr(2); // skip mr prefix
	} else
	{
		// hide all menu
		this.menuHide();

		subBoxId = 'd_'+id;
	}
	if (drop = document.getElementById(subBoxId))
	{
		var left = this.mabsleft(boxItem);
		var top  = this.mabstop(boxItem) + 2;

		if (!id)
		{
			if (left + boxItem.offsetWidth + drop.offsetWidth > screen.availWidth)
				left -= drop.offsetWidth;
			else
				left += boxItem.offsetWidth;

			if (top + drop.offsetHeight > screen.availHeight)
				top = screen.availHeight - drop.offsetHeight;
		} else
		{
			switch (this.menuPosition)
			{
			case 'bottom':
				top -= drop.offsetHeight;
				break;
			case 'right':
				left-= drop.offsetWidth;
				break;
			case 'left':
				left+= boxItem.offsetWidth;
				break;
			case 'top':
				top += boxItem.offsetHeight;
				break;
			}
		}
		this.menuPath.push(drop);
		drop.style.top  = top + 'px';
		drop.style.left = left + 'px';
		drop.style.visibility = 'visible';
	}
}

vmenu.prototype.menuHide = function ()
{
	for (var i=this.menuPath.length-1; i>=0; i--)
		this.menuPath[i].style.visibility = 'hidden';

	this.menuPath.length=0;
	this.menuHideTimer = null;
}

vmenu.prototype.mover = function (menuBox, ev)
{
	if (!ev) ev = window.event;

	ev.cancelBubble = true;
	if (ev.stopPropagation)
		ev.stopPropagation();

	var srcItem = (ev.target ? ev.target:ev.srcElement);

	this.mStopHidding();
	if (srcItem!=menuBox)
	{
		while (typeof(srcItem.parentNode)!='undefined' && srcItem.parentNode!=menuBox)
			srcItem = srcItem.parentNode;

		if (!srcItem) // really this situation unpossible
			return;
		switch (srcItem.className)
		{
		case 'mbscu':
			this.mHideUnusedMenus(srcItem);
			this.menuScrollTimer = setInterval("mbScroll('" + menuBox.id + "', 'up')", 200);
			break;
		case 'mbscd':
			this.mHideUnusedMenus(srcItem);
			this.menuScrollTimer = setInterval("mbScroll('" + menuBox.id + "', 'down')", 200);
			break;
		case 'mr':
		case 'mrc':
			this.mrOver(0, srcItem);
			break;
		}
	}
}

vmenu.prototype.startHiding = function ()
{
	if (this.menuScrollTimer) {
		clearInterval(this.menuScrollTimer);
		this.menuScrollTimer = null;
	}
	var menuObj = this;
	this.menuHideTimer = setTimeout(function () { menuObj.menuHide() }, 600);
}

vmenu.prototype.mout = function (menuBox, ev)
{
	if (!ev) ev = window.event;

	ev.cancelBubble = true;
	if (ev.stopPropagation)
		ev.stopPropagation();

	var srcItem = (ev.target ? ev.target:ev.srcElement);
	if (srcItem!=menuBox)
	{
		while (typeof(srcItem)!='undefined' && srcItem!=menuBox)
			srcItem = srcItem.parentNode;
	}
	if (srcItem == menuBox) {
		this.startHiding();
	}
}
