//================================================================================

var pageURL, pageSequence;

function initMenu(initArrayName,menuId,infoId) {

   pageURL = dynamicPage.pageURL.substring(document.URL.indexOf('/',document.URL.indexOf('.'))+1,dynamicPage.pageURL.length);
   pageSequence = '';
   findPageSequence(initArrayName);

   currentDisplayedPageSequence = pageSequence;
   cursorSequence = '';
   menuArray = eval(initArrayName);

   if ( typeof menuObj == 'undefined' ) { menuObj = document.getElementById(menuId); }
   if ( typeof infoId != 'undefined' ) {
      infoObj = document.getElementById(infoId);
      if ( messageTimer != null ) { infoObj.innerHTML = dfltMessage; }
      else if ( infoObj.innerHTML != '&nbsp;' && dfltMessage == '' ) { dfltMessage = infoObj.innerHTML; }
   }
   menuObj.innerHTML = buildNewLinkArray( initArrayName,Delimit[currentDelimiter] );
   if ( currentDelimiter == newLine ) {
      var menuSectionWidth = parseInt(menuObj.offsetWidth) - menuPadding;
      for ( var linkObj, i = 0; i < menuArray.length-1; i++ ) {
         linkObj = document.getElementById(String.fromCharCode(i+alfaToNumeric));
         if ( menuSectionWidth > linkObj.offsetWidth ) {
            document.getElementById('0'+linkObj.id).width = menuSectionWidth - linkObj.offsetWidth;
            document.getElementById('0'+linkObj.id).height = linkObj.offsetHeight;
   }  }  }
   deleteNavBoxes(0);	// Delete all navigation boxes, Set Attributes, Set highestSectionNbr = 0;
}

//===============================================================================

function buildNavigationBox(obj) {

   if ( typeof debug != 'undefined' && debug & 0x01 ) { trace = ''; }
   cursorSequence = obj.id;
   deleteNavBoxes(obj.id.length-1);
   var sourceArray = eval( obj.name );
   if ( sourceArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][message] != '' ) { showMessage( sourceArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][message] ); }
   else { showMessage( dfltMessage ); }
   if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\n\nObjectName: ('+sourceArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') ObjectId: ('+obj.id+')'; dbug(); }
   if ( sourceArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][ArrayName] != '' ) {
      if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nSection?: '+document.getElementById(highestSectionNbr+1); dbug(); }
      if ( document.getElementById(++highestSectionNbr) == null ) {
         if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nCreate new <div>: <div id="'+highestSectionNbr+'"></div>'; dbug(); }
         var divTag = document.createElement("div");
         divTag.className = 'dropdown';
         divTag.id = String(highestSectionNbr);
         document.body.appendChild(divTag); }
      var childSection = document.getElementById(String(highestSectionNbr));
      if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nCreate new section nbr:'+highestSectionNbr; dbug(); }
      childSection.innerHTML = buildNewLinkArray( sourceArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][ArrayName],Delimit[newLine] );

      if ( obj.id.length-1 == 0 ) {
         if ( currentDelimiter == newLine ) {
            var objWidth = menuObj.offsetWidth;
            childSection.style.left =  String(findPosX(obj)+objWidth+gapBetweenMenu)+'px';
            if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(gapBetweenMenu)+' Sum: '+childSection.style.left; dbug(); }
//            childSection.style.top = String(findPosY(obj)+boxSideBySideYoffset)+'px';
            childSection.style.top = String(findPosY(document.getElementById('A'))+boxSideBySideYoffset)+'px';
            if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(boxSideBySideYoffset)+' Sum: '+childSection.style.top; dbug(); } }
         else {
            childSection.style.left = String(findPosX(obj)+boxXoffset)+'px';
            if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(boxXoffset)+' Sum: '+childSection.style.left; dbug(); }
            childSection.style.top = String(findPosY(obj)+boxYoffset)+'px';
//            childSection.style.top = String(menuObj.offsetTop+boxYoffset)+'px'; }	// This because IE-6 gives an invalid first position y value.
            if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(boxYoffset)+' Sum: '+childSection.style.top; dbug(); } }
      }
      else {
         var objWidth = document.getElementById(String(obj.id.length-1)).offsetWidth+boxXoffset;
         childSection.style.left =  String(findPosX(obj)+objWidth+gapBetweenBoxes)+'px';
         if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(gapBetweenBoxes)+' Sum: '+childSection.style.left; dbug(); }
         childSection.style.top = String(findPosY(obj)+boxSideBySideYoffset)+'px';
         if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(boxSideBySideYoffset)+' Sum: '+childSection.style.top; dbug(); } }

      var childArrayLength = eval(sourceArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][ArrayName]).length;
      var childSectionWidth = parseInt(childSection.offsetWidth) - dropDownPadding;
      if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nChildSectionWidth: '+String(childSectionWidth); dbug(); }
      for ( var linkObj, i = 0; i < childArrayLength; i++ ) {
         linkObj = document.getElementById(String(obj.id+String.fromCharCode(i+alfaToNumeric)));
         if ( childSectionWidth > linkObj.offsetWidth ) { document.getElementById('0'+linkObj.id).width = childSectionWidth - linkObj.offsetWidth; }
      }
      childSection.style.visibility = "visible";
   }
   buildAttributes(obj);		// Set Attributes for Parent -, Current - and Child - Sections.
}

