var ns4 = ((navigator.appName == "Netscape") && document.layers)?1:0;
var ns6 = ((navigator.appName == "Netscape") && document.getElementById)?1:0;
var ie4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4) && (navigator.userAgent.indexOf('Opera') == -1))?1:0;
var op6 = (navigator.userAgent.indexOf('Opera') != -1)?1:0;
if (op6 == 1) ns6 = 1;
var intLastTimeout;
var objCurrentMenu;
window.MouseMoveX = 0;
window.MouseMoveY = 0;
function Menu()
{
	if (!window.Menus)
		window.Menus = new Array();

	this.ID = window.Menus.length;
	this.Type = "Menu";
	this.Width = 180;
	this.Color = '#e0ffff';

	// this gets modified each time an entry is added - modify the add item function to 
	// increment this value by the estimated height of each menu item
	this.Height = 0;
	
	// variables to hold the painted area of the menu
	this.topX = 0;
	this.topY = 0;
	this.botX = 0;
	this.botY = 0;
	
	this.ItemOnColor = 'FFFFFF';
	this.ItemOnBgColor = 'FEAE25';
	this.ItemOffColor = '000000';
	this.ItemOffBgColor = 'EFEFEF';
	
	
	// containers for properties
	this.Icon = new Array();
	this.Items = new Array();
	this.Links = new Array();
	this.OnColors = new Array();
	this.OffColors = new Array();
	this.OnBgColors = new Array();
	this.OffBgColors = new Array();
	
	// procedures for Menu object
	this.AddItem = fAddItem;
	this.ShowMenu = fShowMenu;
	this.HideMenu = fHideMenu;
	this.ShowMenuEx = fShowMenuEx;
	this.WriteMenus = fWriteMenus;
	
	// handles usage of menu
	this.Written = 0;
	this.Activated = 0;
	
	window.Menus[window.Menus.length] = this;
}

function fAddItem(sIcon, sText, sLink, sOnColor, sOffColor, sOnBgColor, sOffBgColor)
{
    if (sIcon) this.Icon[this.Icon.length] = sIcon; else this.Icon[this.Icon.length] = '';
	if (sText) this.Items[this.Items.length] = sText; else this.Items[this.Items.length] = '';
	if (sLink) this.Links[this.Links.length] = sLink; else this.Links[this.Links.length] = '';
	if (sOnColor) this.OnColors[this.OnColors.length] = sOnColor; else this.OnColors[this.OnColors.length] = '';
	if (sOffColor) this.OffColors[this.OffColors.length] = sOffColor; else this.OffColors[this.OffColors.length] = '';
	if (sOnBgColor) this.OnBgColors[this.OnBgColors.length] = sOnBgColor; else this.OnBgColors[this.OnBgColors.length] = '';
	if (sOffBgColor) this.OffBgColors[this.OffBgColors.length] = sOffBgColor; else this.OffBgColors[this.OffBgColors.length] = '';
	
	// adjust the height of the menu container by estimated size of each menu item
	this.Height += 22;
}

