var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;
var imageDir = "/sitedesign/oisehalatte/images/";
var homeDir = "/com";

loaded = false;
menuLoaded = false;

// mouse coordinates
xMin = 0;
xMax = 0;
yMin = 0;
yMax = 0;

scrollPos = 0; // IE: handling scrollbar
currentMenu = 0; // selected menu
menuLRef = ""; // NN4: refer to image i layer

// Fix NS4 resize bug
function reDo() {
	if (innerWidth != origWidth || innerHeight != origHeight) {
		location.reload();
	}
}
if (nn4) {
	var origWidth = innerWidth;
	var origHeight = innerHeight;
	onResize = reDo;
}

// init layer-objects
function init() {
	if (nn4) {
		menuL = document.menubar;
		menuC = document.menucontent;
		menuLRef = "menubar.document.";
		document.captureEvents(Event.MOUSEMOVE)
	} else	if (ie4) {
		menuL = document.all.menubar.style;
		menuC = document.all.menucontent.style;
		cursor = "hand";
	} else if (dom) {
		menuL = document.getElementById("menubar").style;
		menuC = document.getElementById("menucontent").style;
		cursor = "pointer";
	}
}

function initMenu(dest) {

	HTML = '<table bgcolor="#3266CC" cellpadding="0" cellspacing="0" border="0" align="left">';
	HTML += '<tr>';
	
	for (t=0; t<menu.length; t++)
	{
		if(t!=0)
		var rubrique = menu[t][1];
		
//		if ( rubrique != "/article/archive/144/")
//		alert ( rubrique );
//		else
//		alert ( "test" );
			
//		alert ( rubrique );
//			if ( rubrique = "Les communes membres")
//			{
//			var rubrique = "oui";
//			}
//			else
//			{
//			var rubrique = "non";
//			}
		
		
		HTML += '<td bgcolor="#3266CC" width="1" valign="top" align="center"><img src="' + imageDir + 't05.png" width="1" height="18" border="0"></td>';
		if ( rubrique != "/article/archive/144/")
		HTML += '<td bgcolor="#3266CC"><img src="' + imageDir + 'espace.png" width="1" height="18" id="m' + t + '_1" name="m' + t + '_1" border="0"></td><td align="center" bgcolor="#3266CC"><a href="#" class="Topmenu" onMouseover="over(' + t + ');window.status=\''+ menu[t][0] +'\';return true;">&nbsp;&nbsp;' + menu[t][0] +'&nbsp;&nbsp;</a></td>';
		else
		HTML += '<td bgcolor="#3266CC"><img src="' + imageDir + 'espace.png" width="1" height="18" id="m' + t + '_1" name="m' + t + '_1" border="0"></td><td align="center" bgcolor="#3266CC"><a href="'+ rubrique +'" class="Topmenu" onMouseover="over(' + t + ');window.status=\''+ rubrique + '\';return true;">&nbsp;&nbsp;' + menu[t][0] +'&nbsp;&nbsp;</a></td>';


	}
	HTML += '</tr></table>';

	if (nn4) {
		document.menubar.document.open();
		document.menubar.document.write(HTML);
		document.menubar.document.close();
	} else	if (ie4) {
		menubar.innerHTML = HTML;
	} else if (dom) {
		document.getElementById("menubar").innerHTML = HTML;
	}
	menuL.top = ysPos('posGif');	
	menuL.left = xsPos('posGif');
	menuL.visibility = "visible";
	menuLoaded = true;
}


function ysPos(what){
	if(ie4)
		return yIE(eval('document.'+what));
	else if(nn4)
		return eval('document.'+what+'.y');
	else if(dom)
		return yIE(document.getElementById(what));
}

function xsPos(what){
	if(ie4)
		return xIE(eval('document.'+what));
	else if(nn4)
		return eval('document.'+what+'.x');
	else if(dom)
		return xIE(document.getElementById(what));
}

function yIE(what){
	if(what.offsetParent)
		return parseInt(what.offsetTop + yIE(what.offsetParent));
	else
		return parseInt(what.offsetTop);
}

function xIE(what){
	if(what.offsetParent)
		return parseInt(what.offsetLeft + xIE(what.offsetParent));
	else
		return parseInt(what.offsetLeft);
}

function xsHeight(what) {
	if(ie4)
		return(parseInt(eval('document.all.' + what + '.scrollHeight')));
	else if(nn4)
		return(eval('document.' + what + '.clip.bottom'));
	else if(dom)
		return(parseInt(eval('document.getElementById("' + what + '").offsetHeight')));
}

