// Ce fichier contient quelques fonctions relatives à l'application DHTML de l'interface utilisateur pour les sites Internet 

var loaded = false; // état de l'appplication (téléchargée ou pas)
var cursec = "";
var menuCourant = "";
var bw = lib_bwcheck();
var attributMenuDefault = new Array();
var COULEUR_AVANT_PLAN = 1;
var COULEUR_FOND = 2;
var SOUS_MENU_VISIBLE = 3;
var PRIORITE_ETAT = 4;
var IMAGE = 5;
var ETAT_SELECTIONNE = 6;
var APPEL_FONCTION = 7;
var COULEUR_FOND_SOUS = 8;
var NORMAL = 1;
var AU_DESSUS = 2;
var SELECTIONNE = 3;
var HIDDEN = 1;
var VISIBLE = 2;
var POSSIBLE = 1;
var IMPOSSIBLE = 2;
attributMenuDefault[COULEUR_AVANT_PLAN] = new Array();
attributMenuDefault[COULEUR_AVANT_PLAN][NORMAL] = "#FFFFFF";
attributMenuDefault[COULEUR_AVANT_PLAN][AU_DESSUS] = "#FFFFFF";
attributMenuDefault[COULEUR_AVANT_PLAN][SELECTIONNE] = "#FFFFFF";
attributMenuDefault[COULEUR_FOND] = new Array();
attributMenuDefault[COULEUR_FOND][NORMAL] = "#1b76d1";
attributMenuDefault[COULEUR_FOND][AU_DESSUS] = "#00009F";
attributMenuDefault[COULEUR_FOND][SELECTIONNE] = "#00009F";
attributMenuDefault[COULEUR_FOND_SOUS] = new Array();
attributMenuDefault[COULEUR_FOND_SOUS][NORMAL] = "#0F71C9";
attributMenuDefault[COULEUR_FOND_SOUS][AU_DESSUS] = "0A256C";
attributMenuDefault[COULEUR_FOND_SOUS][SELECTIONNE] = "0A256C";
attributMenuDefault[SOUS_MENU_VISIBLE] = new Array();
attributMenuDefault[SOUS_MENU_VISIBLE][NORMAL] = HIDDEN;
attributMenuDefault[SOUS_MENU_VISIBLE][AU_DESSUS] = HIDDEN;
attributMenuDefault[SOUS_MENU_VISIBLE][SELECTIONNE] = HIDDEN;
attributMenuDefault[PRIORITE_ETAT] = SELECTIONNE;
attributMenuDefault[ETAT_SELECTIONNE] = POSSIBLE;
attributMenuDefault[APPEL_FONCTION] = new Array();
attributMenuDefault[APPEL_FONCTION][NORMAL] = 0;      // pour l'instamt (question perf) 
attributMenuDefault[APPEL_FONCTION][AU_DESSUS] = 0;   // l'appel d'un eval ne peut se faire 
attributMenuDefault[APPEL_FONCTION][SELECTIONNE] = 0; // qu'un niveau d'un sous-menu


var menuClick = false;
var tempsAvantTimeoutActif = 250;
var tempsTimeOutMenu = 500;

var tabOImagesPreTelechargees = new Array();
var nbImagesPreTelechargees = 0;

function initSect(){
	if (bw) {
		if (bw.bw) {
			loaded=true;	
		}
	}
}

function high(oSec){
	var sec=oSec.id;
	var proriteEtatNSel
	if ((bw.bw)&&(loaded)) {

		prioriteEtatNSel = (getAttribut(PRIORITE_ETAT, 0, sec, oSec, true)!=SELECTIONNE)
		if (prioriteEtatNSel) {
			var imExist = setImages(sec, oSec, AU_DESSUS);
			if (!imExist&&!oSec.selectionne) {
				setCouleurs(sec, oSec, AU_DESSUS);		
	 		}
		} else {
			if (!oSec.selectionne) {
				if (!setImages(sec, oSec, AU_DESSUS)) {
					setCouleurs(sec, oSec, AU_DESSUS);	
				}
			}
		} 	
		var im2 = getObj(sec+'eSousMenu');
		if (im2) {
			if (getAttribut(SOUS_MENU_VISIBLE,AU_DESSUS, sec, oSec, true)==VISIBLE) {
				var im = im2.style;
				im.visibility = 'visible';
				appelFonctionExterne(sec+'eSousMenu', im2 , AU_DESSUS);	
			}
		}
		allLow(oSec, sec.substring(0, (sec.length-3)), true);					
		timeOutMenu();
		sectionActive=true;
		menuCourant=sec;
	}	
}

