function change_page( ind )
{
  window.location = aMenu[ind][2];
}

function is_root( ind )
{
	return (aMenu[ind][1] == "-1");
}

function find_root( lien )
{
  found = find_menu( lien );
  if( found == -1 ) return -1;
  if( aMenu[found][1] == "-1" ) return found;
  else return find_root( aMenu[found][1] );
}

function find_menu( lien )
{
	var found = -1;
	for( i=0; i<aMenu.length; i++ )
	{
		if( aMenu[i][2] == lien )
		{
			found = i;
		}
	}
  return found;	
}

function getUri()
{
	var loc = window.location;
	var url = new String( loc );
	var host = "http://" + loc.hostname;
	var lnk = url.substring( host.length );
	
	//alert( "page= " + lnk );
	return lnk;
}

function writeHrefPrefixed( prefix, href, text, clss, br )
{
	document.write("&nbsp;&nbsp;&nbsp;" + prefix + "<a href='" + href + "' class='" + clss + "'>" + text + "</a>" + (br ? "<br/>" : "") );
}

function writeHref( href, text, clss, br )
{
  writeHrefPrefixed( "", href, text, clss, br )
}

function write( st )
{
  document.write( st );
}

function writeBr( st )
{
  document.write( st + "<br/>" );
}

function replicate( lng, car )
{
  ret = "";
  while( lng-- > 0 ) ret = ret + car;
  return ret;
}

function get_child_nodes( ind )
{
  var ret = Array();
  var i;
  var j = 0;
  for( i=0; i<aMenu.length; i++ )
  {
    if( is_root( i ) ) continue;
    
    if( aMenu[i][1] == aMenu[ind][2] )
      ret[j++] = aMenu[i];
  }
  
  return ret;
}

/*------------------------------ Menus previous newt sommaire -------------------------------*/
function disp_sommaire( clss )
{
  found = find_menu( getUri() );
  
	if( found >= 0 )
	{
		for( i=found+1; i<aMenu.length && aMenu[i][1] != "-1"; i++ )
		{
			writeHref( aMenu[i][2], aMenu[i][0], clss, true );
		}
	}
}

function disp_previous( clss, before )
{
  found = find_menu( getUri() );
  
	if( found >= 0 && aMenu[found][1] != "-1" && aMenu[found-1][1] != "-1" )
	{
	  writeHref( aMenu[found-1][2], before + aMenu[found-1][0], clss, false );
	}
}

function disp_next( clss, next )
{
  found = find_menu( getUri() );
  
	if( found >= 0 && found < aMenu.length-1 && aMenu[found+1][1] != "-1" )
	{
	  writeHref( aMenu[found+1][2], aMenu[found+1][0] + next, clss, false );
	}
}

/*-------------------------- Menu combo ----------------------------*/
function disp_menu_combo()
{
  lien = getUri();
  root = find_root( lien );
  
  if( root == -1 )
  {
    alert( "la page '" + lien + "' n'est pas référencée dans aMenu !!!" );
    return;
  }
  
	write( "<form name=\"menu_combo\"><td><select class=\"champtexte\" name=\"menu_list\" onChange=\"change_page( menu_combo.menu_list[menu_combo.menu_list.selectedIndex].name )\">" );
	for( i=0; i<aMenu.length; i++ )
	{
		if( aMenu[i][1] == "-1" ) // is root
		{
			write( "<option name=\"" + i + "\">" + aMenu[i][0] + "</option>" );
			//if( i == root )
			//	write( "<option selected>" + aMenu[i][0] + "</option>" );
			//else
			//	write( "<option name=\"" + i + "\">" + aMenu[i][0] + "</option>" );
		}
	}
	write( "</select></td></form>" );
}

/*------------------------- Menu from level 2 --------------------------*/
function disp_menu_from_level2()
{
  lien = getUri();
  found = find_menu( lien );

  if( found == -1 )
  {
    alert( "la page '" + lien + "' n'est pas référencée dans aMenu !!!" );
    return;
  }
  
  owners = Array();
  i=0;
  if( found >= 0 ) sthref=aMenu[found][2];
  else sthref = "pas de screen";
  while( true )
  {
  	indowner = findInMenu( sthref, aMenu );
    if( indowner == -1 ) break;

    owners[i++] = sthref;
    sthref = aMenu[indowner][1];
  }
  
  // Liste des parents de la feuille vers le tronc...
  //owners[i] = "-1";
  
  for( i=0; i<aMenu.length; i++ )
  {
    indowner = find( aMenu[i][1], owners );
    if( indowner >= 0 )
    {
      isAParent = find( aMenu[i][2], owners );
      //alert( "i : " + i + " aMenu : " + aMenu[i][2] + " isAParent " + isAParent );
      
	  retrait = "";
	  level = owners.length-indowner;
	  switch( level )
	  {
	  	case 1:
	  		menuLevel = "menuN2";
			prefix = "<img src=\"/common/images/picto/carre_rouge.gif\"/>";
	  		break;
	  	case 2:
	  		menuLevel = "menuN3";
	  		prefix = "<img src=\"/common/images/calage.gif\" width=\"20\" height=\"1\"/>";
	  		break;
	  	case 3:
	  		menuLevel = "menuN4";
	  		prefix = "<img src=\"/common/images/calage.gif\" width=\"40\" height=\"1\"/>";
	  		break;
	  }

	  document.write( "<tr><td>" + prefix );

      if( i == found || isAParent >= 0 ) // Menu actif
      	menuLevel += "On";

      sthref = "<a href=\"" + aMenu[i][2] + "\" class=\"" + menuLevel + "\">&nbsp;";
      document.write( sthref + aMenu[i][0] + "</a><br/><img src=\"/common/images/calage.gif\" height=\"8\"></td>" );

	  document.write( "</tr>" );
    }
  }

}

function find( value, tab )
{
  for( j=0; j<tab.length; j++ ) if( tab[j] == value ) return j;
  return -1;
}

// Retourne l'indice de l'item dans l'arbre qui pointe sur la page value.
function findInMenu( value, tab )
{
  for( j=0; j<tab.length; j++ ) if( tab[j][2] == value ) return j;
  return -1;
}

/*----------------------------- disp site map from menu --------------------------------*/
function disp_child_nodes( ind, depth )
{
  var child_nodes = get_child_nodes( ind );
  var i;
  for( i=0; i<child_nodes.length; i++ )
  {
    write( "<tr>" );
    write( "<td>" );
    //writeHrefPrefixed( replicate( depth*3, "&nbsp;" ), child_nodes[i][2], child_nodes[i][0], "lien" + (depth+1), false );
    writeHrefPrefixed( replicate( depth*3, "&nbsp;" ), child_nodes[i][2], child_nodes[i][0], "lien", false );
    write( "</td>" );
    write( "</tr>" );
    disp_child_nodes( find_menu( child_nodes[i][2] ), depth+1 );
  }
}

function disp_sitemap_from_menu()
{
  write( "<table cellspacing='0' cellpadding='0' width='100%' border='0'>" );
  var i;
  for( i=0; i<aMenu.length; i++ )
  {
    if( is_root( i ) )
    {
      write( "<tr>" );
      write( "<td>" );
      writeHref( aMenu[i][2], aMenu[i][0], "lien", false );
      write( "</td>" );
      write( "</tr>" );
      disp_child_nodes( i, 1 );
    }
  }
  write( "</table>" );
}




