
//----------------------------------------------------------------------------
// Gestione della barra menu e del bottone attivo.
//----------------------------------------------------------------------------

var browser = new Browser();
var activeButton = null;


//----------------------------------------------------------------------------
// Determinazione tipo Browser
//----------------------------------------------------------------------------

function Browser() 
	{

	var ua, s, i;

	this.isIE    = false;  // Internet Explorer
	this.isOP    = false;  // Opera
	this.isNS    = false;  // Netscape
	this.version = null;

	ua = navigator.userAgent;
	
	s = "Opera";
	if ((i = ua.indexOf(s)) >= 0) 
		{
 		this.isOP = true;
 		this.version = parseFloat(ua.substr(i + s.length));
 	return;
		}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) 
		{
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
		}

	// I browser "Gecko" vengono considerati come Netscape 6.1.
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) 
		{
		this.isNS = true;
		this.version = 6.1;
		return;
		}

	s = "MSIE";
	if ((i = ua.indexOf(s))) 
		{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
		}
}


//----------------------------------------------------------------------------
// Gestione Evento Click del bottone
//----------------------------------------------------------------------------
function buttonClick(event, menuId) 
	{
	var button;

	// preleva il bottone 
	if (browser.isIE)
		button = window.event.srcElement;
	else
		button = event.currentTarget;

	// toglie l'outline al bottone
	button.blur();

	// se manca associa il relativo menuId al bottone 
	if (button.menu == null) 
		{
		button.menu = document.getElementById(menuId);
		// e lo inizializza
		if (button.menu.isInitialized == null)
			menuInit(button.menu);
		}

	// se manca associa la funzione di gestione dell'evento mouseout al bottone
	if (button.onmouseout == null)
	button.onmouseout = buttonOrMenuMouseout;

	// se è il bottone attivo ha finito
	if (button == activeButton)
		return false;

	// altrimenti resetta il bottone attualmente attivo
	if (activeButton != null)
		resetButton(activeButton);

	// e attiva questo bottone a meno che non sia diventato quello attivo nel frattempo
	if (button != activeButton) 
		{
		depressButton(button);
		activeButton = button;
		}
	else
		activeButton = null;

	return false;
}
 
//----------------------------------------------------------------------------
// Gestione Evento Mouseover del bottone
//----------------------------------------------------------------------------
function buttonMouseover(event, menuId) 
	{
	var button;

	// Invia evento click al bottone
	if (activeButton == null) 
		{
 		buttonClick(event, menuId);
 		return;
		}

	// trova l'elemento

	if (browser.isIE)
		button = window.event.srcElement;
	else
		button = event.currentTarget;

	// attiva il bottone 
	if (activeButton != null && activeButton != button)
 		buttonClick(event, menuId);
	}
 

//----------------------------------------------------------------------------
// Gestione Evento Depress del bottone
//----------------------------------------------------------------------------
function depressButton(button) 
	{

	var x, y;

	// aggiorna lo stile del bottone così sembra non premuto
	button.className += " menuButtonActive";

	// imposta il gestore dell'evento mouseout per il bottone 
	if (button.onmouseout == null)
		button.onmouseout = buttonOrMenuMouseout;
	if (button.menu.onmouseout == null)
		button.menu.onmouseout = buttonOrMenuMouseout;


	// Posiziona dropdown menu associato sotto il bottone e lo mostra
	x = getPageOffsetLeft(button);
	y = getPageOffsetTop(button) + button.offsetHeight;
	

	// per IE sistema la posizione
	if (browser.isIE) 
		{
		x += button.offsetParent.clientLeft;
		y += button.offsetParent.clientTop;
		}

	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	button.menu.style.visibility = "visible";
	}
 
 
 
//----------------------------------------------------------------------------
// Reset del bottone
//----------------------------------------------------------------------------
function resetButton(button) 
	{
	removeClassName(button, "menuButtonActive");

	// nasconde il la voce di menu del bottone chiudendo i sottomenu se ci sono
	if (button.menu != null) 
		{
		closeSubMenu(button.menu);
		button.menu.style.visibility = "hidden";
		}
	}
 