function low(oSec, force){
	var changeEtat;
	var sec=oSec.id;
	var prioriteEtatNSel = false;
	if (force) {
		force=true;
	} else {
		force=false;
	}
	if ((bw.bw)&&(loaded)) {
		prioriteEtatNSel = (getAttribut(PRIORITE_ETAT, 0, sec, oSec, true)!=SELECTIONNE)
		if ((!oSec.selectionne)||(force&&prioriteEtatNSel)) {
			changeEtat = true;
			if (getAttribut(SOUS_MENU_VISIBLE,NORMAL, sec, oSec, true)==HIDDEN) {
				oEnsembleSousSections = getObj(sec+'eSousMenu');
				if (oEnsembleSousSections) {
					if (oEnsembleSousSections.style.visibility=="visible") {
						changeEtat = false;
					}
				}
			} 
			if (changeEtat) {
				if (!setImages(sec, oSec, NORMAL)) setCouleurs(sec, oSec, NORMAL);						
			}
		} else {
			if ((prioriteEtatNSel)&&(getAttribut(ETAT_SELECTIONNE, 0, sec, oSec, true)==POSSIBLE)) {
			    var oMov = new Object();
				trouveSelectionDansMenu(location.href.toLowerCase(), sec.substring(0, 3), oMov, true, false);		
			}			
		}
		sectionActive=false;
	}		
}

function downSousSection(oEnsembleSections, nomSection, oSection, force) {
	var i, ok, sousSection, oSousSection, oEnsembleSousSections, im;
	im = oEnsembleSections.style;
	if ((!oSection.selectionne)||((getAttribut(PRIORITE_ETAT, 0, nomSection, oSection, true)!=SELECTIONNE)&&force)) {
		if (getAttribut(SOUS_MENU_VISIBLE,NORMAL, nomSection, oSection, true)==HIDDEN) {
			im.visibility = 'hidden';		
		} else {

			im.visibility = 'visible';		
		}
		appelFonctionExterne(nomSection+'eSousMenu', oEnsembleSections , NORMAL);	
	} else  {
		if ((getAttribut(SOUS_MENU_VISIBLE,SELECTIONNE, nomSection, oSection, true)==HIDDEN)) {
			im.visibility = 'hidden';
		} else {
			im.visibility = 'visible';		
		}
		appelFonctionExterne(nomSection+'eSousMenu', oEnsembleSections , SELECTIONNE);
	}
	ok = true;
	i = 1;
	while(ok) {
		sousSection = nomSection + 'e' + preZero(i);
		oSousSection = getObj(sousSection);	
		if (oSousSection) {
			oEnsembleSousSections = getObj(sousSection+'eSousMenu');
			if (oEnsembleSousSections) {
				downSousSection(oEnsembleSousSections, sousSection, oSousSection, force);
			}
			ok = 1;
			low(oSousSection, force);
		} else {
			ok = 0;
		}
		i++;
	}
}

// fonction qui désactive chacune des section du menu sauf si c'est la section courante
function allDown(cursec, force) {
	// get no menu
	if (cursec) {
		var noMenu = cursec.substring(0, 3);
		allLow("", noMenu, force);
	}
}

// fonction qui désactive chacune des section du menu sauf si c'est la section courante
function allLow(oSousSectionCourante, nomSection, force) {
	var i, ok, sousSection, oSousSection, oEnsembleSousSections;
	ok = true;
	i = 1;
	while(ok) {
		sousSection = nomSection + 'e' + preZero(i);
		oSousSection  = getObj(sousSection);
		ok = oSousSection?1:0
		if (ok) {
			if (oSousSection!=oSousSectionCourante) {
				oEnsembleSousSections = getObj(sousSection+'eSousMenu');
				if (oEnsembleSousSections) {
					downSousSection(oEnsembleSousSections, sousSection, oSousSection, force);
				}
				low(oSousSection, force);
			}
		}
		i++;
	}
}

var timeOutActif;
function timeOutMenu() {
	if (!timeOutActif) {
	    timeOutActif = true;
		setTimeout("tempsHorsSection()", tempsAvantTimeoutActif);
	}
}

function tempsHorsSection() {
	if (!sectionActive) {
		setTimeout("horsSection()", tempsTimeOutMenu);
	} else {
		timeOutActif = false;
		timeOutMenu();
	}
}
function horsSection() {
	if (!sectionActive) {
	allMenuDown(false)
		timeOutActif = false;
	} else {
		timeOutActif = false;
		timeOutMenu();
	}
}
function allMenuDown(force) {
	menu = "m" + preZero(1);
	i=1;
	if (!menuClick) {
		while(getObj(menu)) {
			allDown(menu, force);
			i++;
			menu = "m" + preZero(i);
		}
	}
	menuClick=false;
}

