

/*------------------------------------------------------------------- 
Author's Statement:
This script is based on ideas of the author.
You may copy, modify and use it for any purpose. The only condition is that if you publish web pages that use this script you point to its author at a suitable place and don't remove this Statement from it.
It's your responsibility to handle possible bugs even if you didn't modify anything. I cannot promise any support.
Dieter Bungers
GMD (www.gmd.de) and infovation (www.infovation.de)
--------------------------------------------------------------------*/
/*-------------------------------------------------------------------
Version modifiée :

	21/04/01 =	Rajout des options setImgSize,imgWidth,imgHeight,tocScroll,objMenu,objContent,showTitle
				Modification de l'accès aux objets pour séparer de l'utilisation de frames
	24/04/01 =	Rajout de l'option showAll
	25/04/01 =	Rajout de l'option showLevel
	26/04/01 =	Suivi des valeurs de page en page, par extraction des paramètres de l'URL
	30/04/01 =	Support Netscape 6
				Support des layers
	21/06/01 =	Extraction des paramètres URL améliorée (base du nom de fichier prise en compte)
	02/07/01 =	Modif comportement currentNumber, si non fourni alors prendre "1" par défaut
	04/07/01 =	Correction bug Netscape4 (reload de la page si le layer est visible pendant la modification de son document)
	11/07/01 =	Workaround bug Netscape4 (plantages lors des changements de visibilité) -> showAll forcé à true, menu dégénéré!
				Si le currentNumber est faux, alors le remplacer par la première option dispo du menu - au lieu de stopper l'affichage
	14/10/01 =	Possibilité de ne pas utiliser d'images pour le menu
	08/04/02 =	Le menu se repère de lui-même dans le tocTab[] pour savoir où s'ouvrir par défaut
	15/07/02 =  Correction pour fonctionner avec Netscape 4.x (création du layer au vol), donc showAll n'est plus forcé
				Correction bug Netscape, qui coupe le script si on accède à document.layers AVANT le onLoad (ie : pendant le script global, dans le courant de la page)
--------------------------------------------------------------------*/

var toDisplay;
var isIE,isNS,isMZ,isMac;
var QueryString;

var nsLayer=null;	// Layer pour Netscape.... saloperie de browser de nazes
var mdi,sml;
var oldCurrentNumber, oldLastVisitNumber;
var toDisplay;
var rootNumber;

mainInit();

// ***************************************
// Cette fonction initialise des trucs
// Bicoze Netscape appele parfois onLoad()
// et donc reDisplay AVANT LA PARTIE GLOBALE
// Donc hop, là on est pas emmerdé, nos
// variables globales sont forcées
// ***************************************
function mainInit()
{
	if (typeof(isIE)=="undefined")
	{
		var ua=navigator.userAgent.toLowerCase();
		isIE = false;
		isNS = false;
		isMZ = false;
		if (document.all) isIE=true;
		if (document.getElementById) isMZ=true;
		//if (document.layers) isNS=true; TEST IMPOSSIBLE !! SINON NETSCAPE EXPLOSE
		isNS=((!isIE && !isMZ) && (ua.indexOf("mozilla/4")>=0));
		isMac=(ua.indexOf("mac")>=0);

		QueryString=new Array();
		if (self.location.href.indexOf("?",0)>0)
		{
			var QueryRaw,key;
			QueryRaw=self.location.href.slice(self.location.href.indexOf("?",0)+1).split("&")
			for (i=0;i<QueryRaw.length;i++)
			{
				key=QueryRaw[i].split("=");
				QueryString[key[0]]=key[1];
			}
		}
		otherInit();
	}
}

// Complément de la précédente, spécialisée
// pour les dynToc
function otherInit()
{
	mdi = (isIE) ? textSizes[1]:textSizes[3];
	sml = (isIE) ? textSizes[2]:textSizes[4];

	toDisplay= new Array();
	for (ir=0; ir<tocTab.length; ir++)
		toDisplay[ir] = (tocTab[ir][0].split("-").length==1);
	
	rootNumber=ParseRootNumber();
	
	oldCurrentNumber = "";
	oldLastVisitNumber = "";
}