//================================================================================

function buildNewLinkArray( thisArrayName,delimiter ) {

   var newArray = eval( thisArrayName );
   var navStr = '';

   if ( typeof debug != 'undefined' && debug & 0x02 ) { trace = '' }
   var fontHeight = "1";
   if ( cursorSequence.length > 0 ) {
      if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nMenu-Font Height: '+document.getElementById(cursorSequence).offsetHeight; }
      fontHeight = document.getElementById(cursorSequence).offsetHeight; }
   for ( var i = 0; i < newArray.length; i++ ) {
	if ( i != 0 ) { navStr += delimiter }
	navStr += '<a ';
	if ( typeof debug != 'undefined' && debug & 0x04 ) { trace += '\nURL: ' }
	if ( newArray[i][filename] != '' && cursorSequence+String.fromCharCode(i+alfaToNumeric) != currentDisplayedPageSequence ) {
	   if ( typeof debug != 'undefined' && debug & 0x04 ) { trace += newArray[i][filename] }
	   navStr += 'href="'+newArray[i][filename]+'" '; }
	if ( typeof debug != 'undefined' && debug & 0x04 ) { trace += '\nObjectId: '+cursorSequence+String.fromCharCode(i+alfaToNumeric); }
	navStr += 'id="'+cursorSequence+String.fromCharCode(i+alfaToNumeric)+'" ';
	if ( typeof debug != 'undefined' && debug & 0x04 ) { trace += '\nObjectName: '+thisArrayName; }
	navStr += 'name="'+thisArrayName+'" ';
	navStr += 'onmouseover="buildNavigationBox(this); window.status=\'\'; return true;" ';
	navStr += 'onmouseout="clrNavBoxes(this);" ';
	if ( typeof debug != 'undefined' && debug & 0x04 ) { trace += '\nLinkName: ('+newArray[i][LinkName]+')\n'; dbug(); }
	navStr += 'target="'+(newArray[i][filename].indexOf("http")!=-1?"_blank":"dynamicPage")+'">'+newArray[i][LinkName];
	navStr += '<img id="0'+cursorSequence+String.fromCharCode(i+alfaToNumeric)+'" style="vertical-align: text-bottom;" src="'+dotPath+'/dot.gif" border="0" width="0" height="'+fontHeight+'" />';
	navStr += '</a>';
   }
   if ( typeof debug != 'undefined' && debug & 0x08 ) { trace += '\n'+navStr+'\n'; dbug(); }
   return navStr;
}

//================================================================================

function clrNavBoxes(obj) {

    cursorSequence = '';
    buildAttributes(obj);
    navBoxTimer = setTimeout( "deleteNavBoxes(0)", 500 );
    if ( typeof infoObj != 'undefined' && infoObj.innerHTML != dfltMessage ) {
       messageTimer = setTimeout( "showMessage( dfltMessage )", 1000 ); }
}

//================================================================================

function deleteNavBoxes(currentSectionNbr) {

   clearTimeout( navBoxTimer );
   if ( typeof infoObj != 'undefined' && messageTimer != null && infoObj.innerHTML != dfltMessage ) {
      infoObj.innerHTML = '&nbsp;'; }
   if ( typeof debug != 'undefined' && debug & 0x10 && highestSectionNbr > currentSectionNbr ) { trace += '\nDelete section '+highestSectionNbr; }
   while ( highestSectionNbr > currentSectionNbr ) {
      document.getElementById(String(highestSectionNbr)).style.visibility="hidden";
      document.getElementById(String(highestSectionNbr--)).innerHTML = '';
      if ( typeof debug != 'undefined' && debug & 0x10 && highestSectionNbr > currentSectionNbr ) { trace += ','+String( highestSectionNbr ); }
   }
   if ( typeof debug != 'undefined' && debug & 0x10 ) { dbug(); }
   if ( highestSectionNbr == 0 && cursorSequence == '' ) buildAttributes(document.getElementById('A'));
}

//================================================================================

