// 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 attributMenuDefault = new Array();
var PRIORITE_ETAT = 4;
var IMAGE = 5;
var ETAT_SELECTIONNE = 6;
var APPEL_FONCTION = 7;
var SUPPLANTE_ETAT_DEFAUT = 8;
var NORMAL = 1;
var AU_DESSUS = 2;
var SELECTIONNE = 3;
var HIDDEN = 1;
var VISIBLE = 2;
var POSSIBLE = 1;
var IMPOSSIBLE = 2;
attributMenuDefault[PRIORITE_ETAT] = SELECTIONNE;
attributMenuDefault[ETAT_SELECTIONNE] = POSSIBLE;
attributMenuDefault[SUPPLANTE_ETAT_DEFAUT] = IMPOSSIBLE;
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;

function initSect(){
	if (bw) {
		if (bw.bw) {
			loaded=true;	
		}
	}
}

function high(oSec){
	if (!oSec) oSec = this;
	var sec=oSec.id;
	var proriteEtatNSel;
	var oSousSection = getObj(sec+'eSousMenu');
	var continuer = true;
	var click = false;
	if (high.arguments.length > 1) {
		click = high.arguments[1];
	}
	if ((bw.bw)&&(loaded)) {
		if (click&&oSousSection) {	// pour le cas ou deux évènements identiques (par exemple click) ferment et ouvrent un sous menu exemple menu de loteries dans site loteries
			if (oSousSection.className=="sousMenu " + sec + "eSousMenuHover") {
				allMenuDown(true);
				continuer=false;
			}
		}
		if (continuer) { // pour les autres cas	
			prioriteEtatNSel = (getAttribut(PRIORITE_ETAT, 0, sec, oSec, true)!=SELECTIONNE)
			// si l'etat prioritaire est le au dessus on change l'etat de l'element a audessus (i.e. on change les images et le class pour le css)
			// si l'etat prioritaire est la selection, alors on change l'etat que si l'element courant n'est pas selectionne
			if ((prioriteEtatNSel)||(!prioriteEtatNSel&&!oSec.selectionne)) {
				if (!setImages(sec, oSec, AU_DESSUS)) setClassName(oSec, "Hover", false);	
				if (oSousSection) {
					oSousSection.className="sousMenu " + sec + "eSousMenuHover";
					oSousSection.style.visibility="visible";
					appelFonctionExterne(sec+'eSousMenu', oSousSection , AU_DESSUS);	
				}
			} 	
			allLow(oSec, sec.substring(0, (sec.length-3)), true);					
			timeOutMenu();
			sectionActive=true;
		}
	}	
	oSousSection = null;
}

function low(oSec, force){
	if (!oSec) oSec = this;
	var changeEtat, oEnsembleSousSections;
	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;
			oEnsembleSousSections = getObj(sec+'eSousMenu');
			if (oEnsembleSousSections) {
				if (oEnsembleSousSections.className=="sousMenu " + sec+ "eSousMenuHover") {
					changeEtat = false;
				}		
			} 
			if (changeEtat) {
				if (!setImages(sec, oSec, NORMAL)) setClassName(oSec, "Normal", false); 
			}
		} else {
			if ((prioriteEtatNSel)&&(getAttribut(ETAT_SELECTIONNE, 0, sec, oSec, true)==POSSIBLE)) {
				trouveSelectionDansMenu(location.href.toLowerCase(), sec.substring(0, 3), false, true);		
			}			
		}
		sectionActive=false;
	}		
	oEnsembleSousSections = null;
}

