/* Navigation Menu Javascript - June 04, 2003                      */
/* author: Emmanuel Vasseur                                        */
/* Notes : - Internal functions are prefixed with '_' character    */
/*           (underscore). They should not be call in XHTML pages  */
/*           or Javascript files.                                  */
/*         - other functions (without '_') can be call in XHTML    */
/*           pages to compose Menu (in 'MenuDescription.js' file ) */
/*           or in XHTML pages to initialize the Elements (current */
/*           path, section title and Navigation Menu               */  
/*         - deployMenu() function id for debugging purpose only   */
/*           help you to create a menu (display all subsections of)*/
/*           the navigation menu (does not include the 4th external*/
/*           levels.                                               */
 
/* Arrows Images used in the navigation menu */
var DISPLAYED_IMG       = "../graphics/ico_arrow_down_black.gif";
var NOTDISPLAYED_IMG    = "../graphics/ico_arrow_up_black.gif";
var SUB_DISPLAYED_IMG   = "../graphics/ico_arrow_down_grey.gif";
var SUB_NOTDISPLAYED_IMG= "../graphics/ico_arrow_up_grey.gif";

var ROOT_ELEMENT    = "ROOT"; 

/* Hashtables   */
/* Key  -> instance of ComponentsList                                */
/* Key  -> parent Key of this menu element                           */
/* Key  -> title of this menu element                                */
/* Key  -> link  of this menu element                                */
var componentsHash = new Object();
var parentHash     = new Object();
var titleHash      = new Object();
var linkHash       = new Object();

/* Array and Hash                */
/* cycleTab -> Ordered Item List */
var cycleTab       = new Array();
var orderedHash    = new Object();
var displaySQ      = 0;

/* ComponentsList Constructor                                                 */
/* Parameters : strKey -> abstract key to identify the ComponentsList Element */
/*              strStyle -> stylesheet name for this ComponentsList (color)   */
/* used in MenuDescription.js              */
function ComponentsList(strKey, strStyle)
{
  this.internalKey  = strKey;
  this.colorStyle   = strStyle;
  this.itemArray    = new Array();
  this.typeArray    = new Array();
  
  this.addNewItem = addNewItem;
  this.addList    = addList;
}

/* addItem (Key, Section Title, Section Link) */
/* used in MenuDescription.js                 */
/* Create an array of final items (no List)   */
/* in ComponentsList Element                  */
/* Parameters : strKey -> abstract key to identify the Item               */
/*              strTitle -> section title of this item (displayed in Menu)*/
/*              strLink -> section link of this item (activated in Menu)  */
function addNewItem(strKey, strTitle, strLink)
{
  this.itemArray[this.itemArray.length] = strKey;
  this.typeArray[this.typeArray.length] = "ITEM";

  parentHash[strKey] = this.internalKey;
  titleHash[strKey] = strTitle;
  linkHash[strKey]  = strLink;
}

/* addList (ComponentsList, Section Title, Section Link)                */
/* used in MenuDescription.js                                           */
/* Attach the list 'ComponentsList' to this ComponentsList Element      */
/* Parameters : componentList -> the ComponentsList Element to attach   */
/*              strTitle-> section title of the 'componentList' element */
/*             (displayed in Menu)                                      */
/*              strLink-> section link of the 'componentList' element   */
function addList(componentList, strTitle, strLink )
{
  this.itemArray[this.itemArray.length] = componentList.internalKey;
  this.typeArray[this.typeArray.length] = "LIST";
  
  componentsHash[componentList.internalKey] = componentList;
  parentHash[componentList.internalKey] = this.internalKey;
  titleHash[componentList.internalKey]  = strTitle;
  linkHash[componentList.internalKey]   = strLink;
}

/*************************************************************/
/* Initialization functions                                  */
/* Must be call in 'onLoad' Method in 'Body'    Element      */
/* Parameter : strKey -> the ComponentsList key to initialize*/ 