// ***************************************
// Cette fonction analyse le contexte
// pour extraire l'index pour l'affichage
// initial du menu
// ***************************************

function ParseRootNumber()
{
	var url,tmp,pos1,pos2

	// Ordre donné par QueryString est prioritaire
	if (QueryString["tocMenu"]>"")
		return QueryString["tocMenu"];
	url=self.location.href;
	pos1=url.lastIndexOf("/");
	if (pos1>0)
	{
		// Puis ensuite test si la page est au format xxxxx.1-2-3.xxx -> extrait le 1-2-3
		pos2=url.indexOf(".",pos1);
		if (pos2>0)
		{
			tmp=url.substr(pos1+1,pos2-pos1-1);
			if (tmp.match(/^[0-9]+((-[0-9]+)*)$/g))
				return tmp;
		}
		//Et enfin regarde simplement le nom de la page, pour le trouver dans le tableau.
		tmp=url.substr(pos1+1);
		if (tmp.match(/^(\S+\.[^?]+)/g))
		{
			tmp=RegExp.$1;
			for(pos1=0;pos1<tocTab.length;pos1++)
				if (tocTab[pos1][2].indexOf(tmp)>=0)
					return tocTab[pos1][0];
		}
	}
	return "";
}

// ***************************************
// The function redisplays the ToC and the content 
// ***************************************