function xsWidth(what) {
	if(ie4)
		return(parseInt(eval('document.all.' + what + '.scrollWidth')));
	else if(nn4)
		return(eval('document.' + what + '.clip.right'));
	else if(dom)
		return(parseInt(eval('document.getElementById("' + what + '").offsetWidth')));
}

var menuTimer = null;

mouseY = 0;
mouseX = 0;

// capture mouse movement
function move(e) {
	if(ie4){
		mouseY = event.clientY;
		mouseX = event.clientX;
	}
	else if(nn4 || dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}	
	
	if((mouseX < xMin || mouseX > xMax || mouseY < yMin || mouseY > yMax) && currentMenu >=0 && menuLoaded) {
		menuTimer = setTimeout('hideMenu()',500);
	} else {
		clearTimeout(menuTimer);
	}
}

document.onmousemove = move;

function hideMenu() {
	if ((mouseX < xMin || mouseX > xMax || mouseY < yMin || mouseY > yMax) && currentMenu >=0 && menuLoaded) {
		menuC.visibility = "hidden";
		currentMenu = 0;
	}
}

if (ie4 || dom) {
	window.onresize = posMenuNav;
}

function posMenuNav() {
	menuL.left = xsPos('posGif');
}

function makeMenuContent(what) {

	HTML = '';
	if (menu[what][2])
	{
	HTML += '<table cellpadding="0" cellspacing="0" border="0"><tr><td><img src="' + imageDir + 'espace.png" width="11" height="19"></td><td><font color="#FFFFFF"><a class="TopmenuDHTML" href="' + menu[what][1] + '" onMouseover="window.status=\''+ menu[what][0] +'\';return true;"></a></font></td><td><img src="' + imageDir + 'espace.png" width="11" height="19"></td></tr></table>';
	HTML += '<table cellpadding="1" cellspacing="0" border="0" bgcolor="#3366CC"><tr><td><table cellpadding="3" cellspacing="0" border="0" width=140>';
	for (t=2; t<=menu[what].length-1; t=t+2) {
		if ((t+1)/2 == localPage && what == menuPage) {
				HTML += '<tr><td><img src="' + imageDir + 'espace.png" width="1" height="1">' +  menu[what][t] + '</a></td></tr>';
		} else {
			if (nn4)
				HTML += '<tr><td><img src="' + imageDir + 'espace.png" width="7" height="1"><a href="' + menu[what][t+1] + '" >' +  menu[what][t] + '</a></td></tr>';
			else
				HTML += '<tr><td height="19" class="menuSub'+ what +'" style="cursor:'+ cursor + '" id="sub' + t + '" name="sub'+ t  + '" onMouseover="subOver(' + t + ');window.status=\''+ menu[what][t] +'\';return true;" onMouseout="subOut(' + t + ')" onClick="location.href=\''+ menu[what][t+1] + '\'">' + menu[what][t] + '</td></tr>';
		}
	}
	HTML += '</table></td></tr></table><br>';
	}
//	alert(HTML);
	x = xsPos(menuLRef+'m'+what+'_1');
	y = ysPos(menuLRef+'m'+what+'_1');

	if (nn4) {
		document.menucontent.document.open();
		document.menucontent.document.write(HTML);
		document.menucontent.document.close();
		x += xsPos('posGif');
		y += ysPos('posGif');
	} else 	if (ie4) {
		menucontent.innerHTML = HTML;
	} else if (dom) {
		document.getElementById("menucontent").innerHTML = HTML;
	}

	menuC.left = x + 3;
	menuC.top = y;
	menuC.visibility = "visible";
	// mouse-over area for menus
	if (ie4) {
		scrollPos = document.body.scrollTop;
	}
	xMin = x;
	xMax = x + xsWidth('menucontent');
	yMin = y - scrollPos
	yMax = y + xsHeight('menucontent')-scrollPos;

	currentMenu = what;
}

function subOver(what) {
	if (ie4) {
		bg1 = eval('sub' + what);
	} else {
		bg1 = document.getElementById('sub' + what);
	}
	bg1.style.backgroundColor = "#3366CC";
	bg1.style.color = "#F7F9FD";
}

function subOut(what) {
	if (ie4) {
		bg1 = eval('sub' + what);
	} else {
		bg1 = document.getElementById('sub' + what);
	}
	bg1.style.backgroundColor = "#F7F9FD";
	bg1.style.color = "#3366CC";
}

function over(what) {
	if (currentMenu > 0) {
		menuC.visibility = "hidden";
	}
	currentMenu = what;
	makeMenuContent(what);
}

function out(what) {
	menuC.visibility = "hidden";
	currentMenu = 0;
}

