
// Assign the menu array sizes
var numLinks = 10;
var allowedSubLinks = 8;
var rootPath = "../";

// Create the base level menu arrays
var arrLinkText = new Array(numLinks);
var arrLinkHref = new Array(numLinks);
var arrLinkOpenNew = new Array(numLinks);
var arrLinkHasSubLinks = new Array(numLinks);
var arrLinkImg = new Array(numLinks);

// Default all "Open New" flags to 'false' and
// all "HasSubLink" flasgs to 'false' and
// clear all Link Images
for (var j = 0; j < numLinks; j++)
{
    arrLinkOpenNew[j] = false;
    arrLinkHasSubLinks[j] = false;
    arrLinkImg[j] = "";
}

// Assign the Base-Level menu items
// All links within these menus, need to be absolute paths.
// They cannot be relative because java will interpret a relative path to be relative
//   to the current position within the site. A relative path for pageA while on pageA will
//   not necessarily be the same as a relative path for pageA while on pageB. The only instance
//   in which relative paths will work, is when all pages exist in the same directory on the web server.
arrLinkText[0] = "Home";
arrLinkHref[0] = "index.html";

arrLinkText[1] = "About ANSA";
arrLinkHref[1] = "aboutANSA/index_aboutANSA.html";
arrLinkHasSubLinks[1] = true;

arrLinkText[2] = "Join ANSA";
arrLinkHref[2] = "aboutANSA/page_MembershipCriteria.html";

arrLinkText[3] = "Chandlery";
arrLinkHref[3] = "chandlery/index_chandlery.html";

arrLinkText[4] = "Activities";
arrLinkHref[4] = "activities/index_activities.html";
arrLinkHasSubLinks[4] = true;

arrLinkText[5] = "Training";
arrLinkHref[5] = "training/index_training.html";
arrLinkHasSubLinks[5] = true;

arrLinkText[6] = "Pictures";
arrLinkHref[6] = "pictures/index_pictures.html";
//arrLinkHasSubLinks[6] = true;

arrLinkText[7] = "Ketchall";
arrLinkHref[7] = "ketchall/index_ketchall.html";

arrLinkText[8] = "Associated Clubs";
arrLinkHref[8] = "associated_clubs/index_AssociatedClubs.html";

arrLinkText[9] = "Links";
arrLinkHref[9] = "links/index_Links.html";

// Create the sub-level menu arrays
var arrSubLinkText = MultiDimensionalArray(numLinks, allowedSubLinks);
var arrSubLinkHref = MultiDimensionalArray(numLinks, allowedSubLinks);
var arrSubLinkOpenNew = MultiDimensionalArray(numLinks, allowedSubLinks);
var arrSubLinkImg = MultiDimensionalArray(numLinks, allowedSubLinks);

// Initialize the arrays for the sub-menus
for( var j = 0; j < numLinks; j++ )
{
    for( var k= 0; k < allowedSubLinks; k++ )
    {
        arrSubLinkText[j][k] = "";
        arrSubLinkHref[j][k] = "#";
        arrSubLinkOpenNew[j][k] = false;
        arrSubLinkImg[j][k] = "";
    }
}

// Assign the sub-level menu items

// "About ANSA" submenus
//arrSubLinkText[1][0] = "About ANSA Home";
//arrSubLinkHref[1][0] = "aboutANSA/index_aboutANSA.html";

arrSubLinkText[1][0] = "Officers";
arrSubLinkHref[1][0] = "aboutANSA/officer_Summary.html";

arrSubLinkText[1][1] = "Mission";
arrSubLinkHref[1][1] = "aboutANSA/page_Mission.html";

arrSubLinkText[1][2] = "Our Boats";
arrSubLinkHref[1][2] = "aboutANSA/page_OurBoats.html";

arrSubLinkText[1][3] = "Procedures / By-Laws";
arrSubLinkHref[1][3] = "aboutANSA/aboutANSA_resources/ANSA_PolicyProcedures2k.pdf";
arrSubLinkImg[1][3] = "common_images/PDF_text_blue.gif";
arrSubLinkOpenNew[1][3] = true;

arrSubLinkText[1][4] = "Constitution";
arrSubLinkHref[1][4] = "aboutANSA/aboutANSA_resources/ANSA_ConstitutionNov2008.pdf";
arrSubLinkImg[1][4] = "common_images/PDF_text_blue.gif";
arrSubLinkOpenNew[1][4] = true;

//arrSubLinkText[1][5] = "Meeting Minutes";
//arrSubLinkHref[1][5] = "aboutANSA/page_MeetingMinutes.html";

// "Activities" submenus

