var isNS = false;
var isIE = false;
var isW3C = false;
if (navigator.appName.indexOf('Netscape')>-1) {isNS=true; isIE=false; isW3C=false;}
if (document.getElementById) {isNS=false; isIE=false; isW3C=true;}
if (document.all) {isNS=false; isIE=true; isW3C=false;}
var isMac = (navigator.appVersion.indexOf('Macintosh')>=0) ? true: false;

var level=0; //niveau d'imbrication du calque actuel, mise à zéro
var indice=new Array(); // tableau d'indices pour chaque niveau
indice[0]=0; //mise à zéro de l'indice de la rubrique du niveau 0
var layerIndice=new Array(); //chaîne indice pour nommage des calques
var layerLength=new Array();
var currentLayer=new Array();
var layerTab=new Array();
var pos_x=new Array();
var pos_y=new Array();
pos_x[0]=8;
pos_y[0]=0;

function layerObject(name,x,y) {
	this.x=x;
	this.y=y;
	this.name=name;
	this.Html="";
	}

function displayLayer(l) { zl_hd(l,(isNS)?'show':'visible'); }

function hideLayer(l) { zl_hd(l,(isNS)?'hide':'hidden'); }

function zl_hd(l,s) {
if (isNS) zl_lref(l).visibility = s;
else zl_lref(l).style.visibility=s;
}

function zl_lref(l) {
if (isNS) return document.layers[l];
if (isIE) return document.all[l];
if (isW3C) return document.getElementById(l);
}

function timer_hide_all(menu) {
	window.will_hide=setTimeout("hide_all()",500);
	}

function hide_all(menu) {
	for(i=1;i<layerTab.length;i++) {
		layerTab[i]!=""?hideLayer(layerTab[i]):"";
		layerTab[i]="";
		}
	}

function hide_part(subLevel,menu) {
	for(i=subLevel;i<layerTab.length;i++) {
		layerTab[i]!=""?hideLayer(layerTab[i]):"";
		layerTab[i]="";
		}
	}

function calcIndice() {
	l_ind="";
	for(i=0;i<=level;i++) {
		l_ind+=indice[i];
		}
	return l_ind;
	}

function calcNextIndice() {
	l_ind="";
	for(i=0;i<level;i++) {
		l_ind+=indice[i];
		}
	next=indice[level]+1;
	l_ind+=next;
	l_ind+="0";
	return l_ind;
	}


function calcActions(menu,Ind,hasSubMenu) {
	l_indice=calcNextIndice();
	subLevel=level+1;
	mouseover="";
	mouseout="";
	mouseclick="";
	if(menu.rolls!="") {
		mouseover+=" MenuImg"+Ind+".src='"+menu.path+currentLayer[indice[level]].imageURL+menu.rolls+".gif';";
		mouseout+=" MenuImg"+Ind+".src='"+menu.path+currentLayer[indice[level]].imageURL+".gif';";
		}
	mouseout+=" timer_hide_all("+menu.name+");";
	mouseover+=" clearTimeout(window.will_hide);";
	mouseover+=" hide_part("+subLevel+");";
	if(hasSubMenu) {
		mouseover+=" displayLayer('MENU"+menu.name+l_indice+"');";
		mouseover+=" layerTab["+subLevel+"]='MENU"+menu.name+l_indice+"';";
		}
	actions=" onMouseover=\""+mouseover+"\" onMouseout=\""+mouseout+"\" onClick=\""+mouseclick+"\"";
	return actions;
	}

function calcLevelPos(w,h,way,type) {
	if(type==1) {
		switch(way) { //suite d'un sous-menu
			case "R":
				pos_y[level]+=h;
				break;
			case "B":
				pos_x[level]=pos_x[level]+w;
				break;
			default:
				pos_y[level]+=h;
				break;
			}
		}
	else {
		switch(way) { //position suivante d'un nouveau sous-menu
			case "R":
				pos_x[level+1]=pos_x[level]+w;
				pos_y[level+1]=pos_y[level];
				break;
			case "B":
				pos_x[level+1]=pos_x[level];
				pos_y[level+1]=pos_y[level]+h;
				break;
			default:
				pos_x[level+1]=pos_x[level]+w;
				pos_y[level+1]=pos_y[level];
				break;
			}
		}
	}