function fWriteMenus()
{
	for (x = 0; x < window.Menus.length; x++)
	{
		obj = window.Menus[x];
		
		if (obj.Written == 1)
			continue;
		else
			obj.Written = 1;
		
		if (ie4 == 1)
		{
			strParBeg = "<SPAN ID=syMenu{ID} STYLE=\"width:{WIDTH}px; background:{BGCOLOR}; border: groove 2 #{BORDERCOLOR}; position:absolute; visibility:hidden;\" ONMOUSEOVER=\"fMarkMenuActivated({ID});\">";
			strParEnd = "</SPAN>";
		}
		else if (ns4 == 1)
		{
			strParBeg = "<LAYER ID=syMenu{ID} WIDTH={WIDTH} VISIBILITY=Hidden ONMOUSEOVER=\"fMarkMenuActivated({ID});\" ONMOUSEOUT=\"fHideCurrentMenu();\">";
			strParBeg += "<TABLE CELLSPACING=0 CELLPADDING=4 BORDER=0 WIDTH=100% BGCOLOR=000000>";
			strParBeg += "<TR><TD>";
			strParBeg += "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0 WIDTH=100% BGCOLOR={BGCOLOR}>";
	
			strParEnd = "</TABLE>";
			strParEnd += "</TD></TR>";
			strParEnd += "</TABLE>";
			strParEnd += "</LAYER>";
		}
		else if (ns6 == 1)
		{
			strParBeg = "<DIV ID=syMenu{ID} STYLE=\"width:{WIDTH}px; background#{BGCOLOR}; padding:4px; visibility:hidden; position:absolute;\" ONMOUSEOVER=\"fMarkMenuActivated({ID});\">";
			strParBeg += "<DIV STYLE=\"width:100%; background:#000000; padding:1px;\">";
			strParBeg += "<DIV STYLE=\"width:100%; background:#{BGCOLOR};\">";
			
			strParEnd = "</DIV>";
			strParEnd += "</DIV>";
			strParEnd += "</DIV>";
		}
		else
			return;

		strParBeg = strParBeg.replace('{ID}', x);
		strParBeg = strParBeg.replace('{ID}', x);
		strParBeg = strParBeg.replace('{WIDTH}', obj.Width);
		strParBeg = strParBeg.replace('{BGCOLOR}', obj.Color);
		strParBeg = strParBeg.replace('{BGCOLOR}', obj.Color);
		strParBeg = strParBeg.replace('{BORDERCOLOR}', obj.Color);

		document.writeln(strParBeg);
		
		for (y = 0; y < obj.Items.length; y++)
		{
			if (ie4 == 1)
			{
				//strItemBeg = "<SPAN ID=syMenu{ID}SubMenu{SID} STYLE=\" text-align:left; width:{WIDTH}px; color:{COLOR}; background:{BGCOLOR}; padding:3px; font-family:Verdana, Arial; font-size:8pt; cursor:hand;\" ONMOUSEOVER=\"fMenuItemMouseStyle({ID},{SID},1);\" ONMOUSEOUT=\"fMenuItemMouseStyle({ID},{SID},0);\" ONCLICK=\"fMenuItemClick({ID},{SID});\">";
				strItemBeg = "<SPAN ID=syMenu{ID}SubMenu{SID} STYLE=\" text-align:left; width:{WIDTH}px; color:{COLOR}; background:{BGCOLOR}; padding:3px; font-family:Arial, Verdana; font-size:12px; cursor:hand;\" ONMOUSEOVER=\"fMenuItemMouseStyle({ID},{SID},1);\" ONMOUSEOUT=\"fMenuItemMouseStyle({ID},{SID},0);\" ONCLICK=\"fMenuItemClick({ID},{SID});\">";
				strItemEnd = "</SPAN>";
			}
			else if (ns4 == 1)
			{
				strItemBeg = "<TR><TD>";
				strItemEnd = "";
			
				if (obj.Links != '')
				{
					strItemBeg += "<A HREF=\"" + obj.Links[y] + "\">";
					strItemEnd += "</A>";
				}
	    		strItemBeg += "<FONT ALIGN='LEFT' FACE='Arial, Verdana' COLOR={COLOR} style=\"font-size: 12px;\">";
				strItemEnd += "</FONT>";
				strItemEnd += "</TD></TR>";
			}
			else if (ns6 == 1)
			{
				//strItemBeg = "<DIV ID=syMenu{ID}SubMenu{SID} STYLE=\"text-align:left; background:#{BGCOLOR}; color:{COLOR}; font-family:Verdana, Arial; font-size:8pt; cursor:hand; padding:3px;\" ONMOUSEOVER=\"fMenuItemMouseStyle({ID}, {SID}, 1);\" ONMOUSEOUT=\"fMenuItemMouseStyle({ID}, {SID}, 0);\" ONCLICK=\"fMenuItemClick({ID}, {SID});\">";
				strItemBeg = "<DIV ID=syMenu{ID}SubMenu{SID} STYLE=\"text-align:left; background:#{BGCOLOR}; color:{COLOR}; font-family:Arial, Verdana; font-size:12px; cursor:hand; padding:3px;\" ONMOUSEOVER=\"fMenuItemMouseStyle({ID}, {SID}, 1);\" ONMOUSEOUT=\"fMenuItemMouseStyle({ID}, {SID}, 0);\" ONCLICK=\"fMenuItemClick({ID}, {SID});\">";
				strItemEnd = "</DIV>";
			}
			
			//determine color settings
			if (obj.OffColors[y] == '') strColor = obj.ItemOffColor; else strColor = obj.OffColors[y];
			if (obj.OffBgColors[y] == '') strBgColor = obj.ItemOffBgColor; else strBgColor = obj.OffBgColors[y];
			
			for (z = 0; z < 4; z++)
			{
				strItemBeg = strItemBeg.replace('{ID}', x);
				strItemBeg = strItemBeg.replace('{SID}', y);
			}
			
			strItemBeg = strItemBeg.replace('{COLOR}', strColor);
			strItemBeg = strItemBeg.replace('{WIDTH}', obj.Width - 5);
			strItemBeg = strItemBeg.replace('{BGCOLOR}', strBgColor);
			
			if (obj.Items[y] != '')
			{
			    var NomeIcona = ''
				document.writeln(strItemBeg);
				if (obj.Icon[y] != '')
				{
			        NomeIcona = obj.Icon[y];
					document.writeln("<image src='/images/icone/"+NomeIcona+"' align='AbsMiddle' width='16' height='16' border='0'>");
			    }
				document.writeln(obj.Items[y]);
				document.writeln(strItemEnd);
			}
			else
			{
				if (ns4 == 1)
				{
					document.writeln('<TR><TD ALIGN=CENTER>');
					fMenuSeparator(obj.Width);
					document.writeln('</TD></TR>');
				}
				else
				{
					document.writeln('<CENTER>');
					fMenuSeparator(obj.Width);
					document.writeln('</CENTER>');
				}
			}

		}
	
		document.writeln(strParEnd);
	}
	
}

