/////////////////////////////////////////////////////////////////
// Nome				: MT_Menu.js
// Descrizione		: Libreria per la generazione e il controllo
//					  di menu a tendina
// Engine			: Javascript v1.2
// Autore			: Alessandro genovese
// Versione			: v1.1
// Data rilascio	: 08 Ottobre 2005
// Template			: Tendine standard senza icone
/////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// Definizione delle costanti necessarie per personalizzare i menu
//////////////////////////////////////////////////////////////////
var
	COLORE_SFONDO_TENDINA	= "#ffff66",
	COLORE_ITEM_ATTIVO		= "#ffff00",
	COLORE_CORNICE_ITEM		= "#FF4C00",
	COLORE_BARRA_MENU		= "#FF4C00",
	COLORE_FONT_BARRA_MENU	= "#ffffff",
	COLORE_URL_NULLO		= "#909090",
	WIDTH_TITOLO_MENU		= 732,
	DISTRIBUZIONE_MENU		= 1;   // Imposta 1 per allineare al centro tutti i titoli di menu

//////////////////////////////////////////////////////////////////
// Costanti e variabili private per la gestione dei menu
//////////////////////////////////////////////////////////////////
var
	TIPO_TENDINA 	= 0,
	TIPO_ITEM		= 1,
	TIPO_SEPARATORE = 2,
	TIPO_SUBTENDINA = 3,
// ===================================================
	MenuIndex 		= Array(),
	mouseX			= 0,
	mouseY			= 0,
	MenuAttivo		= null,
	MouseDelay		= 280468,
	ScostamentoMenu = 0,
	OldScostamentoMenu = 0,
	ControlMenuTimer = -1;;
	
/////////////////////////////////////////////////////////
// Definizione degli oggetti che rappresentano i menu
/////////////////////////////////////////////////////////
	
function Tendina() {
	this.Tipo 		= TIPO_TENDINA;
	this.Handle 	= -1;
	this.parent 	= -1;
	this.hasChild 	= false;
	this.Nome 		= "";
	this.URL 		= "";
	this.x 			= 0;
	this.y 			= 0;
	this.w 			= 0;
	this.Items 		= Array();
}

function Item() {
	this.Tipo 	= TIPO_ITEM;
	this.parent = -1;
	this.Nome 	= "";
	this.URL 	= "";
}

function subTendina() {
	this.Tipo = TIPO_SUBTENDINA;
	this.Handle = -1;
	this.Menu = null;
}

// ========================================================================================

//////////////////////////////////////////////////////
// Funzioni per la gestione interna delle tendine
//////////////////////////////////////////////////////

function MT_totTendine() {
	return MenuIndex.length;
}

function MT_totItems(Menu) {
	if (Menu != null) return Menu.Items.length;
				 else return 0;
}

// ========================================================================================

///////////////////////////////////////////////////////
// Funzioni per l'inserimento delle tendine in memoria
///////////////////////////////////////////////////////

function MT_addMenu(Nome,URL,x,y,width) {
	var
		p = MenuIndex.length;
	if ((navigator.appName == "Netscape") || (navigator.appName == "Opera")) y -= 7;
	MenuIndex[p] 		= new Tendina();
	MenuIndex[p].Handle = p;
	MenuIndex[p].Nome 	= Nome;
	MenuIndex[p].URL 	= URL;
	MenuIndex[p].x		= x;
	MenuIndex[p].y		= y;
	MenuIndex[p].w		= width;
	return MenuIndex[p];
}

function MT_addItem(Menu,Nome,URL) {
	var
		p = Menu.Items.length;
	Menu.Items[p] 		= new Item();
	Menu.Items[p].Nome 	= Nome;
	Menu.Items[p].URL	= URL;
}

function MT_addSeparator(Menu) {
	var
		p = Menu.Items.length;
	Menu.Items[p] 		= new Item();
	Menu.Items[p].Tipo 	= TIPO_SEPARATORE;
	Menu.Items[p].URL	= "";
	Menu.Items[p].Nome	= "";
}