/* initialize 3 Level Menu, Page Title and */
/* currentPath Elements                    */
function initAll(strKey) {
  _initTitle(strKey);
  _initCurrentPath(strKey);
  _init3LM(strKey);
}
/* initialize 3 Level Menu, Page Title,         */
/* currentPath Elements, previous and next link */
function initAllSQ(strKey) {
  displaySQ = 1;
  _computeCycle();
  _initTitle(strKey);
  _initCurrentPath(strKey);
  _init3LM(strKey);
  /*_initSequenceTable(strKey);*/
}
/* initialize 3 Level Menu and  Page Title */
function init3LMT(strKey) {
  _init3LM(strKey);
  _initTitle(strKey);
}
/* initialize 3 Level Menu and  current Path */
function init3LMC(strKey) {
  _init3LM(strKey);
  _initCurrentPath(strKey);
}
/* initialize 3 Level Menu                 */
function _init3LM(strKey) { 
  document.getElementById('menu3').innerHTML = _computeLevelMenu(strKey);
}
/* initialize Page Title                   */
function _initTitle(strKey) {
  document.getElementById('title').innerHTML = _getTitle(strKey);
}
/* initialize CurrentPath                  */
function _initCurrentPath(strKey) {
  var returnHTML = "";
  if (displaySQ == 1)
  {
    returnHTML += "<hr class=\"LFTruler\" />";
    returnHTML += _drawSequenceTable(strKey);
    returnHTML += _getCurrentPath(strKey);
  }
  else
    returnHTML +=  _getCurrentPath(strKey);

  //document.getElementById('currentpath').innerHTML = returnHTML;
}
/*************************************************************/

/* internal Function */
function _getCurrentPath(strKey)
{
  var htmlPath="";
  var title= "";
  var link = "";
  
  var tmpKeys = new Array();
  var hashKey = tmpKeys[0] = strKey;
  while ( (tmpParent=parentHash[hashKey]) && (tmpParent!=undefined)  )
    tmpKeys[tmpKeys.length] = hashKey = tmpParent;
    
  for ( each=(tmpKeys.length-1) ; each >= 0; each--)
  {
    if ( each==(tmpKeys.length-1) )
    {
      title = HOMEPAGE_NAME;
      link  = HOMEPAGE_URL;
    }
    else
    {
      title = titleHash[tmpKeys[each]];
      link  = linkHash[tmpKeys[each]];
    }
    if ( each == 0 )
      htmlPath += "<font class=\"NAVpagePath\">" + title + "</font>";
    else 
    {
      htmlPath += "<a class=\"NAVpagePathLink\" href=\"" + link + "\">";
      htmlPath += title + "</a>&nbsp;>&nbsp;" ;
    }
  }
  return htmlPath;
}

/* internal Function */
function _getTitle(strKey)
{
  var tmp = titleHash[strKey];
  if (tmp == undefined)
    tmp = " ";
  var strTitle = "<div class=\"NAVtitleBlue\">" + tmp + "</div>";
  return strTitle;
}

/* internal function */
/* build all type of menu */
function _computeLevelMenu(strKey)
{
	var htmlTABLE = "<table class=\"NAVmenuTable\">";
  
	/* if key does not exist */
  if (( strKey == ROOT_ELEMENT ) || (parentHash[strKey] == undefined) || ( titleHash[strKey] == undefined ))
  	htmlTABLE += _build4Level("", "", "", "", ROOT_ELEMENT);
  else
  {
  	var tmpKeys = new Array();
  	var hashKey = tmpKeys[0] = strKey;
  	
  	while ( (tmpParent=parentHash[hashKey]) && (tmpParent!=undefined)  )
    	tmpKeys[tmpKeys.length] = hashKey = tmpParent;

		switch ( tmpKeys.length	)
		{
			case 2 :
				htmlTABLE += _build4Level(tmpKeys[0],"","","",strKey);
				break;
			case 3 :
				htmlTABLE += _build4Level(tmpKeys[1], tmpKeys[0], "", "",strKey);
				break;
			case 4 :
				htmlTABLE += _build4Level(tmpKeys[2], tmpKeys[1], tmpKeys[0], "",strKey);
				break;
			case 5 :
				htmlTABLE += _build4Level(tmpKeys[3], tmpKeys[2], tmpKeys[1], tmpKeys[0],strKey);
				break;
			default :
				htmlTABLE += _build4Level("", "", "", "", strKey);
				break;
		}
  }

	htmlTABLE += "</table>";
  return htmlTABLE;
}

