/*
** v2.0.20060106
**
** gMenu is a customizable collection of JavaScript routines which can be used
** to quickly create platform independent drop down menus.
**
** gMenu has been tested on IE 6.0, Netscape 7.1, FireFox 1.0, and Opera 8.5
**
** Copyright (C) 2004-2005  Brain Book Software LLC (formfields.com, info@formfields.com)
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
** See http://www.gnu.org/ for an up-to-date copy of the GPL.
*/

var IE_4 = document.all;
var NS_6 = document.getElementById && !document.all;

// The maximum depth of sub menus (not including the root menu)
var MAX_MENUS = 5;

// The number of milliseconds to delay before hiding a menu
var HIDE_MENU_DELAY = 500;

// The number of pixels to offset the top-left corner of the top menu from a
// root menu item.
var TOP_MENU_LEFT_OFFSET = 0;

// This array keeps track of whether the mouse is over a given menu.
var menuFocus = new Array();
for (i = 1; i <= MAX_MENUS; i++)
	menuFocus["menu" + i] = false;

var activeRootMenuId;

var curItemHasChildren = false;

var hideMenuCounter = 0;

function getPosOffset(what, offsetType) {
	var totalOffset= (offsetType == "left") ? what.offsetLeft : what.offsetTop;
	var parentEl = what.offsetParent;
	while (parentEl != null) {
		totalOffset = (offsetType == "left") ? totalOffset + parentEl.offsetLeft : totalOffset + parentEl.offsetTop;
		parentEl = parentEl.offsetParent;
	}
	return totalOffset;
}

function stripPx(x) {
	if (x.length <= 2)
		return x;
	return eval(x.substr(0,x.length-2));
}

function addDimensions(x1, x2) {
	return (stripPx(x1) + stripPx(x2)) + 'px';
}

// This is needed so that a user doesn't cause a js error by doing a mouse over on the menu
// before the menu has completely loaded.
function enableRootMenus() {
	var i = 1;
	while ( (rootMenu = document.getElementById("rootMenu" + (i++))) ) {
		rootMenu.disabled = false;
	}
}

function focusMenu(menu) {
	menuFocus[menu.id] = true;
	
}

function initGMenu() {
	/*html = '';
	for (i = 1; i <= MAX_MENUS; i++) {
		//html += '<div id="menuBase' + i + '" class="subMenuBase" style="position:absolute;top:0px;left:0px;visibility:hidden;">'
		//	+ '<iframe frameborder="0" width="100%"></iframe></div>';
		html += '<div id="menu' + i + '" class="subMenu" style="position:absolute;top:0px;left:0px;visibility:hidden;" onmouseout="delayHideMenu(this);" onmouseover="focusMenu(this)">'
			+ '</div>';
	}
	document.body.innerHTML += html;*/
	//enableRootMenus();
}

function getHighlightedClass(curClass) {
	if (curClass.substr(0, 12) == 'subMenuItemL')
		return 'subMenuItemLeafHighlight';
	else
		return 'subMenuItemHighlight';
}

function getNotHighlightedClass(curClass) {
	if (curClass.substr(0, 12) == 'subMenuItemL')
		return 'subMenuItemLeaf';
	else
		return 'subMenuItem';
}

// Highlight current item and "unhighlight" other items in current menu
function setHighlighting(selectedNode) {
	for (i = 0; i < (selectedNode.parentNode).childNodes.length - 1; i++) {
		var curNode = (selectedNode.parentNode).childNodes[i];
		if (curNode.id == selectedNode.id) {
			if (curNode.className != getHighlightedClass(curNode.className))
				curNode.className = getHighlightedClass(curNode.className);
		} else {
			if (curNode.className != getNotHighlightedClass(curNode.className))
				curNode.className = getNotHighlightedClass(curNode.className);
		}
	}
}

function createLink(text, url, cssClass) {
	if (url == null)
		return text;
	//return '<a class="' + cssClass + '" href="' + url + '" style="height:100%;width:100%;">' + text + '</a>';
	return text;
}