arrSubLinkText[4][0] = "Calendar";
arrSubLinkHref[4][0] = "activities/page_CombinedCalendar.html";
/*
arrSubLinkText[3][1] = "Dinner Cruises";
arrSubLinkHref[3][1] = "calendar/page_DaySails-DinnerCruises.html";

arrSubLinkText[3][2] = "Day Sails, Overnight Trips";
arrSubLinkHref[3][2] = "calendar/page_DaySails-DinnerCruises.html";

arrSubLinkText[3][3] = "Parties and Potlucks";
arrSubLinkHref[3][3] = "calendar/page_PartiesPotlucks.html";

arrSubLinkText[3][4] = "Training";
arrSubLinkHref[3][4] = "calendar/page_Training.html";

arrSubLinkText[3][5] = "Club Ops";
arrSubLinkHref[3][5] = "calendar/page_ClubOps.html";

arrSubLinkText[3][6] = "Maintenance Days";
arrSubLinkHref[3][6] = "calendar/page_MaintenanceDays.html";
*/


// "Training" submenus
//arrSubLinkText[4][0] = "Training Home";
//arrSubLinkHref[4][0] = "training/index_training.html";

arrSubLinkText[5][0] = "Qualification Summary";
arrSubLinkHref[5][0] = "training/qual_Summary.html";

arrSubLinkText[5][1] = "Course Offerings";
arrSubLinkHref[5][1] = "training/course_CourseOfferings.html";

arrSubLinkText[5][2] = "Grog's Knots";
arrSubLinkHref[5][2] = "training/page_GrogsKnots.html";

arrSubLinkText[5][3] = "Resources";
arrSubLinkHref[5][3] = "training/page_Resources.html";

arrSubLinkText[5][4] = "Links";
arrSubLinkHref[5][4] = "training/page_TrainingLinks.html";

// "Pictures" submenus
//arrSubLinkText[5][0] = "Pictures Home";
//arrSubLinkHref[5][0] = "pictures/index_pictures.html";

/*
arrSubLinkText[5][0] = "Our Boats";
arrSubLinkHref[5][0] = "pictures/page_ANSABoats.html";

arrSubLinkText[5][1] = "2007 Holiday Party";
arrSubLinkHref[5][1] = "pictures/page_HolidayParty.html";

arrSubLinkText[5][2] = "Day Sails, Dinner Cruises";
arrSubLinkHref[5][2] = "pictures/page_DaySails-DinnerCruises.html";

arrSubLinkText[5][3] = "Training";
arrSubLinkHref[5][3] = "pictures/page_Training.html";

arrSubLinkText[5][4] = "Social Hours";
arrSubLinkHref[5][4] = "pictures/page_SocialHours.html";

arrSubLinkText[5][5] = "Club Ops";
arrSubLinkHref[5][5] = "pictures/page_ClubOps.html";

arrSubLinkText[5][6] = "Maintenance Days";
arrSubLinkHref[5][6] = "pictures/page_MaintenanceDays.html";
*/

// Create multi-dimensional arrays
function MultiDimensionalArray(iRows,iCols) 
{ 
    var i; 
    var j; 
    var a = new Array(iRows); 
    for (i=0; i < iRows; i++) 
    { 
       a[i] = new Array(iCols); 
    } 
    return(a); 
} 

// Function called to expand sub-menus when the parent menu item is hovered over.
function expand(s)
{
  var td = s;

  // Continue only if there is some text value in the table cell.
  if (td != "")
  {
    td.className = "menuHover";
    var d = td.getElementsByTagName("div").item(0);
    if (d != null)
      d.className = "menuHover";
  }
}

// Function called to collapse sub-menus when the parent menu item loses focus.
function collapse(s)
{
  var td = s;
  
  // Continue only if the table cell exists
  if (td != null)
  {
	td.className = "menuUp";
    var d = td.getElementsByTagName("div").item(0);
    if (d != null)
      d.className = "menuUp";
  }
}

//function collapseActive(s)
//{
//  var td = s;
//    
//  if (td != null)
//  {
//    td.className = "menuUpActive";
//    var d = td.getElementsByTagName("div").item(0);
//    if (d != null)
//      d.className = "menuUpActive";
//  }
//}