function buildAttributes(obj) {

   var thisArray = eval( obj.name );
   var lgt = thisArray.length;
   var childArray = thisArray[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][ArrayName];
   if ( childArray != '' ) {
      childArray = eval( childArray );
      lgt = childArray.length > lgt ? childArray.length : lgt; }

   var parentArray = '';
   if ( obj.id.length > 1 ) {
      parentArray = eval( document.getElementById(obj.id.substring(0,obj.id.length-1)).name );
      lgt = parentArray.length > lgt ? parentArray.length : lgt; }

   if ( (typeof debug != 'undefined' && debug & 0x20) || (typeof debug != 'undefined' && debug & 0x40) || (typeof debug != 'undefined' && debug & 0x80) ) { trace += '\n'; dbug(); }
   for ( var i = 0; i < lgt; i++ ) {
      if ( parentArray.length > i ) { attributes( document.getElementById( obj.id.substring( 0,obj.id.length-2 )+String.fromCharCode(alfaToNumeric+i) ) ); }
      if ( thisArray.length > i ) { attributes( document.getElementById( obj.id.substring( 0,obj.id.length-1 )+String.fromCharCode(alfaToNumeric+i) ) ); }
      if ( childArray.length > i && obj.id == cursorSequence ) { attributes( document.getElementById( obj.id+String.fromCharCode(alfaToNumeric+i) ) ); }
   }
}

//================================================================================

function attributes(obj) {

   if ( obj.id == currentDisplayedPageSequence ) {												// Current Displayed Page
      if ( obj.id == cursorSequence ) {														// Current object
         if ( typeof debug != 'undefined' && debug & 0x40 ) { trace += '\nCurrent obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set MouseOverCurrentDisplayedPage'; dbug(); }
         if ( MouseOverCurrentDisplayedPageColor != '' ) obj.style.color = (obj.id.length>1?MouseOverCurrentDisplayedPageColor:MainCurrentDisplayedPageColor);
         if ( MouseOverCurrentDisplayedPageBackgroundColor != '' ) obj.style.backgroundColor = (obj.id.length>1?MouseOverCurrentDisplayedPageBackgroundColor:MainMouseOverCurrentDisplayedPageBackgroundColor);
      }
      else if ( obj.id.length < cursorSequence.length && obj.id == cursorSequence.substring(0,obj.id.length) ) {		// Parent object
         if ( typeof debug != 'undefined' && debug & 0x40 ) { trace += '\nParent obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set MouseLeadInOverCurrentDisplayedPage'; dbug(); }
         if ( MouseLeadInOverCurrentDisplayedPageColor != '' ) obj.style.color = (obj.id.length>1?MouseLeadInOverCurrentDisplayedPageColor:MainMouseLeadInOverCurrentDisplayedPageColor);
         if ( MouseLeadInOverCurrentDisplayedPageBackgroundColor != '' ) obj.style.backgroundColor = (obj.id.length>1?MouseLeadInOverCurrentDisplayedPageBackgroundColor:MainMouseLeadInOverCurrentDisplayedPageBackgroundColor);
      }
      else {																		// Init, Parent, Current, Child object
         if ( typeof debug != 'undefined' && debug & 0x20 ) {
            tmp = cursorSequence.length < obj.id.length ? cursorSequence == '' ? 'Init' : 'Child' : cursorSequence.length == obj.id.length ? 'Current' : 'Parent';
            trace += '\n'+tmp+' obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set CurrentDisplayedPage'; dbug(); }
         obj.style.color = (obj.id.length>1?CurrentDisplayedPageColor:MainCurrentDisplayedPageColor);
         obj.style.backgroundColor = (obj.id.length>1?CurrentDisplayedPageBackgroundColor:MainCurrentDisplayedPageBackgroundColor); }
   }
   else if ( obj.id == currentDisplayedPageSequence.substring(0,obj.id.length) ) {							// Lead in to current displayed page
      if ( obj.id == cursorSequence ) {														// Current object
         if ( typeof debug != 'undefined' && debug & 0x40 ) { trace += '\nCurrent obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set MouseOverLeadInToCurrentDisplayedPage'; dbug(); }
         if ( MouseOverLeadInToCurrentDisplayedPageColor != '' ) obj.style.color = (obj.id.length>1?MouseOverLeadInToCurrentDisplayedPageColor:MainMouseOverLeadInToCurrentDisplayedPageColor);
         if ( MouseOverLeadInToCurrentDisplayedPageBackgroundColor != '' ) obj.style.backgroundColor = (obj.id.length>1?MouseOverLeadInToCurrentDisplayedPageBackgroundColor:MainMouseOverLeadInToCurrentDisplayedPageBackgroundColor);
      }
      else if ( obj.id == cursorSequence.substring(0,obj.id.length) ) {									// Could be parent object
         if ( typeof debug != 'undefined' && debug & 0x40 ) { trace += '\nParent obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set LeadInMouseOverLeadInToCurrentDisplayedPage'; dbug(); }
         if ( LeadInMouseOverLeadInToCurrentDisplayedPageColor != '' ) obj.style.color = (obj.id.length>1?LeadInMouseOverLeadInToCurrentDisplayedPageColor:MainLeadInMouseOverLeadInToCurrentDisplayedPageColor);
         if ( LeadInMouseOverLeadInToCurrentDisplayedPageBackgroundColor != '' ) obj.style.backgroundColor = (obj.id.length>1?LeadInMouseOverLeadInToCurrentDisplayedPageBackgroundColor:MainLeadInMouseOverLeadInToCurrentDisplayedPageBackgroundColor);
      }
      else {																		// Init, Parent, Current, Child object
         if ( typeof debug != 'undefined' && debug & 0x20 ) {
            tmp = cursorSequence.length < obj.id.length ? cursorSequence == '' ? 'Init' : 'Child' : cursorSequence.length == obj.id.length ? 'Current' : 'Parent';
            trace += '\n'+tmp+' obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set LeadInToCurrentDisplayedPage'; dbug(); }
         obj.style.color = (obj.id.length>1?LeadInToCurrentDisplayedPageColor:MainLeadInToCurrentDisplayedPageColor);
         obj.style.backgroundColor = (obj.id.length>1?LeadInToCurrentDisplayedPageBackgroundColor:MainLeadInToCurrentDisplayedPageBackgroundColor); }
   }
   else if ( obj.id == cursorSequence ) {														// At cursor position
      if ( typeof debug != 'undefined' && debug & 0x40 ) { trace += '\nCurrent obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set MouseOverObject'; dbug(); }			// current
      if ( MouseOverObjectColor != '' ) obj.style.color = (obj.id.length>1?MouseOverObjectColor:MainMouseOverObjectColor);
      if ( MouseOverObjectBackgroundColor != '' ) obj.style.backgroundColor = (obj.id.length>1?MouseOverObjectBackgroundColor:MainMouseOverObjectBackgroundColor);
   }
   else if ( obj.id == cursorSequence.substring(0,obj.id.length) ) {									// Lead in to cursor position
      if ( typeof debug != 'undefined' && debug & 0x40 ) { trace += '\nParent obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set LeadInMouseOverObject'; dbug(); }		// current
      if ( LeadInMouseOverObjectColor != '' ) obj.style.color = (obj.id.length>1?LeadInMouseOverObjectColor:MainLeadInMouseOverObjectColor);
      if ( LeadInMouseOverObjectBackgroundColor != '' ) obj.style.backgroundColor = (obj.id.length>1?LeadInMouseOverObjectBackgroundColor:MainLeadInMouseOverObjectBackgroundColor);
   }
   else {																			// Menu Text, Init, Parent, Current, Child object
      if ( typeof debug != 'undefined' && debug & 0x80 ) {
         tmp = cursorSequence.length < obj.id.length ? cursorSequence == '' ? 'Init' : 'Child' : cursorSequence.length == obj.id.length ? 'Current' : 'Parent';
         trace += '\n'+tmp+' obj: ('+eval(obj.name)[obj.id.charCodeAt(obj.id.length-1)-alfaToNumeric][LinkName]+') Set MenuObject'; dbug(); }
      obj.style.color = (obj.id.length>1?menuTextColor:MainMenuTextColor);
      obj.style.backgroundColor = (obj.id.length>1?menuTextBackgroundColor:MainMenuTextBackgroundColor);
   }
}