function clickAutour(obj, newWin) {
	menuClick= true;
	url = trouvePremierUrl(obj);
    if (url) {
    	if (newWin) {
    		var nW = window.open(url, "new");
    		if (nW) {
    			nW.focus();
    		}
    	} else {
	    	location.href = url;
		}
    }	
}

//fonction qui va chercher une référence à un objet selon le DOM du browser
function getObj(nObj) {
	return bw.dom?document.getElementById(nObj):bw.ie4?document.all[nObj]:0;
}

function activeSectionUrl(noMenu) {
    var oMov = new Object();
    oMov.Y= 0;
    var oMenu = getObj(noMenu);
    if (getAttribut(ETAT_SELECTIONNE, 0, noMenu, oMenu, true)==POSSIBLE) {
	    if (!bw.opera6) {
	    	trouveSelectionDansMenu(location.href.toLowerCase(), noMenu, oMov, true, true);
			moveObjPosition(oMov.X, oMov.Y, noMenu+'PadApres');
	    } else	{
	    	trouveSelectionDansMenu(location.href.toLowerCase(), noMenu, oMov, false, true);
			moveObjPosition(oMov.X, oMov.Y, noMenu+'PadApres');
	    	trouveSelectionDansMenu(location.href.toLowerCase(), noMenu, oMov, true, false);		
		}	
	}
}


function trouveSelectionDansMenu(url, nomSection, oMov, visibility, movObject) {
	var i, ok, sousSection, oSousSection, oEnsembleSousSections, urlSection, oUrlSection;
	var selectionne = false;
	var elementSousSectionSelectionne = false;
	ok = true;
	i = 1;
	if (isElement(nomSection)&&movObject) moveObjPosition(oMov.X, oMov.Y, nomSection);
	while(ok) {
		selectionne = false;
		sousSection = nomSection + 'e' + preZero(i);
		if (movObject) moveObjPosition(oMov.X, oMov.Y, sousSection)			
		urlSection = 'l' + sousSection;
		oUrlSection = getObj(urlSection);
		if (oUrlSection) {
			selectionne=compareUrl(oUrlSection, url);
			elementSousSectionSelectionne = elementSousSectionSelectionne || selectionne;
			oSousSection = getObj(sousSection);
			oEnsembleSousSections = getObj(sousSection+'eSousMenu');
			if (oEnsembleSousSections) {
				selectionne = (trouveSelectionDansMenu(url, sousSection, oMov, visibility, movObject) || selectionne);
				if (selectionne) {
					if (visibility) {
						if (getAttribut(SOUS_MENU_VISIBLE,SELECTIONNE, sousSection, oSousSection,true)==VISIBLE) {				
							oEnsembleSousSections.style.visibility = 'visible';
						} else {
							oEnsembleSousSections.style.visibility = 'hidden';				
						}
					}
					oMov.Y = oMov.Y + getAttribut("DeplaceMenuApresY",SELECTIONNE, sousSection, oSousSection, false);
					oMov.X = oMov.X + getAttribut("DeplaceMenuApresX",SELECTIONNE, sousSection, oSousSection, false);					
					if (!oMov.Y) oMov.Y = 0;
					if (!oMov.X) oMov.X = 0;
					
				}				
			}
			oSousSection.selectionne = selectionne;
			if (selectionne) if (!setImages(sousSection,oSousSection, SELECTIONNE)) setCouleurs(sousSection,oSousSection, SELECTIONNE);		
		} else {
			ok = false;
		}
		i++;
	}
	var oSection = getObj(nomSection);
	if (!oSection.selectionne) {
		oSection.selectionne = elementSousSectionSelectionne;
		if (oSection.selectionne) {
			if (isElement(nomSection)) if (!setImages(nomSection, oSection, SELECTIONNE)) setCouleurs(nomSection, oSection, SELECTIONNE);				
		}	
	}
	return elementSousSectionSelectionne;
}

function compareUrl(oUrlSection, url) {
	var lUrl = oUrlSection.href.toLowerCase();
	if (url.indexOf(lUrl)>-1) {
		return true;
	} else {
		return false;
	}
}