function createSideMenu(folderLevel)
{   
    var s = new String;
    var myS = new String;

    for (var i = 0; i < numLinks; i++)
    {        
        s = "";
        s += "<tr>";

        // Create base level menu item
        s += "<td class=\"menuUp\" onmouseover=\"expand(this); this.style.cursor='pointer';\" onmouseout=\"collapse(this);\" ";
        
        // This IF statement checks for the existance of sub menus. If any exist, the menu
        // item is not linked to a new page.
        
        // **********************************************************************************************************
        // With the current design, if the base menu item contains submenu items and contains a link itself,
        // when the user clicks on the submenu item, the base menu item link is called. In otherwords, if a base menu
        // item contains a link to another page and also contains sub menu items, all submenu items link
        // the base menu item. Therefore, the submenu items are inaccessible. Removing the base menu item link
        // fixes the problem. I do not understand why and am currently attempting to correct the problem. 12-28-2007
        // 
        // The problem has been fixed with a little help. Because we are using tables as the menu items and sub tables
        // as sub menus, any event triggered on a sub menu will "bubble up" through the hierarchy of event handlers.
        // This in turn triggers the event for the parent item, thus being the base-menu item. Since the base menu item
        // has a link to something, it overrides the sub menu item.
        // **********************************************************************************************************
        //if (!arrLinkHasSubLinks[i] )
        {
            // Check for "open in new window" flag
            if (arrLinkOpenNew[i])
            {
                // Flag set to open link in a new window
                // There is no reason to check for folder level here, because the link will be
                // opened in a new window and will have no relation to the current page location.
                s += "onclick=\"window.open('" + arrLinkHref[i] + "');\">";
            }
            else
            {
                // Open link in existing window
                if( folderLevel == 0 )
                {
                    // Use reference for index page menu
                    s += "onclick=\"document.location.href='" + arrLinkHref[i] + "';\">";
                }
                else
                {
                    // Use reference for non-index page menu
                    s += "onclick=\"document.location.href='" + rootPath + arrLinkHref[i] + "';\">";
                }
            }
        }
        //else
        {
        //    s += ">";
        }
        
        // Create cell/top-level-menu-item text.
        s += "<span class=\"menutext\">";
        s += arrLinkText[i].toString();
        s += "</span>";

        // If the menu item has submenus, create them.
        if (arrLinkHasSubLinks[i])
        {
            s += "<div class=\"menuUp\" style=\"width: 190px;\">";
            s += "<table class=\"menu\" cellspacing=\"0\" cellpadding=\"1\" width=\"100%\"><tbody>";
        
            for (var subi = 0; subi < allowedSubLinks; subi++)
            {
                // Continue if the submenu item text is NOT empty.
                if (arrSubLinkText[i][subi] != "")
                {
                    s += "<tr>";
                    
                    s += "<td class=\"menuUp\" onmouseover=\"expand(this); this.style.cursor='pointer'\" onmouseout=\"collapse(this);\" style=\"width:252px\" ";
                    
                    // Check for the "open in new window" flag
                    if (arrSubLinkOpenNew[i][subi])
                    {
                        // Flag set to open link in a new window
                        // There is no reason to check for folder level here, because the link will be
                        // opened in a new window and will have no relation to the current page location.
                        if( folderLevel == 0 )
                        {
                            s += "onclick=\"window.open('" + arrSubLinkHref[i][subi] + "'); window.event.cancelBubble=true;\">";
                        }
                        else
                        {
                            s += "onclick=\"window.open('" + rootPath + arrSubLinkHref[i][subi] + "'); window.event.cancelBubble=true;\">";
                        }
                    }
                    else
                    {                        
                        // Open link in existing window
                        if( folderLevel == 0 )
                        {
                            // Use reference for index-page menu
                            // Using the cancelBubble=true, kills the event from "bubbling up" the hierarchy of event handlers
                            s += "onclick=\"document.location.href='" + arrSubLinkHref[i][subi] + "'; window.event.cancelBubble=true;\">";
                            
                        }
                        else
                        {
                            // Use reference for non-main-index-page menu
                            // Using the cancelBubble=true, kills the event from "bubbling up" the hierarchy of event handlers
                            s += "onclick=\"document.location.href='" + rootPath;
                            s += arrSubLinkHref[i][subi] + "'; window.event.cancelBubble=true;\">";
                        }
                    }
                
                    s += "<span class=\"menutext\">";
                    //if ((i == menuItem-1) && (subi == subMenuItem-1))
                    //    s += "-> ";
                    
                    s += arrSubLinkText[i][subi].toString();
                    
                    if( arrSubLinkImg[i][subi].toString() != "" )
                    {
                        s += "&nbsp <img src='";
                        
                        if( folderLevel == 0 )
                        {
                            s += arrSubLinkImg[i][subi].toString();
                        }
                        else
                        {
                            s += rootPath + arrSubLinkImg[i][subi].toString();
                        }
                        
                        s += "' alt='PDF' />";
                    }
                        
                    s += "</span>";
                
                    s += "</td>";
                    s += "</tr>";
                }
            }
            
            s += "</tbody></table>";
           s += "</div>";
        }

        s += "</td>";
        s += "</tr>";
        
        document.write(s);
    }
}