// Fill in the sub menus. This is done every time a menu pops up.
function drawSubMenu(menuId, menuArray,orientation,style,menuName) {
	var menu = document.getElementById(menuId);
	//alert (menuId);
	var html = '';
//alert ("draw sub menu");
	if (orientation=='h') 
		html+="<div class='submenuhorizontal' style='width:600px;height:30px;'>"
	else {
		html+="<div class='submenuvertival' style='"+style+"'>"
		//menu1.style+=style
		//alert (style)
	}
	for (i = 0; i < menuArray.length; i++) {
		if (orientation=='h') {
			html += '<a href="#" id="' + menuId + "-" + i + '" class="' + (menuArray[i][2] == null ? 'menuh2' : 'subMenuItem"') + '"'
			+ ' onmouseover="' + (menuArray[i][2] == null ? '' : 'showMenu(\'' + menuId + '\', ' + menuArray[i][2] + ', this);') + 'highlightIfNeeded(this);"'
			+ (menuArray[i][1] != null ? ' style="cursor:pointer;" onclick="window.location=\'' + menuArray[i][1] + '\';"' : '')
			+ '>'
			+ createLink(menuArray[i][0], menuArray[i][1], 'subMenuItem')
			+ '</a>'

		}else{
		
		html += '<a href="#" id="' + menuId + "-" + i + '" class="menuv' + menuName.substr(9, 9) + '"  onclick="window.location=\'' + menuArray[i][1] + '\';" >'
			+ createLink(menuArray[i][0], menuArray[i][1], 'subMenuItem')
			+ '</a><br>';
		}
	}
	if (orientation=='h') 
		html += '<div class="menuBottom"></div>';
	menu.innerHTML = '</div>' + html;
//alert (html);

			//+ ' onmouseover="' + (menuArray[i][2] == null ? '' : 'showMenu(\'' + menuId + '\', ' + menuArray[i][2] + ', this);') + 'highlightIfNeeded(this);"'
			//+ (menuArray[i][1] != null ? ' style="cursor:pointer;'+style
	return menu;
}

// Shows a level 1 menu, right below the root menus.
// Had to adjust the top location for netscape?????????
function showTopMenu(curMenu, menuArray,orientation,style) {
//alert ("topmenu");
	var columnOffset=0;//document.getElementById('centering').offsetLeft;
	//alert ("test"+columnOffset);
	curMenu.className = curMenu.id + 'Highlight';
	
	/*if (menuFocus['menu1'])
		subMenu = document.getElementById('menu1');
	else*/
		subMenu = drawSubMenu('menu1', menuArray,orientation,style,curMenu.id);
		//alert (style);
	//subMenu.style.top = curMenu.offsetParent.offsetTop + curMenu.offsetParent.offsetHeight;
	//subMenu.style.top = curMenu.offsetParent.offsetTop + curMenu.offsetTop + curMenu.offsetHeight;
	
	subMenu.style.top = getPosOffset(curMenu, "top") + curMenu.offsetHeight +2+ "px";
	if (orientation=='h')
	{
	//subMenu.style.left = 4 + "px";
	//alert (menuArray[0][0]);
	if (menuArray[0][0]=='En bref' ) {
	subMenu.style.left = getPosOffset(curMenu, "left") + (TOP_MENU_LEFT_OFFSET - columnOffset) - 14 + "px";
	//alert ("yoyo");
	}
	else  
	subMenu.style.left = getPosOffset(curMenu, "left") + TOP_MENU_LEFT_OFFSET - columnOffset -8 + "px";
	}
	else
	subMenu.style.left = getPosOffset(curMenu, "left") + TOP_MENU_LEFT_OFFSET - columnOffset -2 + "px";
	//subMenu.style.left = curMenu.offsetParent.offsetLeft + curMenu.offsetLeft;
	//var subMenuBase = document.getElementById('menuBase1');
	//subMenuBase.childNodes[0].style.height = subMenu.offsetHeight;
	//subMenuBase.style.top = subMenu.style.top;
	//subMenuBase.style.left = subMenu.style.left;
//subMenu.style.left ="800px";
	subMenu.style.visibility = 'visible';
	//subMenuBase.style.visibility = 'visible';
	menuFocus[curMenu.id] = true;
	//alert(curMenu.id);
	activeRootMenuId = curMenu.id;
	curItemHasChildren = true;
	hideMenus2();
}