function fMenuSeparator(Width)
{
	document.writeln('<IMG SRC=\"/images/menuseparator.gif\" border=0 width=' + (Width - 5) + ' HEIGHT=2><BR>');
}

function fMenuItemClick(id, sid)
{
	objMenu = window.Menus[id];
	
	if (objMenu.Links[sid] != '')
		try { document.location = objMenu.Links[sid]; }
		catch(e) { }
}

function fMenuItemMouseStyle(id, sid, state)
{
	objMenu = window.Menus[id];

	if (ie4 == 1)
		objItem = document.all('syMenu' + id + 'SubMenu' + sid);
	else if (ns6 == 1)
		objItem = document.getElementById('syMenu' + id + 'SubMenu' + sid);
	else
		return;
	
	if (state == 1)				// menu is on
	{
		if (objMenu.OnColors[sid] == '') strColor = objMenu.ItemOnColor; else strColor = objMenu.OnColors[sid];
		if (objMenu.OnBgColors[sid] == '') strBgColor = objMenu.ItemOnBgColor; else strBgColor = objMenu.OnBgColors[sid];
	}
	else						// menu is off
	{
		if (objMenu.OffColors[sid] == '') strColor = objMenu.ItemOffColor; else strColor = objMenu.OffColors[sid];
		if (objMenu.OffBgColors[sid] == '') strBgColor = objMenu.ItemOffBgColor; else strBgColor = objMenu.OffBgColors[sid];
	}
	
	if (ie4 == 1)
	{
		objItem.style.color = strColor;
		objItem.style.background = strBgColor;
	}
	else if (ns6 == 1)
	{
		objItem.style.color = strColor;
		objItem.style.background = '#' + strBgColor;
	}
}

function fShowMenu(xPos, yPos)
{
	if (objCurrentMenu)
		objCurrentMenu.HideMenu();

	if (ie4 == 1)
	{
		objMenu = document.all('syMenu' + this.ID);
	}
	else if (ns4 == 1)
	{
		objMenu = document.layers['syMenu' + this.ID];
		yPos-=6;
		xPos-=4;
	}
	else if (ns6 == 1)
	{
		objMenu = document.getElementById('syMenu' + this.ID);
		yPos-=6;
		xPos-=4;
	}
	else
		return;
				
	objCurrentMenu = window.Menus[this.ID];
	objCurrentMenu.topX = xPos;
	objCurrentMenu.topY = yPos ;
	objCurrentMenu.botX = xPos + this.Width;
	objCurrentMenu.botY = yPos + this.Height ;
	
	if (ie4 == 1)
	{
		objMenu.style.pixelTop = yPos;
		objMenu.style.pixelLeft = xPos;
		objMenu.style.visibility = 'visible';
	}
	else if (ns4 == 1)
	{
		objMenu.top = yPos;
		objMenu.left = xPos;
		objMenu.visibility = 'visible';
	}
	else if (ns6 == 1)
	{
		objMenu.style.top = yPos;
		objMenu.style.left = xPos;
		objMenu.style.visibility = 'visible';
	}
	
	fClearTimeout();
	intLastTimeout = setTimeout('fHideMenuIfNotActivated()', 8000);
}

