var description;
var base;
var open;
var compteur=1
var affichage=new Array(2);
var index_description=0
affichage[0]=0;

// fait glisser le titre
function glisse()
{
  //ce truc ne marche qu'avec netscape
  //alert("!");
  if (f2.document.top1.left>0)
    {
      f2.document.top1.left=f2.document.top1.left-10;
      setTimeout('glisse()',1);
    }
}

// fait glisser l'update
function glisse2()
{
  //ce truc ne marche qu'avec netscape
  if (f2.document.update1.top<160)
    {
      f2.document.update1.top=f2.document.update1.top+6;
      setTimeout('glisse2()',1);
    }
}

// un noeud d'arbre
function noeud( nom, frere, fils, deploye, description )
{
  this.nom = nom
  this.open = false
  this.frereSuivant = frere
  this.premierFils = fils
  this.filsDeploye = deploye
  this.description = description
}


// rajouter un fils a un noeud
// seulement s'il ne l'est pas deja
// on retourne une ref sur le fils cree ou existant
function rajouter_fils(pere,nom_du_fils)
{
  var fils_tmp,fils;

  fils=new noeud(nom_du_fils,null,null,false,compteur);
  if (pere.premierFils==null) //cas ou le pere n'a pas de fils
    {
      pere.premierFils=fils;
      compteur++
      return fils;
    }
  else
    {
	fils_tmp=pere.premierFils;
	while ((fils_tmp.frereSuivant)!=null)
	  {
	    if (fils_tmp.nom==nom_du_fils) 
			return fils_tmp;
	    fils_tmp=fils_tmp.frereSuivant;
	  }
	if (fils_tmp.nom==nom_du_fils)
		return fils_tmp;
	//si on en est la c'est que le fils n'a pas ete trouve
	fils_tmp.frereSuivant=fils;
	compteur++;
	return fils;
    }
}




// creer les noeuds a partir de la liste
function creation(liste)
{
  listeGroupes = new Array(10);
  var i,j, ind=0 ;
  var tmp;
  
  for(i=0; ind<liste.length; i++) 
    {
      listeGroupes[i] = liste.substring(ind, liste.indexOf(';',ind));
      ind +=  listeGroupes[i].length + 1;
    }
  var racine=new noeud("racine",null,null,true,0);
  for(j=0;j<i;j++)//pour chaque groupe  
    {
	//on decompose d'abord
	groupe=new Array(10);
	var k,l;
	ind=0;
	for(k=0; ind<listeGroupes[j].length; k++) 
	  {		//il faut un '.' a la fin de la chaine
	    groupe[k] = listeGroupes[j].substring(ind, listeGroupes[j].indexOf('.',ind));
	    ind +=  groupe[k].length + 1;
	  }
	tmp=racine;
	for (l=0;l<k;l++)
	  {
	    tmp=rajouter_fils(tmp,groupe[l]);
	  }
    }
  return racine;
}



function prepare_affichage(pere,profondeur)
{
  var i;
  if (pere.nom=="racine")
    {
	prepare_affichage(pere.premierFils,0,0);
	return;
    }
  affichage[0]++;
  affichage[affichage[0]]=new Array(profondeur+3);
  affichage[affichage[0]][0]=profondeur+2;
  for (i=0;i<profondeur;i++)
    {
	if ((affichage[affichage[0]-1][i+1]==0)||(affichage[affichage[0]-1][i+1]==3))
	  affichage[affichage[0]][i+1]=0;
	else
	  affichage[affichage[0]][i+1]=2;
    }
  if (pere.frereSuivant!=null) affichage[affichage[0]][profondeur+1]=1;
  else affichage[affichage[0]][profondeur+1]=3;
  affichage[affichage[0]][profondeur+2]=pere;
  if ((pere.premierFils!=null)&&(pere.filsDeploye==true)) prepare_affichage(pere.premierFils,profondeur+1);
  if (pere.frereSuivant!=null) prepare_affichage(pere.frereSuivant,profondeur);
  return;
}


function header(pere,ancre)
{
  if (pere.open) return;
  if (open!=null)
    open.open=false;
  pere.open=true;
  open=pere;
  affichage=new Array(2);
  affichage[0]=0;
  prepare_affichage(base,0);
  index_description=ancre;
  affiche(affichage);
  return;
}


