//////////////////////////////////////////////////////////////////////////////////
//	OBJET	:	Classe et fonctions javascript, permettant la gestion du menu	//
//				principal d'une page DHTML										//
//	DATE	:	24 octobre 2006													//
//	AUTEUR	:	GOLINSKI Ludwig													//
//////////////////////////////////////////////////////////////////////////////////



// VARIABLE GLOBALE REPRESENTANT LE MENU PRINCIPAL D'UNE PAGE
var mainMenu 

// CLASSE PERMETTANT DE STOCKER ET D'AFFICHER LES ITEMS DU MENU DE LA PAGE
function MainMenu( menuStyle, left, top )
{
	this.left = left
	this.top = top
	
	this.enabled = true
	this.menu = new Menu( 999, menuStyle )
	this.menu.SetToMainMenu()
	
	// Méthodes de la classe
	this.Add = MainMenu_Add
	this.Start = MainMenu_Start
}

// MÉTHODE PERMETTANT D'AJOUTER UN ITEM AU MENU PRINCIPAL
function MainMenu_Add( imageItem, texteItem, styleItem, styleOnFocus )
{
	// Recherche le prochain emplacement libre du tableau
	var indice = 0
	while( this.menu.tableauItems[ indice ] != null )
		indice ++

	// Crée puis stocke l'item
	this.menu.tableauItems[ indice ] = new Item( this.menu.index + "_" + indice, imageItem, texteItem, styleItem, styleOnFocus )

	// Retourne le menu pour pouvoir y ajouter des items
	return this.menu.tableauItems[ indice ]
}

// MÉTHODE PERMETTANT DE CONSTRUIRE LE MENU PRINCIPALE. A APPELER EN DERNIER POUR QUE LES MENU SOIENT AU PREMIER PLAN
function MainMenu_Start( menuStyle )
{
	// Capture les évènements (pour les navigateurs Netscape)
	if( ! document.all )
		document.captureEvents( Event.MOUSEMOVE | Event.CLICK | Event.SELECTSTART )

	// Modifie les fonctions à appeler lorsqu'un évènement se produit dans la page

	document.onmousemove = FocusChange
	document.onclick = OnSelect

	//document.onselectstart = NoMenu

	// Construit le menu ,ces items et ces sous-menus
	this.menu.Draw()
	SelectVisible("hidden",document.getElementsByTagName('select'));

}

// FONCTION APPELÉE LORSQUE L'UTILISATEUR CLIQUE DANS LA PAGE
function OnSelect( evenementSouris )
{


	// Fait appel à la méthode du menu contextuel actif
	if( menuContext != null && menuContext.tableauMenus.length > 0 )
	{
		if( ! menuContext.tableauMenus[ menuContext.indexMenuActif ].OnSelect( evenementSouris ) && mainMenu != null )
		{
			// Fait appel à la méthode du menu actif
			mainMenu.menu.OnSelect( evenementSouris )
		}
	}
	else if( mainMenu != null && mainMenu.enabled )
		mainMenu.menu.OnSelect( evenementSouris )
}

// FONCTION APPELÉE LORSQUE LE CURSEUR SE DÉPLACE SUR LA PAGE, PERMETTANT DE DÉTECTER PUIS MODIFIER L'ITEM AYANT LE FOCUS
function FocusChange( evenementSouris )
{

	// Fait appel à la méthode du menu contextuel actif
	if( menuContext != null && menuContext.tableauMenus.length > 0 )
	{
		if( ! menuContext.tableauMenus[ menuContext.indexMenuActif ].FocusChange( evenementSouris ) && mainMenu != null && mainMenu.enabled )
		{
			// Fait appel à la méthode du menu actif
			mainMenu.menu.FocusChange( evenementSouris )
		}
	}
	else if( mainMenu != null && mainMenu.enabled )
		mainMenu.menu.FocusChange( evenementSouris )
}

// FONCTION APPELÉE LORSQUE L'UTILISATEUR COMMENCE UNE SÉLECTION OU FAIT UN CLICK DROIT
// ( PERMET D'ARRÊTER L'ACTION )
function NoMenu()
{
	return false
}

// FONCTION APPELÉE LORS D'UN CLICK SUR LA PAGE, PERMETTANT DE CACHER LE MENU ACTIF
function HideMenu()
{	

	if( mainMenu != null )
		mainMenu.menu.Hide()

	// Vérifie que le menu actif est affiché
	if( menuContext != null && menuContext.tableauMenus[ menuContext.indexMenuActif ] != null && menuContext.tableauMenus[ menuContext.indexMenuActif ].isVisible )
	{
		// Récupère l'instance sur le menu actif et le cache
		menuContext.tableauMenus[ menuContext.indexMenuActif ].Hide()
	}

	// Retourne false pour que le menu contextuel du navigateur ne se lance pas
	// si la fonction a été appelé lors d'un click droit
	return false
}



// FONCTION PERMETTANT D'OBTENIR UN ELEMENT DE LA PAGE, D'APRES SON ID
function GetElement( idElement )
{
	if( document.all )
		return document.all[ idElement ]

	return document.getElementById( idElement )
}

