var Menus =
    ["menu",                             // Prefix des classes de style à créer (XXX_class, XXX0_item, XXX_subclass, XXX1_item)
     500,                                // Delais de disparition du sous-menu
     0,                                  // Active(1) / Désactive(0) l'effet de transparence des sous-menus
     [
       [ "Accueil", "./accueil.htm", "centre",
       ["", "#"]
       ], 
     [ "L'Equipe", "./html/equipe.html", "centre",
       ["", "#"] 
       ], 
     [ "Programme 2011", "./html/programme.html", "centre",
       ["", "#"] 
       ],
     [ "Le Petit Canard Boîteux", "./html/Canard.htm", "centre",
       ["", "#"] 
       ],
	 [ "Partenaires", "./html/sponsors.html", "centre",
       ["", "#"] 
       ],
     [ "Contacts/Adhésion", "./html/contact.html", "centre",
       ["", "#"]
       ]
     ]
  ];


var MenusArr = new Array();
var divtohide = new Array();
var moznav = false;

function insertMenu(m)
{
    if (!m|| m.length==0) return;
    
    var newMenu = new Menu(m);
    var l = MenusArr.push(newMenu);

    newMenu.num = l-1;
    constructMenus(l-1);
}

function Menu(m)
{
    this.name = m[0];
    this.delay = m[1];
    this.menu = m[3];
    this.timeout_id = 0;
    this.num = 0;
    this.enable_opacity = m[2];
    this.opacity = 0.00;

    this.constructSubMenus = _constructSubMenus;
    this.showSubMenu = _showSubMenu;
    this.hideSubMenu = _hideSubMenu;

    this.subMenusStr = this.constructSubMenus();
}



/* window.onlaod = init(); 

  if(navigator.appName=="Netscape")
    moznav = true;
  else
    moznav = false;
*/

function constructMenus(l) {
    var m = MenusArr[l];
    document.write("<div id='"+m.name + l +"_div' class='" + m.name + "_class'"
		   + "onmouseover='window.clearTimeout(MenusArr[" + l + "].timeout_id);' onmouseout='MenusArr["+ l + "].hideSubMenu();' >");
    for (var i=0; i<m.menu.length; i++) {
	var _id = m.menu[i][0];
	var _href = m.menu[i][1];
	var _target = m.menu[i][2];
	document.write("<a class='" + m.name + "0_item' id='" + m.name + l + "0_item" + i + "' target='" + _target + "' href='" + _href
		       + "' onMouseOver='MenusArr[" + l + "].showSubMenu(" + i + ");return false;'>" + _id + "</a>");
	
    }
    document.write("</div>");
    document.write("<div id='" + m.name + l + "_subdiv' class='" + m.name + "_subclass' style='visible: hidden; position: absolut; top: 0; left: 0;'" 
		   + "onmouseover='window.clearTimeout(MenusArr["+ l +"].timeout_id);' onmouseout='MenusArr["+ l + "].hideSubMenu();' >");
    document.write("</div>");
}

function _constructSubMenus() {
    var s;
    var res= new Array();

    for(var i=0; i<this.menu.length; i++) {
		s = "";
		var t = this.menu[i][0];
		var tlien = this.menu[i][1];
		for(var j=3; j < this.menu[i].length; j++) {
	    	st = this.menu[i][j][0];
	    	stlien = this.menu[i][j][1];
	    	if (st == "") {
		    	res[i] = "";
		    	break;
	        }
	    	s = s + "<a class='"+ this.name + "1_item' href='" + stlien + "' alt='" + stlien + "'> "+ st +" </a>";
		}
		res[i] = s;
    }
    return(res);
}

/*
function getInfos(e) {
    var infos = e.id + ": <br>";
    var infos += "top : " + e.offsetTop + ", left : " + e.offsetLeft + ", height : " + e.offsetHeight + ", width : " + e.offsetWidth + "<br>";
    var infos += "margin : " + e.style.margin + "padding : " + e.style.padding + "<br>";

    return (infos);
}
*/

function _showSubMenu(i) {
    var dbg = document.getElementById("debug");

    var div0 = document.getElementById(this.name + this.num + "_div");
    if (!div0) return;

    var div1 = document.getElementById(this.name + this.num + "_subdiv");
    if (!div1) return;

    var a0 = document.getElementById(this.name + this.num + "0_item" + i);
    if (!a0) return;

    
    div1.style.visible='hidden';
	if (this.subMenusStr[i] == "") return;
	
    div1.innerHTML = this.subMenusStr[i];

    var a0Height = getTop(div0) + div0.offsetHeight;
    var a0Left = getLeft(a0);

    div1.style.top = a0Height + "px";
    div1.style.left = getLeft(a0) + "px";

    if (this.enable_opacity) {
	this.opacity = 0.025;
	div1.style.opacity = this.opacity;
    }

    div1.style.visibility = 'visible';

    if (dbg)
	dbg.innerHTML = "Navigateur: " + navigator.appName +  "<br>top calculé : " + tc + "<br>" + getInfos(m0) + getInfos(a0) + getInfos(m1);

    if (this.enable_opacity)
	_setOpacity(this.num);
}

function _setOpacity(n) {
    var mobj = MenusArr[n];
    var div1 = document.getElementById(mobj.name + mobj.num + "_subdiv");
    if (!div1) return;

    div1.style.opacity = mobj.opacity;
    mobj.opacity += 0.05;

    if (mobj.opacity < 1.0) {
	setTimeout("_setOpacity("+n+")", 75);
    }
}

function _hideSubMenu() {
    divtohide[this.num] = this.name + this.num;
    this.timeout_id = setTimeout("doRealHide("+this.num+")", this.delay);
}

function doRealHide(n) {
    var ndiv = divtohide[n];
    var submenudiv = document.getElementById(ndiv + "_subdiv");

    if (!submenudiv) return;
    submenudiv.style.visibility="hidden";
}

function _overMenuItem() {
    window.clearTimeout(this.timeout_id);
}

function getLeft(elt) {
    if (elt.offsetParent)
	return (elt.offsetLeft + getLeft(elt.offsetParent));
    else
	return (elt.offsetLeft);
}

function getWidth(elt) {
    if (elt.offsetParent)
	return (elt.offsetWidth + getWidth(elt.offsetParent));
    else
	return (elt.offsetWidth);
}

function getTop(elt) {
    if (elt.offsetParent)
	return (elt.offsetTop + getTop(elt.offsetParent));
    else
	return (elt.offsetTop);
}

function getHeight(elt) {
    if (elt.offsetParent)
	return (elt.offsetHeight + getHeight(elt.offsetParent));
    else
	return (elt.offsetHeight);
}