function reDisplay(currentNumber,tocChange,noLink,e)
{
	mainInit();

// Input parameters:
// currentNumber: Hierarchical ordering string (or number) of the heading the user wants to display (we call this heading the "current"). This controls the change of both, the ToC depending on the second parameter tocChange and the content's URL depending on the third parameter noLink. 
// tocChange: Controls how to change the ToC. 0 = No change, 1 = Change with automatic collapsing of expanded headings that are not on the path to the current heading, 2 = Change without automatic colapsing (as use for example by Windows Explorer or Mac OS).
// noLink: Controls wether the content's URL shall be changed to the value given by the 3rd element of an tocTab's entry (= 0) or not (= 1).
// e: The event that triggered the function call. If it is set it must be the event object.

	var goURL=true;

	if (currentNumber=="")	// Si pas de numéro passé à la fonction alors s'attaquer au RootNumber ou bien "1" par défaut
	{
		if (rootNumber>"")
		{
			currentNumber=rootNumber;
			rootNumber="";
			goURL=false;
		}
		else
			currentNumber="1";
	}

	callURL= (isFramed ? "parent." : "")

// If there is an event that triggered the function call: Checking the control key depending on the browser used. If it is pressed and tocChange is greater than 0 tocChange is set to 2 so the ToC schanges without automatic collapsing: 
	if (e) {
		ctrlKeyDown = (isIE) ? e.ctrlKey : (e.modifiers==2);
		if (tocChange && ctrlKeyDown) tocChange = 2;
	}

// Initializing the ToC window's document and displaying the title on it's top. The ToC is performed by a HTML table:
	var contentText=bodyHead + "<table width=" + tabWidth + (backColor>"" ? " bgcolor="+backColor : "") + " border=0 cellspacing=0 cellpadding=1>\n";

	if (showTitle)
		contentText+="\n<tr>\n<td width=" + tabWidth + " colspan=" + (nCols+1) + "><a href=\"javaScript:history.go(0)\" onMouseDown=\"parent.reDisplay('" + tocTab[0][0] + "',0,0)\" style=\"font-family: " + fontTitle + "; font-weight:bold; font-size:" + textSizes[0] + "em; color: " + titleColor + "; text-decoration:none\">" + tocTab[0][1] + "</a></td></tr>\n";

//	currentLevel = the level of the current heading:
	var currentNumArray = currentNumber.split("-");
	var currentLevel = currentNumArray.length-1;

// currentIndex = Current heading's index in the tocTab array:
	var currentIndex = null;
	for (i=0; i<tocTab.length; i++) {
		if (tocTab[i][0] == currentNumber) {
			currentIndex = i;
			break;
		}
	}
// If currentNumber was not found in tocTab: prendre le premier par défaut.
	if (currentIndex == null) currentIndex=1;
	
// currentIsExpanded = Expand/Collaps-state of the current heading:
	if (currentIndex < tocTab.length-1) {
		nextLevel = tocTab[currentIndex+1][0].split("-").length-1;
		currentIsExpanded = nextLevel > currentLevel && toDisplay[currentIndex+1];
	} 
	else currentIsExpanded = false;

// Determining the new URL and target (if given) of the current heading
	theHref = (noLink) ? "" : tocTab[currentIndex][2];
	theTarget = tocTab[currentIndex][3];

// ***************************************
// 1st loop over the tocTab entries: Determining which heading to display:
// ***************************************
	for (i=1; i<tocTab.length; i++) {
		thisNumber = tocTab[i][0];
		thisNumArray = thisNumber.split("-");
		thisLevel = thisNumArray.length-1;

// Nothing to do if the tocChange parameter is set to 0. If it is set to 1 or 2...
		if (tocChange) {
// isOnPath = this heading is on the path to the current heading in the ToC hierarchy or a sibling of such a heading:
			isOnPath = true;
			if (thisLevel > 0) {
				for (j=0; j<thisLevel; j++) {
					isOnPath = (j>currentLevel) ? false : isOnPath && (thisNumArray[j] == currentNumArray[j]);
				}
			} 
// By the following, the headings on the path to the current heading and the siblings of such  headings (isOnPath==true, see above) will be displayed anyway. If the tocChange parameter is set to 1 no other heading will be displayed. If it is set to a number greater than 1 the headings that have been displayed before will additionally be displayed again.  
			toDisplay[i] = (tocChange == 1) ? isOnPath : (isOnPath || toDisplay[i]);

// Now let's perform the expand/collaps mechanism: If the heading is a descendant of the current heading it's next display depends on wether the current heading was expanded or collapsed. If it was expanded the descendants have not to be displayed this time, otherwise only the childs has to be displayed but not the grandchildren, great-grandchildren etc.. Remember that currentIsExpanded says wether the current heading was expanded or not. The if-clause is a criteria for being a descendant of the current heading. If it's a descendant and thisLevel == currentLevel+1 it's a child.
			if (thisNumber.indexOf(currentNumber+"-")==0 && thisLevel > currentLevel) { 		
				if (currentIsExpanded)
					toDisplay[i] = false;
				else
					toDisplay[i] = (thisLevel == currentLevel+1);
			}
		} 
		toDisplay[i]=(toDisplay[i] || (showLevel>thisLevel) || showAll);
	} // End of loop over the tocTab

// ***************************************
// 2nd loop over the tocTab entries: Displaying the headings:
// ***************************************
	var scrollY=0, addScroll=tocScroll; 
	for (i=1; i<tocTab.length; i++) {
		if (toDisplay[i]) {
			thisNumber = tocTab[i][0];
			thisNumArray = thisNumber.split("-");
			thisLevel = thisNumArray.length-1;
			isCurrent = (i == currentIndex);

// Setting the heading's symbol depending on whether this heading is expanded or not or if it is a leaf. It is expanded if the next heading has a greater level than this one AND has to be displayed: 
			if (i < tocTab.length-1) {
				nextLevel = tocTab[i+1][0].split("-").length-1;
				img = (thisLevel >= nextLevel) ? "leaf" : ((toDisplay[i+1]) ? "minus" : "plus");
			} 
			else img = "leaf"; // The last heading is always a leaf.

			img=(imgShow ? img : "void");

// If the scoll parameter is set true than increment the scrollY value:
			if (addScroll) scrollY+=((thisLevel<2)?mdi:sml)*25;
			if (isCurrent) addScroll=false;

// thisTextColor = the text color of this heading
			if (noLink)
				thisTextColor = (thisNumber==oldCurrentNumber) ? currentColor:((thisNumber==oldLastVisitNumber) ? lastVisitColor:normalColor);
			else thisTextColor = (thisNumber==currentNumber) ? currentColor:((thisNumber==oldCurrentNumber) ? lastVisitColor:normalColor);

// Now writing this ToC line, i.e. a table row...:			
			contentText+="<tr>";

// ...first some empty cells for the line indent depending on the level of this heading...:
			for (k=1; k<=thisLevel; k++)
				contentText+="<td width=" + imgWidth + ">&nbsp;</td>";

// ...then the hading symbol and the heading text each with a javaScript link caling just this function reDisplay again: 
			contentText+="<td valign=top><a href=\"javascript:void(0)\" onClick=\"" + callURL + "reDisplay('" + thisNumber + "'," + tocBehaviour[0] + "," + tocLinks[0] + ",event); return false;\"><img src=\"/images/" + img + ".gif\"" + (setImgSize ?  " width=" + imgWidth + " height=" + imgHeight : "") + " border=\"0\"></a></td><td colspan=" + (nCols-thisLevel) + " class=\"" + ((thisLevel<=mLevel)?"tocBig":"tocTiny") + (isNS ? "NS" : (isMac ? "MAC" : "")) + "\"><a href=\"javascript:void(0)\" onClick=\"" + callURL + "reDisplay('" + thisNumber + "'," + tocBehaviour[1] + "," + tocLinks[1] + ",event); return false;\">" + ((showNumbers)?(thisNumber+" "):"") + tocTab[i][1] + "</a></td></tr>\n";
		}
	} // End of loop over the tocTab

// ***************************************
// Closing the ToC document, scrolling its frame window and displaying new content in the content frame or in the top window if required 
// ***************************************

// Updating the global variables oldCurrentNumber and oldLastVisitNumber. See above for its definition
	if (!noLink) { 
		oldLastVisitNumber = oldCurrentNumber;
		oldCurrentNumber = currentNumber;
	}

// Closing the ToC table and the document

	contentText+="<tr>";
	for (k=0; k<nCols; k++)
		contentText+="<td width=" + imgWidth + "></td>";
	contentText+="<td width=" + ((tabWidth-nCols)*(imgWidth+1)) + "><img width=1 height=" + tocSpacer + " src=\"/images/void.gif\"></td></tr>\n";
	contentText+="</table>" + bodyFoot;

	if (isFramed)
	{
		eval("var toc=" + objMenu + ".document;");
//		toc.open();
		toc.write(contentText);
		toc.close();
	}
	else
	{
		if (isMZ)
			document.getElementById(objMenu).innerHTML=contentText;
		else
			if (isIE)			
				document.all[objMenu].innerHTML=contentText;
			else
			{
				if (isNS)
				{
					if (nsLayer==null)
					{
						nsLayer=new Layer(1,window);
						//nsLayer=document.layers[objMenu];
						nsLayer.resizeTo(nsWidth,nsHeight);
						nsLayer.moveToAbsolute(nsLeft,nsTop);
						nsLayer.visibility="show";
					}
					var toc=nsLayer.document;
					//toc.open();
					toc.write(contentText);
					toc.close();
				}
			}
	}

// Scrolling the ToC if required
	if (tocScroll && isFramed) eval(objMenu + ".scroll(0,scrollY);");
	
// Setting the top or content window's location if required
	if (theHref && goURL) 
	{
		if (theTarget=="top") top.location.href = theHref;
		else if (theTarget=="parent") parent.location.href = theHref;
		else if (theTarget=="blank") open(theHref,"");
		else eval(objContent + '.location.href = "' + theHref + '";');
	}
}



