// generate the Menu structure that HV_Menu wants from the Actinic section tree structures.
// last update 28-09-06 V1.14
if ( AutoSize )
   {
   var ExpYes=navigator.userAgent.toLowerCase().indexOf('msie')!=-1?1:0;
   var WinHeight = ExpYes?document.body.clientHeight:window.innerHeight;
   }

if (SingleRoot) NoOffFirstLineMenus = 1; else NoOffFirstLineMenus = section_tree_names.length;

// Menu tree structure ( X is 1..n 1st level, 1_1..n second level 1_1_1..n third, etc
//	MenuX=new Array(Text to show, Link, background image (optional), number of sub elements, height, width);

function BuildMenuArray(pItem, pURL, base)
 {
 if ( MaxDepth == 0) MaxDepth = 1000;
 if (ThisLevel == 0) { var LineWidth=FirstLineWidth; var LineHeight=FirstLineHeight; var BGImage=FirstBGImage}
   else { var LineWidth=SubLineWidth; var LineHeight=SubLineHeight; var BGImage=SubBGImage} 
 ThisLevel++;
  
 if ( AutoSize )
   {
   var NumItems = pItem.length;
   var MenuHeight = NumItems * LineHeight;
   var BorderHeight=BorderBtwnElmnts?BorderWidth * NumItems:0;
   if ((MenuHeight + BorderHeight) > WinHeight)
     {
     LineHeight = Math.floor((WinHeight - BorderHeight) / NumItems);	// auto size
     if ( LineHeight < AutoMin ) LineHeight = AutoMin;   		// if too small give up and set a minimum height
     }
   }

 for (var i = 1; i <= pItem.length; i++)
   {
   if ((pItem[i].pChild) && (ThisLevel < MaxDepth)) var children = pItem[i].pChild.length; else children = 0;
   // build a menu line from actinic section array and also fix the &#nn; stuff that Actinic puts in
   var LinkURL = pURL[i].sURL.replace(/&amp;/ig, '&');

   // deal with V5 Preview by stripping all but page name
   if (location.href.indexOf('PreviewHTML') != -1 ) LinkURL = LinkURL.replace(/.*SECTIONID=(.*)&NOLOGIN.*/, "$1");
     // if we've a LinkPrefix then make absolute URL from it
     else if ( LinkPrefix )
       {
       var thispage = location.href;
       if ( ! (LoggedIn && (thispage.search(/(ss|ca|os)\d{6}.pl/) != -1)) )  // use Actinic original link if on Search,Cart or Checkout
         {
         // we're on a Product or Brochure page so let's see if we're a logged in customer.
         var scriptpos = thispage.search(/bb\d{6}\.pl/);   // looking for bb000001.pl (any 6 digit number)
         if ( scriptpos != -1 )
           // form a business logged in type URL
           {
           var gotoURL = unescape(LinkURL.replace(/.*SECTIONID=(.*)&NOLOGIN.*/, "$1"));  // recover the actual URL
           var busURL = thispage.substring(0, scriptpos + 11);  // the  http://www.mysite.com/cgi-bin/.bb000001.pl  bit
           LinkURL = busURL + '?ACTINIC_REFERRER=' + busURL + '&PRODUCTPAGE=' + gotoURL;  // form the logged in link
           }
             // simple absolute link
             else LinkURL = LinkPrefix + unescape(LinkURL.replace(/.*SECTIONID=(.*)&NOLOGIN.*/, "$1"));
         }
       }

   var SName = pItem[i].sName;
   var I = SName.indexOf("&#");   // Actinic escapes special characters as &#123; we need to restore the ascii
   while ( I > -1) 
     {
     var J = SName.indexOf(";", I);
     var SNVal = SName.substring(I + 2, J);
     SName = SName.substring(0, I) + String.fromCharCode(SNVal - 0) + SName.substr(J + 1);
     I = SName.indexOf("&#");
     }
   SName = SName.replace(/"/g, "\\\"");    // escape double quotes
   if ( OnlyFinalLinks && children ) LinkURL = '';    // hide intermediate nodes if required
   eval('Menu' + base + i + '=new Array("' + SName
	+ '","' + LinkURL + '","' + BGImage + '",' + children + ',' + LineHeight + ',' + LineWidth + ');');
   // recurse if child section
   if ((pItem[i].pChild) && (ThisLevel < MaxDepth)) { BuildMenuArray(pItem[i].pChild, pURL[i].pChild, base + i + '_'); }
   }
 ThisLevel--;
 }

 var ThisLevel = 0;
 if (SingleRoot) 
   {
   eval('Menu1=new Array("' + FirstLineText + '","' + FirstLineURL + '","' + FirstBGImage 
        + '",' + section_tree_names.length + ',' + FirstLineHeight + ',' + FirstLineWidth + ');');
   ThisLevel++;
   BuildMenuArray(section_tree_names,section_tree_URLs, '1_');
   }
  else                                          // multiple level base
   {
   BuildMenuArray(section_tree_names,section_tree_URLs, '');
   }
// free up actinic arrays
section_tree_names.length = 0;
section_tree_URLs.length = 0;
// end of - generate the Menu structure that HV_Menu wants from the Actinic section tree structures.