function fShowMenuEx(Align, Offset)
{
	if (Align == 'Right')
	{
		xPos = window.MouseMoveX - this.Width + Offset;
		yPos = window.MouseMoveY;
	}
	else
	{
		xPos = window.MouseMoveX;
		yPos = window.MouseMoveY;
	}

	this.ShowMenu(xPos, yPos);
}

function fHideMenu()
{
	window.Menus[this.ID].Activated = 0;
	
	if (ie4 == 1)
	{
		objMenu = document.all('syMenu' + this.ID);
		objMenu.style.visibility = 'hidden';
		objMenu.style.pixelTop = 0;
		objMenu.style.pixelLeft = 0;
	}
	else if (ns4 == 1)
	{
		objMenu = document.layers['syMenu' + this.ID];
		objMenu.visibility = 'hide';
		objMenu.top = 0;
		objMenu.left = 0;
	}
	else if (ns6 == 1)
	{
		objMenu = document.getElementById('syMenu' + this.ID);
		objMenu.style.visibility = 'hidden';
		objMenu.style.top = 0;
		objMenu.style.left = 0;
	}

	objCurrentMenu = null;

}

function fHideCurrentMenu()
{
	if (objCurrentMenu)
	{
		objCurrentMenu.HideMenu();
	}
}

function fMarkMenuActivated(id)
{
	fClearTimeout();
	window.Menus[id].Activated = 1;
}

function fHideMenuIfNotActivated()
{
	if (objCurrentMenu)
	{
		if (!objCurrentMenu.Activated)
		{
			objCurrentMenu.HideMenu();
			MostraNascondi('Mostra');
		}
	}
}

function fShortDelayOnMouseOut()
{
	fClearTimeout();
	intLastTimeout = setTimeout('fHideMenuIfNotActivated()', 500);
}

function fClearTimeout()
{
	if (intLastTimeout != null)
	{
		clearTimeout(intLastTimeout);
		intLastTimeout = null;
	}
}

function eClick(e)
{
	if (e) evt = e;
	if (window.event) evt = event;

	if (objCurrentMenu)
		fHideCurrentMenu();
}

function eMouseMove(e)
{
	if (e) evt = e;
	if (window.event) evt = event;

	if (ie4 == 1 || ns6 == 1)
	{
		window.MouseMoveX = evt.clientX;
		window.MouseMoveY = evt.clientY;
		
		// get vertical scroll offset
		if (ie4 == 1)
			yScroll = document.body.scrollTop;
		else if (ns6 == 1)
			yScroll = window.pageYOffset;

		window.MouseMoveY = window.MouseMoveY + yScroll;
		
	}
	else if (ns4 == 1)
	{
		window.MouseMoveX = evt.pageX;
		window.MouseMoveY = evt.pageY;
	}

	if (objCurrentMenu && objCurrentMenu.Activated)
	{
		mouseX = window.MouseMoveX;
		mouseY = window.MouseMoveY;
		
		if(mouseX < (objCurrentMenu.topX) || mouseX > (objCurrentMenu.botX) || mouseY < (objCurrentMenu.topY) || mouseY > (objCurrentMenu.botY))
		{
			objCurrentMenu.HideMenu();    //MX
			MostraNascondi('Mostra');
	    }
	}
}

if (ie4 == 1 || ns6 == 1)
	document.onclick = eClick;

if (ie4 == 1 || ns6 == 1)
	document.onmousemove=eMouseMove;

if (document.captureEvents)
{
	if (Event.CLICK)
	{
		document.captureEvents(Event.CLICK);
		document.onclick = eClick;
	}
	if (Event.MOUSEMOVE)
	{
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = eMouseMove;
	}
}