function retriveParentLayer(menu) {
	layer=menu.menuItem;
	for(i=0;i<level;i++) {
		decrement=i<level-1?1:0;
		layer=layer[indice[i]-1].menuItem;
		}
	return layer;
	}

function writeRub(way,menu,hasSubMenu) {
	br=(way=="R" || !way)?"<br>":"";
	Ind=calcIndice();
	actions=calcActions(menu,Ind,hasSubMenu);
	rub="<a href=\""+currentLayer[indice[level]].link+"\""+actions+"><img src=\""+menu.path+currentLayer[indice[level]].imageURL+".gif\" border=\"0\" name=\"MenuImg"+Ind+"\"></a>"+br;
	l_name="MENU"+menu.name+layerIndice[level];
	writeHTML(l_name,rub);
	//alert();
	}

function writeHTML(l,h) {
if (isNS) {with (zl_lref(l).document) {open(); write(h); close();} }
else {zl_lref(l).innerHTML += h;}
}

function createLayer(v_menu,x,y) {
	layerIndice[level]=calcIndice();
	l_x=x+pos_x[level];
	l_y=y+pos_y[level];
	if(isIE || isW3C) {
		sh=level==0?"visible":"hidden";
		document.write("<div id='MENU"+v_menu.name+layerIndice[level]+"' style='position:absolute; left:"+l_x+"px; top:"+l_y+"px; z-index:1; visibility:"+sh+"'>");
		document.write("</div>");
		}
	else if(isNS) {
		sh=level==0?"show":"hide";
		document.write("<layer id='MENU"+v_menu.name+layerIndice[level]+"' left='"+l_x+"' top='"+l_y+"' z-index='1' visibility='"+sh+"'>");
		document.write("</layer>");
		}
	}

function zigZag(menu,x,y) {
	if(currentLayer[indice[level]].menuItem.length>0) { //la rubrique actuelle contient un sous-menu
		calcLevelPos(currentLayer[indice[level]].w,currentLayer[indice[level]].h,currentLayer[indice[level]].way,0);
		calcLevelPos(currentLayer[indice[level]].w,currentLayer[indice[level]].h,currentLayer[indice[level]].way,1);
		writeRub(currentLayer[indice[level]].way,menu,true);
		currentLayer=currentLayer[indice[level]].menuItem; // la sous-rubrique devient la rubrique actuelle
		indice[level]++; //on passe à l'item suivant après traitement
		level++; //on passe au niveau supérieur
		layerLength[level]=currentLayer.length;
		indice[level]=0; //on met l'indice de ce niveau supérieur à zéro pour être au début du niveau
		createLayer(menu,x,y); //document.write("creation cadre<br>"); // on crée un calque pour le menu actuel
		}
	else { // la rubrique actuelle ne contient pas de sous menu
		if(indice[level]<currentLayer.length-1) { //il y a encore des items non parcourus
			calcLevelPos(currentLayer[indice[level]].w,currentLayer[indice[level]].h,currentLayer[indice[level]].way,1);
			writeRub(currentLayer[indice[level]].way,menu);
			indice[level]++; //on passe à l'item suivant après traitement
			}
		else { //on est au dernier indice
			calcLevelPos(currentLayer[indice[level]].w,currentLayer[indice[level]].h,currentLayer[indice[level]].way,1);
			writeRub(currentLayer[indice[level]].way,menu);
			level--; //on revient au niveau inférieur
			currentLayer=retriveParentLayer(menu); //
			if(indice[level]>=layerLength[level]) {
				level--;
				currentLayer=retriveParentLayer(menu);
				}
			}
		}
	document.write(" "); //pour le mac
	if(level>=0) {zigZag(menu,x,y);} //si on est en-dessous du niveau le plus bas, on a terminé, on ne rappelle plus la fonction récurrente
	}

function writeMenu(menu,x,y) {
	x=x-30;
	createLayer(menu,x,y); //creation d'un premier cadre pour le niveau 0
	currentLayer=menu.menuItem; //rubrique courrante: rubrique 0 du niveau 0
	layerLength[0]=menu.menuItem.length;
	zigZag(menu,x,y);
	for(i=0;i<indice.length;i++) {
		layerTab[i]=0;
		}
	}

