
function menuLinkOver(args, event)
{
	Event.stop(event);
	var el = $(args.link);
	var elPopup = $(args.popup);
	if (!el.spinCount)
		el.spinCount=0;
	el.spinCount++;
	//el.down("i").update(el.spinCount);
	if (elPopup && (el.spinCount==1) && (elPopup.style.display!='block'))
	{
		var align = 'bl';
		var xOffset = 0;
		var yOffset = 0;
		var wOffset = 0;
		if (typeof(args.x)!=undefined) xOffset = parseInt(args.x);
		if (isNaN(xOffset)) xOffset = 0;
		if (typeof(args.y)!=undefined) yOffset = parseInt(args.y);
		if (isNaN(yOffset)) yOffset = 0;
		if (typeof(args.w)!=undefined) wOffset = parseInt(args.w);
		if (isNaN(wOffset)) wOffset = 0;
		
		if (typeof(args.align)!='undefined')
			align = args.align;
		//alert(1);
		var xPos = el.cumulativeOffset().left;
		var yPos = el.cumulativeOffset().top + el.getDimensions().height;
		if (align=='br')
		{
			xPos = el.cumulativeOffset().left + el.getDimensions().width - elPopup.getDimensions().width;
		}
		xPos += xOffset;
		yPos += yOffset;
		if (typeof(args.matchWidth) && args.matchWidth)
		{
			// Fix: only stretch the drop down, do not shrink it
			if (elPopup.getDimensions().width < el.getDimensions().width)
				elPopup.setStyle({width:(el.getDimensions().width+wOffset)+'px'});
		}
		elPopup.setStyle({left:xPos+'px', top:yPos+'px', display:'block'});
		el.addClassName('hover');
	}
	if (el.timeout)
		clearTimeout(el.timeout);
}

function menuLinkOut(args, event)
{
	if (event)
		Event.stop(event);
	_menuLinkOut.defer(args, event);
	//_menuLinkOut(args, event);
}

function _menuLinkOut(args, event)
{	
	var el = $(args.link);
	var elPopup = $(args.popup);
	if (!el.spinCount)
		el.spinCount=0;
	else
		el.spinCount--;
	if (elPopup && (el.spinCount<=0))
	{
		el.spinCount = 0;
		el.timeout = Element.hide.defer(elPopup);
		//el.timeout = Element.hide(elPopup);
		Element.removeClassName(el, 'hover');
	}
}

// Set up roll over / roll out: When rolling over link_el,
// popup_el will become visible.  When rolling out of
// either the link or the popup, the popup will become invisible
// args is an object:
//		link: ID of the link element
//		popup: ID of the popup element
//		align: Alignment of the popup wrt to the link, possible values:
//					bl - bottom left (the default)
//					br - bottom right
//		x: x offset in pixels wrt to the the alignment above
//		y: y offset in pixels wrt to the the alignment above
function setupMenuMouseover(args)
{
	$(args.link).observe('mouseover', menuLinkOver.curry(args));
	$(args.link).observe('mouseout', menuLinkOut.curry(args));
	if ($(args.popup))
	{
		$(args.popup).observe('mouseover', menuLinkOver.curry(args));
		$(args.popup).observe('mouseout', menuLinkOut.curry(args));
	}
}
