/* *******************************
* menu.js
* generation de menus dynamiques
*
************************************************* */

var wait = 500;
var hideTimer;
var menus = Array();
var visibleMenus = Array();
var MINMENUSIZE = 75;
var oldMargin = 0;

// detection de IE+
IE = (document.all) ? 1 : 0;

// Classe menu et sa methode addItem

function menu(idMenu) {	
  var bodyRef = document.body;
  
  this.main = document.createElement("div");	
  this.main.id = "dhtml_menu"+idMenu;
  this.main.className = "dhtmlmenu";
  
  this.idMenu = idMenu;
  this.parent = 0;	
  this.addItem = item;
  this.getId = new Function("return(\""+this.main.id+"\");");
  bodyRef.appendChild(this.main);

  this.depth = 0;
}

function item(text, href, menuToShow, menuIndex) {
  var menuItem = document.createElement("a");
  var menuId = this.idMenu;
  
  menuItem.href = href;
  menuItem.innerHTML = text;
  
  if (menuIndex == 0) {
    menuItem.style.borderTop = 0;
  }
  
  rollover = "hideAfter("+menuId+");";
  
  if (menuToShow != "") {
    rollover += "showMenu('"+menuToShow+"', '"+menuId+"', this);";
    menuItem.style.backgroundImage = 'url(/doc/img/puce.gif)';
  }
  
  rollover += "keepOpen();";
  rollout = "hide();";
  
  menuItem.onmouseover = new Function(rollover);
  menuItem.onmouseout = new Function(rollout);
  
  this.main.appendChild(menuItem);
}

// Fonctions l'affichage/masquage de menu

function showMenu(_menuId, _pmenuId, _parent) {
  var thisMenu = menus[_menuId];
  var elemMenu = document.getElementById("dhtml_menu"+_menuId);

  hideAfter(_pmenuId);
  thisMenu.parent = _parent;
  
  xPos = getLeft(_parent);
  yPos = getTop(_parent);

  // mod la cornue
  if (_pmenuId != -1) {
    thisMenu.depth = menus[_pmenuId].depth + 1;
    
    xPos += 159;

    if (_parent != document.getElementById("dhtml_menu"+_pmenuId).firstChild || thisMenu.depth == 1)
      yPos -= 1;

    elemMenu.style.margin = 0;
    elemMenu.firstChild.style.borderTop = "1px solid white";    
  }
  else {
    if (IE) {
      elemMenu.style.margin = "30px 0 0 0";
    }
    else {
      elemMenu.style.margin = "15px 0 0 0";
    }
    elemMenu.firstChild.style.borderTop = 0;    
  }
  // fin mod
  
  elemMenu.style.display = "block";
  elemMenu.style.left = xPos + "px";
  elemMenu.style.top = yPos + "px";

  elemMenu.style.visibility = "visible";
  visibleMenus.push(_menuId);
  

  // mod la cornue
  if (thisMenu.parent != 0) {
    if (thisMenu.parent.firstChild.tagName == "IMG") {
      var thisImg = thisMenu.parent.firstChild;
      
      index = thisImg.src.lastIndexOf(".gif");
      
      if (thisImg.src.indexOf("_over.gif") == -1)
	thisImg.src = thisImg.src.substring(0,index) + "_over.gif";
    }
    //thisMenu.parent.style.backgroundColor = MOVERBGCOLOR;
    //thisMenu.parent.style.color = MOVERFTCOLOR;
  }
  // fin mod
  

}

function hideMenu(_menuId) {
  var menuId = "dhtml_menu"+_menuId;	
  var thisMenu = menus[_menuId];
  var elemMenu = document.getElementById(menuId);
  
  // mod la cornue
  if (thisMenu != -1 && thisMenu.parent != 0) {
    if (thisMenu.parent.firstChild.tagName == "IMG") {
      var thisImg = thisMenu.parent.firstChild;
      
      index = thisImg.src.lastIndexOf("_over.gif");
      
      if (thisImg.src.indexOf("_over.gif") != -1)
	thisImg.src = thisImg.src.substring(0,index) + ".gif";
    }
  }
  // fin mod

  elemMenu.style.visibility = "hidden";
}

// Fonctions de recuperation des coordonnées et du style

function getTop(_elem) {
  yPos = _elem.offsetTop;
  tempEl = _elem.offsetParent;
  while (tempEl != null) {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
 return yPos;
}

function getLeft(_elem) {
  xPos = _elem.offsetLeft;
  tempEl = _elem.offsetParent;
  while (tempEl != null) {
    xPos += tempEl.offsetLeft;
    tempEl = tempEl.offsetParent;
  }
  return xPos;
}

function getStyle(x,styleProp) {
  if (x.currentStyle)
    var y = x.currentStyle[styleProp];
  else if (window.getComputedStyle)
    var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
  return y;
}

// Fonction gerant le masquage automatique des menus

function keepOpen() {
  if (hideTimer != null)
    clearTimeout(hideTimer);
}

function hide() {
  hideTimer = setTimeout("hideAll()", wait);
}

function hideAll() {
  if (visibleMenus.length > 0) {
    limit = visibleMenus.length;
    for (i=0; i<limit; i++) {
      menuId = visibleMenus.pop();
      hideMenu(menuId);
    }
  }
}

function hideAfter(_menuId) {
  if (visibleMenus.length > 0 && visibleMenus[visibleMenus.length-1] != _menuId) {
    hideMenu(visibleMenus.pop());
  }
}


// Fonction d'appel publique de menu

function callMenu(idRef, parentRef) {
  var idMenu = "dhtml_menu"+idRef;
  
  hideAll();
  
  if (document.getElementById(idMenu) != null) {   
    showMenu(idRef, '-1', parentRef);
  }
  keepOpen();
}