//----------------------------------------------------------------------------
// Gestione Evento Mouseover di menu e sottomenu
//----------------------------------------------------------------------------
function menuMouseover(event) 
	{

	var menu;

	// cerca il menu
	if (browser.isIE)
		menu = getContainerWith(window.event.srcElement, "DIV", "menu");
	else
		menu = event.currentTarget;

	// chiude ogni sottomenu attivo
	if (menu.activeItem != null)
		closeSubMenu(menu);
	}
 
//----------------------------------------------------------------------------
// Gestione Evento MouseOver di menuItem e sottomenu
//----------------------------------------------------------------------------
function menuItemMouseover(event, menuId) 
	{

	var item, menu, x, y;

	// cerca l'item e suo padre
	if (browser.isIE)
	 	item = getContainerWith(window.event.srcElement, "A", "menuItem");
	else
	 	item = event.currentTarget;
	
	menu = getContainerWith(item, "DIV", "menu");

	// chiude ogni sottomenu attivo e marca questo come attivo
	if (menu.activeItem != null)
		closeSubMenu(menu);
	menu.activeItem = item;

	// evidenzia l'item
	item.className += " menuItemHighlight";

	// se manca, inizializza il sottomenu 
	if (item.subMenu == null) {
		item.subMenu = document.getElementById(menuId);
	if (item.subMenu.isInitialized == null)
	   menuInit(item.subMenu);
	}

	// se manca, imposta il gestore dell'evento mouseout per i sottomenu
	if (item.subMenu.onmouseout == null)
		item.subMenu.onmouseout = buttonOrMenuMouseout;


	// calcola la posizione del sottomenu 
	x = getPageOffsetLeft(item) + item.offsetWidth;
	y = getPageOffsetTop(item);
	
	// cambia la posizione perchè sia visibile
	var maxX, maxY;
	if (browser.isIE) 
		{
		maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
	   		   (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
	 	maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
	   			(document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
		}
	if (browser.isOP) 
		{
		maxX = document.documentElement.scrollLeft + window.innerWidth;
		maxY = document.documentElement.scrollTop  + window.innerHeight;
		}
	if (browser.isNS) 
		{
		maxX = window.scrollX + window.innerWidth;
		maxY = window.scrollY + window.innerHeight;
		}
	
	maxX -= item.subMenu.offsetWidth;
	maxY -= item.subMenu.offsetHeight;
		
	if (x > maxX)
		x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));

	y = Math.max(0, Math.min(y, maxY));

	
	// posizione e mostra il sottomenu
	item.subMenu.style.left = x + "px";
	item.subMenu.style.top  = y + "px";
	item.subMenu.style.visibility = "visible";

	// stoppa il bubbling dell'evento
	if (browser.isIE)
		window.event.cancelBubble = true;
	else
		event.stopPropagation();
	}


//----------------------------------------------------------------------------
// Chiusura sottomenu
//----------------------------------------------------------------------------
function closeSubMenu(menu) 
	{
	if (menu == null || menu.activeItem == null)
		return;

	// ricorsivamente chiude tutti i sottomenu
	if (menu.activeItem.subMenu != null) 
		{
		closeSubMenu(menu.activeItem.subMenu);
		menu.activeItem.subMenu.style.visibility = "hidden";
		menu.activeItem.subMenu = null;
		}
	
	removeClassName(menu.activeItem, "menuItemHighlight");
	menu.activeItem = null;
	}
 
 
//----------------------------------------------------------------------------
// Gestione Evento MouseoOut per bottoni e sottomenu
//----------------------------------------------------------------------------
function buttonOrMenuMouseout(event) 
	{
	var el;

	// se non esiste bottone attivo esce
	if (activeButton == null)
		return;

	// cerca l'elemento verso il quale il mouse sta andando

	if (browser.isIE)
		el = window.event.toElement;
	else if (event.relatedTarget != null)
		el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);

	// se l'elemento non è parte di un menu resetta il bottone attivo
	if (getContainerWith(el, "DIV", "menu") == null) 
		{
		resetButton(activeButton);
		activeButton = null;
		}
	}
 
 