//================================================================================

function findPosX( obj )
{
  if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nxPos: '; }
  for ( var xPos = 0; obj.offsetParent; obj = obj.offsetParent ) {
     xPos += obj.offsetLeft;									// Add up all parent x positions
     if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(obj.offsetLeft)+','; }
  }
  return xPos;
}

//================================================================================

function findPosY( obj )
{
  if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += '\nyPos: '; }
  for ( var yPos = 0; obj.offsetParent; obj = obj.offsetParent ) {
     yPos += obj.offsetTop;									// Add up all parent y positions
     if ( typeof debug != 'undefined' && debug & 0x10 ) { trace += String(obj.offsetTop)+','; }
  }
  return yPos;
}

//================================================================================

function showMessage( str ) {

   clearTimeout( messageTimer );
   if ( typeof infoObj != 'undefined' ) { infoObj.innerHTML = str; }									// write message
}

//================================================================================

function findPageSequence(menuName) {

   for( var i = 0, searchArray = eval(menuName); i < searchArray.length; i++ ) {
      if ( pageURL == searchArray[i][filename] ) {
         pageSequence += String.fromCharCode(i+alfaToNumeric);
         return true; }
      if ( searchArray[i][ArrayName] == '' ) { continue; }
      pageSequence += String.fromCharCode(i+alfaToNumeric);
      if ( findPageSequence(searchArray[i][ArrayName]) ) { return true; }
      pageSequence = pageSequence.substring(0,pageSequence.length-1);
   }
   return false;
}

//================================================================================