function MT_addSubMenu(Menu,Nome,width) {
	// Definisce una nuova tendina di menu
	var
		pm = MenuIndex.length,
		p = Menu.Items.length;
	MenuIndex[pm] 			= new Tendina();
	MenuIndex[pm].Tipo		= TIPO_SUBTENDINA;
	MenuIndex[pm].Handle 	= pm;
	MenuIndex[pm].Nome 		= Nome;
	MenuIndex[pm].URL 		= "";
	MenuIndex[pm].x			= Menu.x + Menu.w - 6;
	MenuIndex[pm].y			= Menu.y + (p * 18);
	MenuIndex[pm].w			= width;
	Menu.Items[p]			= new subTendina();
	Menu.Items[p].Handle 	= pm;
	Menu.Items[p].Menu		= MenuIndex[pm];
	Menu.hasChild			= true;
	return MenuIndex[pm];
}

// ========================================================================================

/////////////////////////////////////////////////////////
// Funzioni per la gestione degli eventi
/////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// Nome	: ChiudiTendinePrincipali
// Descrizione : Chiude tutte le tendine principali
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function ChiudiTendinePrincipali() {
	for (var i=0;i<MT_totTendine();i++) {
		var
			Obj = document.getElementById("Menu"+MenuIndex[i].Handle);
		Obj.style.visibility = "hidden";
		SpegniSubMenu(MenuIndex[i]);
	}
	MenuAttivo = null;
}

//////////////////////////////////////////////////////////////////
// Nome	: VisualizzaTendina
// Descrizione : Rende visibile la tendina puntata da MenuHandle
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function VisualizzaTendina(MenuHandle) {
	var
		Obj = document.getElementById("Menu"+MenuHandle);
	ChiudiTendinePrincipali();
	Obj.style.visibility = "visible";
	// Aggiorna il puntatore alla tendina attiva
	MenuAttivo = MenuIndex[MenuHandle];
}