//----------------------------------------------------------------------------
// INIZIALIZZAZIONE DEI MENU
//----------------------------------------------------------------------------
function menuInit(menu) 
	{
	var itemList, spanList;
	var textEl, arrowEl;
	var itemWidth;
	var w, dw;
	var i, j;

	// per IE, rimpiazza i caratteri freccina
	if (browser.isIE) 
		{
		menu.style.lineHeight = "2.5ex";
		spanList = menu.getElementsByTagName("SPAN");
		
		for (i = 0; i < spanList.length; i++)
			if (hasClassName(spanList[i], "menuItemArrow")) 
				{
 				spanList[i].style.fontFamily = "Webdings";
 				spanList[i].firstChild.nodeValue = "4";
				}
		}

	// calcola la larghezza del menu
	itemList = menu.getElementsByTagName("A");
	if (itemList.length > 0)
		itemWidth = itemList[0].offsetWidth;
	else
		return;

	// per gli elementi con la freccina aggiunge un po' di spazio 
	for (i = 0; i < itemList.length; i++) 
		{
		spanList = itemList[i].getElementsByTagName("SPAN");
		textEl  = null;
		arrowEl = null;
		
		for (j = 0; j < spanList.length; j++) 
			{
			if (hasClassName(spanList[j], "menuItemText"))
				textEl = spanList[j];

			if (hasClassName(spanList[j], "menuItemArrow"))
				arrowEl = spanList[j];
			}

		if (textEl != null && arrowEl != null) 
			{
			textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";

			// per Opera,rimuove il margine destro negativo 
			if (browser.isOP)
				arrowEl.style.marginRight = "0px";
			}
		}

	// per IE c'è problema con l'hover: si setta allora una larghezza esplicita per il primo item del menu

	if (browser.isIE) 
		{
		w = itemList[0].offsetWidth;
		itemList[0].style.width = w + "px";
		dw = itemList[0].offsetWidth - w;
		w -= dw;
		itemList[0].style.width = w + "px";
		}

	// Marca il menu come inizializzato
	menu.isInitialized = true;
	}
 


//----------------------------------------------------------------------------
// a partire da un nodo cerca il primo elemento che lo contiene con un certo tag e un certo stile
//----------------------------------------------------------------------------
function getContainerWith(node, tagName, className) 
	{

	while (node != null) 
		{
		if (node.tagName != null && node.tagName == tagName && hasClassName(node, className))
			return node;
		node = node.parentNode;
		}
	return node;
	}


//----------------------------------------------------------------------------
// torna vero se elemento ha quella classe
//----------------------------------------------------------------------------
function hasClassName(el, name) 
	{
	var i, list;
	list = el.className.split(" ");
	
	for (i = 0; i < list.length; i++)
		if (list[i] == name)
			return true;
	
	return false;
	}


//----------------------------------------------------------------------------
// elimina una classe dall elemento 
//----------------------------------------------------------------------------
function removeClassName(el, name) 
	{
//	var i, curList, newList;

	if (el.className == null)
		return;
	
	newList = new Array();
	curList = el.className.split(" ");
	for (i = 0; i < curList.length; i++)
		if (curList[i] != name)
			newList.push(curList[i]);
	
	el.className = newList.join(" ");

	}


//----------------------------------------------------------------------------
// restituisce la coordinata x di un elemento relativamente alla pagina
//----------------------------------------------------------------------------
function getPageOffsetLeft(el) 
	{
	var x;

	x = el.offsetLeft;
	if (el.offsetParent != null)
		x += getPageOffsetLeft(el.offsetParent);
	
	return x;
	}


//----------------------------------------------------------------------------
// restituisce la coordinata y di un elemento relativamente alla pagina
//----------------------------------------------------------------------------
function getPageOffsetTop(el) 
	{

	var y;

	y = el.offsetTop;
	if (el.offsetParent != null)
		y += getPageOffsetTop(el.offsetParent);

	return y;
}