function highlightIfNeeded(curItem) {
	if (curItem.className != "subMenuItemHighlight")
		setHighlighting(curItem);
	curItemHasChildren = !(curItem.className.substr(0, 15) == "subMenuItemLeaf");
}

// Shows a level2..leveln menu, directly to the right of the selected item.
function showMenu(curMenuId, menuArray, curItem) {
	if (curItem.className == "subMenuItemHighlight")
		return;
	var curMenu = document.getElementById(curMenuId);
	var subMenuId = 'menu' + (eval((curMenuId).substr(4,1)) + 1);
	//setHighlighting(curItem);
	//var subMenuBaseId = 'menuBase' + (eval((curMenuId).substr(4,1)) + 1);
	//var subMenuBase = document.getElementById(subMenuBaseId);
	var subMenu;
	if (menuFocus[subMenuId])
		subMenu = document.getElementById(subMenuId);
	else
		subMenu = drawSubMenu(subMenuId, menuArray);
	//subMenu.style.top = curItem.offsetParent.offsetTop + curItem.offsetTop;
	//subMenu.style.left = stripPx(curMenu.style.left) + curMenu.offsetWidth;
	subMenu.style.top = getPosOffset(curMenu, "top") + curItem.offsetTop + "px";
	subMenu.style.left = getPosOffset(curMenu, "left") + curMenu.offsetWidth + "px";
	//subMenuBase.style.top = subMenu.style.top;
	//subMenuBase.childNodes[0].style.height = subMenu.offsetHeight;
	//subMenuBase.style.left = subMenu.style.left;
	subMenu.style.visibility = 'visible';
	//subMenuBase.style.visibility = 'visible';
	var subSubMenuId = 'menu' + (eval((curMenuId).substr(4,1)) + 2);
	hideMenu(subSubMenuId, subMenuId);
}

// Recursively check all sub menus of the current menu.
// If the mouse has left a menu and all its sub menus then hide that menu.
function hideMenu(curMenuId, parentId) {
	var curMenu = document.getElementById(curMenuId);
	var hideChild = true;
	var childId = 'menu' + (eval((curMenu.id).substr(4,1)) + 1);

	//var curMenuBaseId = 'menuBase' + (eval((curMenu.id).substr(4,1)));
	//var curMenuBase = document.getElementById(curMenuBaseId);

	var child = document.getElementById(childId);
	if (child != null) {
		hideChild = hideMenu(child.id, curMenu.id);
	}

	if (!menuFocus[curMenuId] && hideChild && (!menuFocus[parentId] || !curItemHasChildren)) {
		curMenu.style.visibility = 'hidden';
		return true;
	} else {
		return false;
	}

}

function hideMenus() {
	if (--hideMenuCounter > 0)
		return;
	hideMenus2();
}

function hideMenus2() {
	var i = 1;
	var menusHidden = hideMenu('menu1', activeRootMenuId);
	while ( (rootMenu = document.getElementById("rootMenu" + (i++))) ) {
		if ( (!menuFocus[rootMenu.id] && activeRootMenuId == rootMenu.id && menusHidden)
		     || (activeRootMenuId != rootMenu.id && rootMenu.className == rootMenu.id + "Highlight") ) {
			rootMenu.className = rootMenu.id;
		}
	}
}

function delayHideMenu(menu) {
	menuFocus[menu.id] = false;
	hideMenuCounter++;
	setTimeout('hideMenus()', HIDE_MENU_DELAY);
}


// Pour nouveau layout de Pierre

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