//////////////////////////////////////////////////////////////////
// Nome	: SpegniSubMenu
// Descrizione : Chiude tutte le tendine collegate ad un
//				 ITEM che rappresenta un SUBMENU - La procedura
//				 chiude ricorsivamente tutte le finestre figlie
//				 ad esso collegate
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function SpegniSubMenu(Menu,Item) {
	if ((Menu != null) && (Menu.hasChild)) {
		for (var i=0;i<Menu.Items.length;i++) {
			if ((Menu.Items[i].Tipo == TIPO_SUBTENDINA) && (i != Item)) {
				// Chiude la tendina
				document.getElementById("Menu"+Menu.Items[i].Menu.Handle).style.visibility = "hidden";
				if (Menu.Items[i].Menu.hasChild) SpegniSubMenu(Menu.Items[i].Menu);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////
// Nome	: ItemInEvidenza
// Descrizione : Colora l'ITEM selezionato mettendolo
//				 in evidenza
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function ItemInEvidenza(MenuHandle,Item) {
	var
		Obj = document.getElementById("Menu"+MenuHandle+"Item"+Item);
	Obj.style.backgroundColor = COLORE_ITEM_ATTIVO;
	Obj.style.borderColor = COLORE_CORNICE_ITEM;
	Obj.style.borderStyle = "solid";
	Obj.style.borderWidth = "1px";
	// Cancella altri SubMenu
	SpegniSubMenu(MenuIndex[MenuHandle],-1);
	// Verifica se si tratta di un SubMenu
	if (MenuIndex[MenuHandle].Items[Item].Tipo == TIPO_SUBTENDINA) {
		
		// Visualizza SubTendina
		document.getElementById("Menu"+MenuIndex[MenuHandle].Items[Item].Menu.Handle).style.visibility = "visible";
	}
}

//////////////////////////////////////////////////////////////////
// Nome	: ItemDefault
// Descrizione : Ripristina tutti i colori di un ITEM
//				 alle condizioni di default 
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function ItemDefault(MenuHandle,Item) {
	var
		Obj = document.getElementById("Menu"+MenuHandle+"Item"+Item);
	SpegniSubMenu(MenuAttivo,Item);
	// Verifica che il mouse non superi ina certa soglia
	Obj.style.backgroundColor = COLORE_SFONDO_TENDINA;
	Obj.style.borderColor = COLORE_SFONDO_TENDINA;
	Obj.style.borderStyle = "solid";
	Obj.style.borderWidth = "1px";
}

//////////////////////////////////////////////////////////////////
// Nome	: ItemLink
// Descrizione : Effettua il salto alla nuova pagina
//				 utilizzando l'indirizzo inserito 
//				 nell'ITEM selezionato
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function ItemLink(MenuHandle,Item) {
	var
		Obj = MenuIndex[MenuHandle].Items[Item];
	if (Obj.URL != "") {
		window.location = Obj.URL;
	}
}

//////////////////////////////////////////////////////////////////
// Nome	: CalcolaMenuAttivo
// Descrizione : Restituisce un puntatore globale alla tendina
//				 di menu su cui il mouse sta transitando
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function CalcolaMenuAttivo(MenuHandle) {
	MenuAttivo = MenuIndex[MenuHandle];
}

//////////////////////////////////////////////////////////////////
// Nome	: DisattivaMenuAttivo
// Descrizione : Azzera il puntatore al menu attivo quando il mouse
//				 esce dalla tendina
// Restituisce : Niente
//////////////////////////////////////////////////////////////////
function DisattivaMenuAttivo() {
	MenuAttivo = null;
}

// ========================================================================================

////////////////////////////////////////////////////////////
// Funzioni per il disegno dei menu
////////////////////////////////////////////////////////////

function DisegnaTendina(Menu) {
	if (Menu.Items.length == 0) return null;
	document.write('<div class="Tendina" ID="Menu'+Menu.Handle+'" ');
	document.write('style="left: '+(Menu.x+ScostamentoMenu)+'px; top: '+Menu.y+'px; width: '+Menu.w+'px;" ');
	document.write('onMouseMove="CalcolaMenuAttivo('+Menu.Handle+')" ');
	document.write('onMouseOut="DisattivaMenuAttivo()" ');
	document.writeln('>');
	document.writeln('<table border="0" cellpadding="2" cellspacing="0" width="100%" BGColor="'+COLORE_SFONDO_TENDINA+'">');
	for (var i=0;i < Menu.Items.length; i++) {
		switch(Menu.Items[i].Tipo) {
			case TIPO_ITEM:
				document.writeln('<tr>');
				document.write('<td class="Tendina" ID="Menu'+Menu.Handle+'Item'+i+'" style="border-color: '+COLORE_SFONDO_TENDINA+'; "');
				document.write('height="18" ');
				document.write('onMouseOver="ItemInEvidenza('+Menu.Handle+','+i+')" ');
				document.write('onMouseOut="ItemDefault('+Menu.Handle+','+i+')" ');
				document.write('onClick="ItemLink('+Menu.Handle+','+i+')" ');
				document.writeln('>');
				document.writeln('<table border="0" cellpadding="0" cellspacing="0" width="100%">');
				document.writeln('<tr>');
				document.writeln('<td width="10" NoWrap>&nbsp;</td>');
				if (Menu.Items[i].URL != "") document.writeln('<td>'+Menu.Items[i].Nome+'</td>');
										else document.writeln('<td style="color: '+COLORE_URL_NULLO+';">'+Menu.Items[i].Nome+'</td>');
				document.writeln('<td width="10" NoWrap>&nbsp;</td>');
				document.writeln('</tr>');
				document.writeln('</table>');
				document.writeln();
				document.writeln('</td>');
				document.writeln('</tr>');
				break;
			case TIPO_SUBTENDINA:
				var
					SubMenu = Menu.Items[i].Menu;
				document.writeln('<tr>');
				document.write('<td ID="Menu'+Menu.Handle+'Item'+i+'" class="Tendina" height="18" style="border-color: '+COLORE_SFONDO_TENDINA+'; "');
				document.write('onMouseOver="ItemInEvidenza('+Menu.Handle+','+i+')" ');
				document.write('onMouseOut="ItemDefault('+Menu.Handle+','+i+')" ');
				document.writeln('>');
				document.writeln('<table border="0" cellpadding="0" cellspacing="0" width="100%">');
				document.writeln('<tr>');
				document.writeln('<td width="10" NoWrap>&nbsp;</td>');
				document.writeln('<td>');
				document.writeln(SubMenu.Nome);
				document.writeln('</td>');
				document.writeln('<td width="10" NoWrap align="right"><img src="Freccia.gif"></td>');
				document.writeln('</tr>');
				document.writeln('</table>');
				document.writeln('</td>');
				document.writeln('</tr>');	
				break;
			case TIPO_SEPARATORE:
				document.writeln('<tr>');
				document.writeln('<td height="18" background="Linea.gif" style="border-color: '+COLORE_SFONDO_TENDINA+'; ">');
				document.writeln('&nbsp;');
				document.writeln('</td>');
				document.writeln('</tr>');
				break;
		}
	}  
	document.writeln('</table>');
	document.writeln('</div>');
}

function MT_execute() {
	for (var i=0;i<MT_totTendine();i++) {
		DisegnaTendina(MenuIndex[i]);
	}
	// Disegna la barra dei menu
	if (DISTRIBUZIONE_MENU == 0) {
		document.writeln('<table ID="BarraTendina" border="0" cellpadding="0" cellspacing="0" width="100%" align="center" BGColor="'+COLORE_BARRA_MENU+'">');
	} else {
		document.writeln('<table ID="BarraTendina" border="0" cellpadding="0" cellspacing="0" style="width:'+WIDTH_TITOLO_MENU+'px;" align="center" BGColor="'+COLORE_BARRA_MENU+'">');
	}
	document.writeln('<tr>');
	var
		w = 100 / MT_totTendine();
	for (var i=0;i<MT_totTendine();i++) {
		if (MenuIndex[i].Tipo == TIPO_TENDINA) {
			if (DISTRIBUZIONE_MENU == 0) {
				document.write('<td width="'+WIDTH_TITOLO_MENU+'" NoWrap align="center" ');
			} else {
				
				document.write('<td align="center" width="'+w+'%" ');
			}
			document.write('onMouseOver="VisualizzaTendina('+MenuIndex[i].Handle+')" ');
			document.write('onMouseOut="DisattivaMenuAttivo()" ');
			document.writeln('class="TitoloTendina" style="color: '+COLORE_FONT_BARRA_MENU+';">');
			document.writeln('<strong>'+MenuIndex[i].Nome+'</strong>');
			document.writeln('</td>');
		}
	}
	if (DISTRIBUZIONE_MENU == 0) document.writeln('<td width="90%" class="BarraMenu">&nbsp;</td>');
	document.writeln('</tr>');
	document.writeln('</table>');
	SpegniTendineMenu();
}

// ========================================================================================

///////////////////////////////////////////////////
// Funzioni generali per il controllo dei menu
///////////////////////////////////////////////////

//function CalcolaCoordinateMouse() {
//	mouseX = event.x;
//	mouseY = event.y;
//}

function SpegniTendineMenu() {
	if (MenuAttivo == null) {
		var w = document.getElementById("BarraTendina").style.width.substr(0,3);
		ScostamentoMenu = ((document.body.clientWidth - w) / 2) - 10;
		// Spegne tutto
		for (var i=0;i < MT_totTendine();i++) {
			var
				Obj = document.getElementById("Menu"+MenuIndex[i].Handle);
			Obj.style.visibility = "hidden";
			// Aggiorna la posizione delle tendine
			if (OldScostamentoMenu != ScostamentoMenu) {
				Obj.style.left = MenuIndex[i].x + ScostamentoMenu;
			}
		}
	}
	// Calcola lo scostamento dei menu
	OldScostamentoMenu = ScostamentoMenu;
	//document.getElementById("Test").innerHTML = ScostamentoMenu;
	
	// Aggiorna il timer
	if (ControlMenuTimer != -1) window.clearTimeout(ControlMenuTimer);
	ControlMenuTimer = window.setTimeout("SpegniTendineMenu()",800);
}

//document.onmousemove = CalcolaCoordinateMouse;

