/**
 * mpl_mm_menu May, 16 2002 17:15
 * Created by Marcello Rutter
 * Copyright (c) 2002 Mediamaster, srl
 *
 */
/**
	May, 20 2002 12:10 (MARRUT)
	Added 'PaintTD' method (used to paint the menu TD tag)
 */
/**
	Sep, 19 2002 15:10 (MARRUT)
	Removed alert into function "PopupMyMenu" (menu can be empty: without items)
 */

function PaintTD(source, action) {

	if (!source || !source.style)
		return;
	
	s = source.style;
	switch (action) {
	case 0:	// Normal
		if (source.mpl_fc)
			s.color = source.mpl_fc;
		if (source.mpl_bg)
			s.backgroundColor = source.mpl_bg;
		break;
	case 1:	// Over
		if (source.mpl_fch)
			s.color = source.mpl_fch;
		if (source.mpl_bgh)
			s.backgroundColor = source.mpl_bgh;
		s.cursor = 'hand';
		break;
	}
}

function PopupMyMenu(source, menu, direction) {

	if (!menu || !source || !window.FW_showMenu) {
		// alert ('Unable to show the menu!');
		return;
	}
	
	// Autodetect LEFT, TOP of the new menu
	var newLeft = 0;
	var newTop = 0;
	if (direction == 'H')
		newTop = source.offsetHeight;
	else
		newLeft = source.offsetWidth;
	// Add offsets		
	newLeft += GetOffset(source, 'left');
	newTop += GetOffset(source, 'top');
	
	// TODO: Check if ranges (newLeft and newTop)
	// are out of screen resolution (screen.width and screen.height)

	return window.FW_showMenu(menu, newLeft, newTop);
}

function GetOffset(src, mode) {

	var offset = 0;
	var hParent = src;
	
	while (typeof hParent != 'undefined') {
		switch (mode) {
		case 'top':
			offset += hParent.offsetTop;
			break;
		case 'left':
			offset += hParent.offsetLeft;
			break;
		case 'with':
			offset += hParent.offsetWidth;
			break;
		case 'height':
			offset += hParent.offsetHeight;
			break;
		}
		if (hParent.tagName == 'BODY')
			break;
		hParent = hParent.offsetParent;
	}

	return offset;	
}