function affiche(tableau)
{
  var i,j,tmp;
  self.f2.document.close();
  self.f2.document.open();
  self.f2.document.write("<html><body BACKGROUND='fonds/green_stucco.gif'>")
  self.f2.document.write("<table HEIGHT='100%' WIDTH='100%' cellspacing=0 cellpadding=0 border=0>")
  self.f2.document.write("<tr><td rowspan=2 valign='top' WIDTH='50%'><table cellspacing=0 cellpadding=0 border=0>")
  self.f2.document.write("<tr><td><IMG SRC=\"tools/pas.gif\" ALIGN=ABSCENTER></tr>");
  for (i=1;i<=tableau[0];i++)
    {
	self.f2.document.write("<tr><td ALIGN=LEFT>");
	tmp="";
	for (j=1;j<=tableau[i][0];j++)
	  {
	    if (tableau[i][j]==0)
		{tmp+="<IMG SRC=\"tools/branche0.gif\" ALIGN=TEXTTOP>" }
	    else 
		if (tableau[i][j]==1)
		  {tmp+="<IMG SRC=\"tools/branche1.gif\" ALIGN=TEXTTOP>" }
		else
		  if (tableau[i][j]==2)
		    {tmp+="<IMG SRC=\"tools/branche2.gif\" ALIGN=TEXTTOP>" }
		  else
		    if (tableau[i][j]==3)
			{tmp+="<IMG SRC=\"tools/branche3.gif\" ALIGN=TEXTTOP>" }
		    else
			{
			  var ouvert="";
			  if (tableau[i][j].open==true)
			    ouvert="_op";
			  if (tableau[i][j].premierFils==null)
			    tmp+="<IMG BORDER=0 SRC=\"tools/pas"+ouvert+".gif\" ALIGN=ABSCENTER>" ;
			  else
			    {
				tmp+="<A HREF=\"javascript:parent.deploi(parent.affichage["+i+"]["+j+"])\">";
				if (tableau[i][j].filsDeploye==true)
				  tmp+="<IMG BORDER=0 SRC=\"tools/moins"+ouvert+".gif\" ALIGN=ABSCENTER></A>";
				else
				  tmp+="<IMG BORDER=0 SRC=\"tools/plus"+ouvert+".gif\" ALIGN=ABSCENTER></A>";
			    }
			  tmp+="<A HREF=\"javascript:parent.header(parent.affichage["+i+"]["+j+"],"+tableau[i][j].description+")\">" +
				"<FONT SIZE=-2>&nbsp;"+tableau[i][j].nom+"</FONT></A>";
			}
	  }
	self.f2.document.write(tmp+"</td></tr>");    
    }

  self.f2.document.write("</table><td align='left' valign='top'>"+description[index_description]+"<tr><td valign='bottom'><A HREF='"+home_page+"' target='_top'><IMG SRC='tools/back.gif' HEIGHT=40 WIDTH=40 ALT='Back Home'  ALIGN=BOTTOM>Home Page</A>.</table></body></html>")
  return;
}



function un_fils_est_t_il_ouvert(pere)
{
  if (pere.filsDeploye==true)
    {
	var noeud_tmp=pere.premierFils;
	while (noeud_tmp!=null)
	  {
	    if (noeud_tmp.open==true) return (true);
	    if (un_fils_est_t_il_ouvert(noeud_tmp)==true) return (true);
	    noeud_tmp=noeud_tmp.frereSuivant;
	  }
    }
  return(false);
}


function deploi(pere) 
{
   /* verifier qu'un fils n'est pas ouvert
   * auquel cas il faudra l'ouvrir lui
   * si on vient de replier la branche */
  if (pere.filsDeploye==true)
    {
	if (un_fils_est_t_il_ouvert(pere)==true)
	  {
	    pere.filsDeploye=!(pere.filsDeploye);
	    header(pere);
	    return;
	  }
    }
  pere.filsDeploye=!(pere.filsDeploye);
  affichage=new Array(2);
  affichage[0]=0;
  prepare_affichage(base,0);
  affiche(affichage);
  return;
}


function initGroupes( liste)
{
  base =creation(liste);
  prepare_affichage(base,0);
  affiche(affichage);
  return;
}