/* write one Section Level */
function _drawLevel1( title, link, color, boolActive)
{
  var activeClass = "";
  var activeImg   = "";
  
  if ( boolActive == 1 )  {
    activeClass = "NAVactiveSection";
    activeImg   = DISPLAYED_IMG;
  } else { 
    activeClass = "NAVinactiveSection";
    activeImg   = NOTDISPLAYED_IMG;
  }
  
  level1 = "<tr>";
  level1 += "  <td class=\"" + color + "\"></td>";
  level1 += "  <td class=\"NAVmenuSectionLine\"><img src=\"" + activeImg + "\" /></td>";
  level1 += "  <td class=\"NAVmenuSectionLine\"><a href=\""+ link +"\" class=\"" + activeClass + "\">" + title + "</a></td>";
  level1 += "</tr>";
  return level1;
}

/* write one Section Level 2 */
function _drawLevel2( title, link, color, boolActive)
{
  var activeClass = "";
  var activeImg   = "";
  
  if ( boolActive == 1 )  {
    activeClass = "NAVactiveSubSection";
    activeImg   = SUB_DISPLAYED_IMG;
  } else { 
    activeClass = "NAVinactiveSubSection";
    activeImg   = SUB_NOTDISPLAYED_IMG;
  }
  
  level2 = "  <tr>";
  level2 += "    <td class=\"" + color + "\"></td>";
  level2 += "    <td><img src=\"" + activeImg + "\" /></td>";
  level2 += "    <td><a href=\""+ link +"\" class=\"" + activeClass + "\">" + title + "</a></td>";
  level2 += "  </tr>";
  return level2;
}

/* write one Section Level 3 */
function _drawLevel3( title, link, color, boolActive)
{
  var activeClass = "";
  var activeImg   = "";
  
  if ( boolActive == 1 )  {
    activeClass = "NAVactiveContent";
    activeImg   = NOTDISPLAYED_IMG;
  } else { 
    activeClass = "NAVinactiveContent";
    activeImg   = SUB_NOTDISPLAYED_IMG;
  }
  
  level3 = "  <tr>";
  level3 += "    <td class=\"" + color + "\"></td>";
  level3 += "    <td><img src=\"" + activeImg + "\" /></td>";
  level3 += "    <td><a href=\""+ link +"\" class=\"" + activeClass + "\">" + title + "</a></td>";
  level3 += "  </tr>";
  return level3;
}

/* write one Section (level 3 or level 4)*/
function _drawExternalLevel( separator, title, link, color, booleanActive )
{
  var htmlItem = "";
  
  if ( booleanActive == 1 )
    htmlItem += separator + "<font class=\"NAVpagePath\">" + title + "</font>";
  else
  {
      htmlItem += separator + "<a class=\"NAVpageExtlevelLink\" href=\"" + link + "\">";
      htmlItem += title + "</a>" ;
  }
  return htmlItem;
}

/* _computeCycle -                   */
/* fill the double Strucutre         */
function _computeCycle()
{
  cycleTab = new Array();
  orderedHash= new Object();
  
  /* initialisation */
  linkHash[ROOT_ELEMENT] = HOMEPAGE_URL;
  cycleTab[0] = ROOT_ELEMENT;
  orderedHash[ROOT_ELEMENT] = 0;
  
  for (eachSection = 0 ; eachSection < rootNav.itemArray.length ; eachSection++ )
  {
    orderedHash[rootNav.itemArray[eachSection]] = cycleTab.length;
    cycleTab[cycleTab.length] = rootNav.itemArray[eachSection];
    if ( rootNav.typeArray[eachSection] == "LIST" )
    {
      
      subSection = componentsHash[rootNav.itemArray[eachSection]];
      for (eachSub = 0 ; eachSub < subSection.itemArray.length ; eachSub++ )
      {
        orderedHash[subSection.itemArray[eachSub]] = cycleTab.length;
        cycleTab[cycleTab.length] = subSection.itemArray[eachSub];
        if ( subSection.typeArray[eachSub] == "LIST" )
        {
       
          subsubSection = componentsHash[subSection.itemArray[eachSub]];
          for (eachSubSub = 0 ; eachSubSub < subsubSection.itemArray.length ; eachSubSub++ )
          {
            orderedHash[subsubSection.itemArray[eachSubSub]] = cycleTab.length;
            cycleTab[cycleTab.length] = subsubSection.itemArray[eachSubSub];
            if ( subsubSection.typeArray[eachSubSub] == "LIST" )
            {
              
              fourthlevel = componentsHash[subsubSection.itemArray[eachSubSub]];
              for (section4 = 0; section4 < fourthlevel.itemArray.length ; section4++)
              {
                orderedHash[fourthlevel.itemArray[section4]] = cycleTab.length;
                cycleTab[cycleTab.length] = fourthlevel.itemArray[section4];
              }
            }
          }
        }
      }
    }
  }
}

