
/*
 * 
 * THE MENU
 *
 */

function MenuPanel(htmlElt, parent) {
	/* Declaration des attributs de l'objet */
	var that        = this;
	var panelElt    = null;
	var parentItem  = null;
	var panelItem   = null;
	var closing     = null;
	var dir         = "vertical";

	// Initialisation
	that.panelElt   = htmlElt;
	that.parentItem = parent;
	that.panelItem  = new Array();
	Array.prototype.contains = function(objet) {
		if(objet != null) {
			for(var i=0; i < this.length; i++) 
				if(objet == this[i])
					return i;
		}
		return -1;
	}

	// Mise à jour du style du panel
	if(that.panelElt.getAttribute("ft-menuClass") != null) {
		that.panelElt.className = that.panelElt.getAttribute("ft-menuClass");
		//alert("Changement de classe de l'item");
	}

	if(that.panelElt.getAttribute("ft-menuDir") != null) {
		that.dir = that.panelElt.getAttribute("ft-menuDir");
		//alert(that.dir);
	}
	
	// Enregistrer le lien declanchant de l'item
	var objet = that.panelElt.firstChild;
	while(objet != null) {
		if(objet.nodeName.indexOf("LI") != -1) {					
			that.panelItem.push(new MenuItem(objet, that, that.dir));
		}
		objet = objet.nextSibling;
	}
	
	
	/* Declaration des methodes de l'objet */
	this.getHtmlElt = function () {
		return that.panelElt;
	}
	
	this.openPanel = function() {
		if(that.parentItem != null) {
			// Ouverture du panel
			if(that.closing != null) {
				clearTimeout(that.closing);
			}
			else {
				that.panelElt.style.display = "block";
			}
		
			// Demander l'ouverture de l'item parent
			that.parentItem.show(null);
		}
	}
	
	this.closePanel = function() {
		var toClose = function () {
			that.panelElt.style.display = "none";
			that.closing = null;
		}
		if(that.parentItem != null) {
			// Fermeture du panel
			that.closing = setTimeout(toClose,150);
			// Demander la fermeture de l'item parent
			that.parentItem.hide(null);
		}
	}

	this.selectNext = function(menuItem) {
		if(that.panelItem.contains(menuItem) != -1 && that.panelItem.contains(menuItem) < (that.panelItem.length-1)) {
			that.panelItem[that.panelItem.contains(menuItem)+1].setFocus();
		}
	}
	this.selectPrevious = function(menuItem) {
		if(that.panelItem.contains(menuItem) > 0)
			that.panelItem[that.panelItem.contains(menuItem)-1].setFocus();
	}
	this.selectDown = function() {
		if(that.panelItem.length > 0) {
			that.panelItem[0].setFocus();
		}
	}
	this.selectUp = function(menuItem) {
		if(that.parentItem != null) {
			that.parentItem.setFocus();
		}
	}
}

function MenuItem(htmlElt, parent, direction) {
	/* Declaration des attributs de l'objet */
	var that        = this;
	var itemElt     = null;
	var itemLink    = null;
	var itemPanel   = null;
	var parentMenu  = null;
	var dir         = "vertical";
	
	// Initialisation
	that.itemElt    = htmlElt;
	that.parentMenu = parent;
	that.dir        = direction;
	//alert(that.dir);

	// Enregistrer le lien declanchant de l'item
	var objet = that.itemElt.firstChild;
	while(objet != null) {
		if(objet.nodeName.indexOf("A") != -1) {					
			that.itemLink = objet;
		}
		objet = objet.nextSibling;
	}
	
	// Enregistrer le menu associe a l'item
	var objet = that.itemElt.firstChild;
	while(objet != null) {
		if(objet.nodeName.indexOf("UL") != -1) {					
			that.itemPanel = new MenuPanel(objet, that);
		}
		objet = objet.nextSibling;
	}
	
	// Ajouter les evenements declanchant au lien de l'item
	that.itemLink.onmouseover = function(){
		that.show(that);
	}
	that.itemLink.onfocus = function() {
		that.show(that);
	}
	that.itemLink.onmouseout = function(){
		that.hide(that);
	}
	that.itemLink.onblur = function(){
		that.hide(that);
	}
	that.itemLink.onkeydown = function(e) {
		var evt        = null;
		//alert(that.dir);
		if(that.dir == "horizontal") {
			var prev    = 37;
			var next    = 39;
			var lvlUp   = 38;
			var lvlDown = 40;
		}
		else {
			var prev    = 38;
			var next    = 40;
			var lvlUp   = 37;
			var lvlDown = 39;
		}

		// Compatibilite des evenement entre les differents moteurs de scripts
		if(e)	evt = e;
		else 	evt = event;
		
		switch(evt.keyCode) {
			case prev    : that.previousItem(); break;
			case next    : that.nextItem(); 	break;
			case lvlUp   : that.upItem(); 		break;
			case lvlDown : that.downItem(); 	break;
			default : break;
		}
	}

	// Mettre a jour le style de l'item et de ses composant associe
	if(that.itemElt.getAttribute("ft-menuClass") != null) {
		that.itemElt.className = that.itemElt.getAttribute("ft-menuClass");
		//alert("Changement de classe de l'item");
	}
	if(that.itemLink != null) {
		if(that.itemLink.getAttribute("ft-menuClass") != null) {
			that.itemLink.className = that.itemLink.getAttribute("ft-menuClass");
			//alert("Changement de classe du lien");
		}
	}
	if(that.itemPanel != null) {
		that.itemPanel.getHtmlElt().style.display = "none";
	}
	
	/* Declaration des methodes de l'objet */
	this.getHtmlElt = function () {
		return that.itemElt;
	}
	
	this.setFocus = function() {
		that.itemLink.focus();
	}
	
	this.show = function (itemMenu) {
		if(itemMenu == that && that.itemPanel != null) { 
			// Afficher le menu associe
			that.itemPanel.openPanel();
		}
		else {
			// Afficher les menu parents
			that.parentMenu.openPanel();
		}
	}
	
	this.hide = function(itemMenu) {
	
		if(itemMenu == that && that.itemPanel != null) {
			// Fermer le menu associe
			that.itemPanel.closePanel();
		}
		else 
			// Fermer les menus parents
			that.parentMenu.closePanel();
		
	}

	this.nextItem = function() {
		if(that.parentMenu != null)
			that.parentMenu.selectNext(that);
	}
	this.previousItem = function() {
		if(that.parentMenu != null)
			that.parentMenu.selectPrevious(that);

	}
	this.downItem = function() {
		if(that.itemPanel != null)
			that.itemPanel.selectDown();
	}
	this.upItem = function() {
		if(that.parentMenu != null)
			that.parentMenu.selectUp(that);
	}
}

function Menu(name) {
	/* Test JavaScript OK et DOM OK */
	if( !document.getElementById)
		return false;

	/* Declaration des attributs de l'objet */
	var that          = this;
	var menuContainer = null; 				// => conteneur principal du menu
	var menuRoot      = null;				// => racine du menu

	// Initialisation
	that.menuContainer = document.getElementById(name);
	
	if(that.menuContainer != null) {
		var objet = that.menuContainer.firstChild;
		while(objet != null) {
			if(objet.nodeName.indexOf("UL") != -1) {					
				that.menuRoot = new MenuPanel(objet,null);
			}
			objet = objet.nextSibling;
		}
	}
	else {
		alert("Aucun container de menu [" +name+"]");
		return false;
	}
	
	/* Declaration des methodes de l'objet */
}

/*
 * FIN DU MENU
 */