function getAttributPourObj(nomAttribut,evenementUtilisateur, obj) {
	var attribut = 0; // un attribut qui n'existe pas est 0
	try {
		if (evenementUtilisateur!=0) {
			attribut = obj.attributMenu[nomAttribut][evenementUtilisateur] 	
		} else {
			attribut = obj.attributMenu[nomAttribut];
		}
	}
	catch(e) {
		return 0;
	}		
	return attribut;		
} 

function getDefaultAttribut(nomAttribut,evenementUtilisateur) {
	var attribut = 0; // un attribut qui n'existe pas est 0
	try {
		if (evenementUtilisateur!=0) {
			attribut = attributMenuDefault[nomAttribut][evenementUtilisateur];	
		} else {
			return attributMenuDefault[nomAttribut];
		}
	}
	catch(e) {
		return 0;
	}	
	return attribut;				
}

function getAttribut(nomAttribut,evenementUtilisateur, nomObj, obj, rechParent) {
// si l'objet a un attribut specifique, on renvoie cet attribut
// sinon on prend l'objet parent dont le nom de substr(0,3)
// sinon on prend l'attribut par defaut 
	var attribut;
	attribut = getAttributPourObj(nomAttribut,evenementUtilisateur, obj);
	if (!attribut&&rechParent) {
		nomObj = nomObj.substring(0, 3);
		objDeLAttribut = getObj(nomObj);
		attribut = getAttributPourObj(nomAttribut,evenementUtilisateur, objDeLAttribut);
		if (!attribut) {
			attribut = getDefaultAttribut(nomAttribut,evenementUtilisateur);
		}
	}
	return attribut;
}

function setCouleurs(nomSection, oSection, etat) {
		var	urlSection = 'l' + nomSection;
		var oUrlSection	
		//TODO: IL FAUDRAIT REMPLACER CETTE CONDITION PAR UNE CONDITION PLUS GÉNÉRIQUE - DM	
		//Le fond des sous-menus est de couleur différente
		if (nomSection.match("m01e02e02e")!=null || nomSection.match("m01e02e03e")!=null || nomSection.match("m01e02e04e")!=null || nomSection.match("m01e02e01e")!=null)  {
			//alert(nomSection);
			oUrlSection = getObj(urlSection);
			if (!oUrlSection) oUrlSection = oSection;
			oSection.style.backgroundColor = getAttribut(COULEUR_FOND_SOUS,etat, nomSection, oSection, true);						
		} else {
			oUrlSection = getObj(urlSection);
			if (!oUrlSection) oUrlSection = oSection;
			oSection.style.backgroundColor = getAttribut(COULEUR_FOND,etat, nomSection, oSection, true);				
		}
		oUrlSection.style.color = getAttribut(COULEUR_AVANT_PLAN,etat, nomSection, oSection, true);	
}

function setImages(nomSection, objSection, etat) {
	var image = false;
	image = getAttribut(IMAGE,etat, nomSection, objSection, false);
	if (image) { // pour les images, on ne recherche par l<attribut du parent (inutile pour une images)
		im2 = getObj('i'+nomSection);
		if (im2) {
			im2.src=image;			
		}
		return true;	
	}
	return false;
}

function isElement(nomObj) {
	return ((nomObj.length>3)&&(!nomObj.indexOf("Sous")));
}

function moveObjPosition(OffSetX, OffSetY, nomObjet) {
	// les objets doivent avoir position:relative pour que ca marche !!!!
	if ((OffSetX)||(OffSetY)){
			document.write('<style type="text/css">' +
						'#' + 	nomObjet + '{' +
						'top:' + OffSetY + 
						';left:' + OffSetX +
						'}</style>');
	}
}

function trouvePremierUrl(obj) {
	if (bw.dom) {
		if (obj.childNodes) {
			for (i=0;i<obj.childNodes.length;i++) {
				if (obj.childNodes[i].href) return obj.childNodes[i].href;
				valRecursif = trouvePremierUrl(obj.childNodes[i]);
				if (valRecursif) return valRecursif;
			}
		}
	}
	return false;
}

function creerImage(urlRelatif) {
	oImage = new Image();
	hrefImage = urlRelatif;
	oImage.src = hrefImage;
	tabOImagesPreTelechargees[nbImagesPreTelechargees] = oImage; //(on garde la référence au cas où)
	nbImagesPreTelechargees ++;
	return urlRelatif;
}

// attention : ne jamais donner des paramètres provenant d'un querystring aux objets
// sinon vulnérable au XSS
function appelFonctionExterne(nomSection, objSection, etat) {
	var aEvaluer = false;
	aEvaluer = getAttribut(APPEL_FONCTION,etat, nomSection, objSection, false);
	if (aEvaluer) {
		eval(aEvaluer);
	}	
}