/* These 2 functions allow cycle  */
/* navigation through menu items  */

/* get the previous element of the current item */
function _getPreviousLink(itemKey)
{
  strLink = "";
  order = parseInt(orderedHash[itemKey]);
  order--;     /* the previous */

  if (( order < cycleTab.length) && ( order >= 0))
    strLink = linkHash[cycleTab[order]];

  return strLink;
}

/* get the next element of the current item */
function _getNextLink(itemKey)
{
  strLink = "";
  order = parseInt(orderedHash[itemKey]);
  order++;     /* the next */

  if (( order < cycleTab.length) && ( order >= 0))
    strLink = linkHash[cycleTab[order]];

  return strLink;
}

function _drawSequenceTable(strKey)
{
  if ((parentHash[strKey] == undefined) || ( titleHash[strKey] == undefined ))
    strKey = ROOT_ELEMENT;

  seqHTML = "<table class=\"NAVsequenceTable\"><tr>";
  seqHTML += "<td class=\"LFTdocLeftCell\">";

  /* previous item */  
  leftArrow = _getPreviousLink(strKey);
  if (leftArrow != "")
    seqHTML += "<a href=\"" + leftArrow + "\"><img src=\"graphics/arrow_left.gif\" alt=\"left\" /></a>";
  else
    seqHTML += "&nbsp;";
  seqHTML += "</td><td class=\"LFTdocRightCell\">";

  /* next item */ 
  rightArrow = _getNextLink(strKey);
  if (rightArrow != "")
    seqHTML += "<a href=\"" + rightArrow + "\"><img src=\"graphics/arrow_right.gif\" alt=\"right\" /></a>";
  else
    seqHTML += "&nbsp;";
  seqHTML += "</td></tr></table>";
  
  return seqHTML;
}

/* deployMenu -                     */
/* for debugging purpose only       */
/* Does not deploy the 4th levels   */
/* because they are external levels */
function deployMenu()
{
  menuHTML = "<table class=\"NAVmenuTable\">";
  /* Level 1 (Section Level) */
  for (eachSection = 0 ; eachSection < rootNav.itemArray.length ; eachSection++ )
  {
    if ( rootNav.typeArray[eachSection] == "LIST" )
    {
      menuHTML += _drawLevel1(titleHash[rootNav.itemArray[eachSection]],
                              linkHash [rootNav.itemArray[eachSection]],rootNav.colorStyle,1);
      
      /* Level 2 (Sub-Section Level) */
      subSection = componentsHash[rootNav.itemArray[eachSection]];
      for (eachSub = 0 ; eachSub < subSection.itemArray.length ; eachSub++ )
      {
        if ( subSection.typeArray[eachSub] == "LIST" )
        {
          menuHTML += _drawLevel2(titleHash[subSection.itemArray[eachSub]],
                                  linkHash [subSection.itemArray[eachSub]],subSection.colorStyle,1);
          
          /* Level 3 (Sub-Sub-Section Level) */
          subsubSection = componentsHash[subSection.itemArray[eachSub]];
          for (eachSubSub = 0 ; eachSubSub < subsubSection.itemArray.length ; eachSubSub++ )
          {
            menuHTML += _drawLevel3(titleHash[subsubSection.itemArray[eachSubSub]],
                                    linkHash [subsubSection.itemArray[eachSubSub]],subsubSection.colorStyle,1);
          }
        }
      }
    }
  }
  
  menuHTML += "</table>";
  document.getElementById('menu3').innerHTML = menuHTML;
}