function downSousSection(oEnsembleSections, nomSection, oSection, force) {
	var i, ok, sousSection, oSousSection, oEnsembleSousSections;
	if ((!oSection.selectionne)||((getAttribut(PRIORITE_ETAT, 0, nomSection, oSection, true)!=SELECTIONNE)&&force)) {
		oEnsembleSections.className="sousMenu " + nomSection + "eSousMenuNormal";
		appelFonctionExterne(nomSection+'eSousMenu', oEnsembleSections , NORMAL);	
	} else  {
		oEnsembleSections.className="sousMenu " + nomSection + "eSousMenuSel";
		appelFonctionExterne(nomSection+'eSousMenu', oEnsembleSections , SELECTIONNE);
	}
	ok = true;
	i = 1;
	
	
	var oSectionAEnlever = getObj(nomSection+'eSousMenu');
	oSectionAEnlever.style.visibility="hidden";
	
	
	while(ok) {
		sousSection = nomSection + 'e' + preZero(i);
		oSousSection = getObj(sousSection);	
		if (oSousSection) {
			oEnsembleSousSections = getObj(sousSection+'eSousMenu');
			if (oEnsembleSousSections) {
				downSousSection(oEnsembleSousSections, sousSection, oSousSection, force);
				oEnsembleSousSections.style.visibility="hidden"
			}
			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 getObj(nObj) {
	return document.getElementById(nObj);
}

function activeSectionUrl(noMenu) {
    var oMenu = getObj(noMenu);
    var effSelection = (getAttribut(ETAT_SELECTIONNE, 0, noMenu, oMenu, true)==POSSIBLE);
    var overridable = (getAttribut(SUPPLANTE_ETAT_DEFAUT, 0, noMenu, oMenu, true)==POSSIBLE);    
    trouveSelectionDansMenu(location.href.toLowerCase(), noMenu, effSelection, overridable);
}


function trouveSelectionDansMenu(url, nomSection, effSelection, overridable) {
	var i, ok, sousSection, oSousSection, oEnsembleSousSections, urlSection;
	var selectionne = false;
	var elementSousSectionSelectionne = false;
	ok = true;
	i = 1;
	while(ok) {
		selectionne = false;
		sousSection = nomSection + 'e' + preZero(i);
		oSousSection = getObj(sousSection);
		if (oSousSection) {
			selectionne=effSelection && compareUrl(oSousSection, url);
			oEnsembleSousSections = getObj(sousSection+'eSousMenu');
			if (oEnsembleSousSections) {
				selectionne = ((trouveSelectionDansMenu(url, sousSection, effSelection) && effSelection) || selectionne);
				if (selectionne) {
					oEnsembleSousSections.className="sousMenu " + sousSection+'eSousMenu'+'Sel';	
				}				
			}
			elementSousSectionSelectionne = elementSousSectionSelectionne || selectionne;
			oSousSection.selectionne = selectionne && effSelection;
			if (selectionne) if (!setImages(sousSection,oSousSection, SELECTIONNE)) setClassName(oSousSection, "Sel", false);	
			// on ajoute les événements par défaut sur les objets	
			if (!overridable || !getAttribut(SUPPLANTE_ETAT_DEFAUT, 0, sousSection, oSousSection, false)==POSSIBLE) {
			if (bw.ie&&!bw.opera) {
					oSousSection.onmouseover=high;
					oSousSection.onmouseout=low;		
					oSousSection.onfocus=high;		
					oSousSection.onblur=low;	
				} else {
					oSousSection.setAttribute("onmouseover", "high(this)");	
					oSousSection.setAttribute("onmouseout", "low(this)");	
					oSousSection.setAttribute("onfocus", "high(this)");	
					oSousSection.setAttribute("onblur", "low(this)");
				}	
			}
		} else {
			ok = false;
		}
		i++;
	}
	oSousSection = null;
	oEnsembleSousSections = null;
	return elementSousSectionSelectionne;
}

function compareUrl(oUrlSection, url) {
	var lUrl = oUrlSection.href.toLowerCase();
	return (url==lUrl);
}

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 setImages(nomSection, objSection, etat) {
	var image = false;
	var im2;
	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;			
		}
		im2 = null;
		return true;	
	}
	return false;
}

// 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);
	}	
}

function setClassName(obj, evenement, classNameStart) {
	var className = "";
	if (classNameStart) {
		className=classNameStart;
	}
	var objId = obj.id;
	var i=3;
	className=objId.substring(0, 3) + "e" + evenement; 
	for(i=6;i<=(objId.length);i+=3) {
		className=className + " " + objId.substring(0, i) + "e" +  evenement; 		
	}
	obj.className = className;
}

function setAttribut(nomObjet, noAttribut, valeurNormale, valeurAuDessus, valeurSelectionne) {
	var obj; 
	obj = getObj(nomObjet);
	obj.attributMenu = new Array();
	obj.attributMenu[noAttribut] = new Array();
	obj.attributMenu[noAttribut][NORMAL] = valeurNormale;
	obj.attributMenu[noAttribut][AU_DESSUS] = valeurAuDessus;
	obj.attributMenu[noAttribut][SELECTIONNE] = valeurSelectionne;
}

