﻿/************************************************************************************************************************************
    menu.js
        
    Functions for handling menu events

************************************************************************************************************************************/

// Attaches event handlers to menu items in the document
function menuBar_attachEvents(init)
{   var currentReadyStateChanged;
    var menuItems;
    var menuItemIdx;
    var currentMenuItem;

    if (init == true)
    {   // Function has been called to initialise event handlers
        if (document.onreadystatechange)
        {   // Appends a call to this function to any existing window.onload eent
            currentReadyStateChanged = document.onreadystatechange;
            document.onreadystatechange = function()
            {   currentReadyStateChanged();
                menuBar_attachEvents(false);
            }
        }
        else
        {   document.onreadystatechange = function()
            {   menuBar_attachEvents(false);
            }
        }
    }
    else if(document.readyState == 'complete')
    {   // Attach an event handler to the document to hide all menus when the mouse moves away from the menubar
        document.all('contentContainer').onmouseover = menuBar_hideMenus;
    
        // Get a reference to all menu items in the document
        menuItems = document.all('menuItem');
        
        if (menuItems != null)
        {   // If the reference has a length property the reference is to an array of menu items
            if (menuItems.length)
            {   // Loop thtrough the menu items attaching events
                for (menuItemIdx=0; menuItemIdx<menuItems.length; menuItemIdx++)
                {   // Get a reference to the current item in the array
                    currentMenuItem = menuItems[menuItemIdx];
                    // Attach the onmouseover event handler
                    currentMenuItem.onmouseover = menuItem_onmouseover;
                    // Attach the onclick event handler
                    currentMenuItem.onclick = menuItem_onclick;
                }
            }
            else
            {   // Only a single menu item was returned
                // Attach the onmouseover event handler
                menuItems.onmouseover = menuItem_onmouseover;
                // Attach the onclick event handler
                menuItems.onclick = menuItem_onclick;
            }

            // Get a reference to all sub-menu items in the document
            menuItems = document.all('subMenuItem');
            
            if (menuItems != null)
            {   // If the reference has a length property the reference is to an array of sub-menu items
                if (menuItems.length)
                {   // Loop thtrough the sub-menu items attaching events
                    for (menuItemIdx=0; menuItemIdx<menuItems.length; menuItemIdx++)
                    {   // Get a reference to the current item in the array
                        currentMenuItem = menuItems[menuItemIdx];
                        // Attach the onclick event handler
                        currentMenuItem.onclick = menuItem_onclick;
                    }
                }
                else
                {   // Only a single menu item was returned
                    // Attach the onclick event handler
                    menuItems.onclick = menuItem_onclick;
                }
            }
        }
    }
}

// Hides all sub-menus
function menuBar_hideMenus()
{   var menuItems;
    var menuItemIdx;
    var currentMenuItem;
    var subMenuId;
    var subMenu;

    // Get a reference to all menu items in the document
    menuItems = document.all('menuItem');
        
    if (menuItems != null)
    {   // If the reference has a length property the reference is to an array of menu items
        if (menuItems.length)
        {   // Loop thtrough the menu items
            for (menuItemIdx = 0; menuItemIdx < menuItems.length; menuItemIdx++)
            {   // Get a reference to the current item in the array
                currentMenuItem = menuItems[menuItemIdx];
                // Get the id of the sub-menu associated with the menu item
                subMenuId = currentMenuItem.getAttribute('submenu');
                
                if (subMenuId)
                {   // Get a reference to the sub-menu
                    subMenu = document.all(subMenuId);
                    // Hide the sub-menu
                    subMenu.style.visibility = 'hidden';
                    // Move the sub-menu off the edge of the window to avoid 
                    // interference with the placement of other elements
                    subMenu.style.left = -65534;
                }
            }
        }
        else
        {   // Only a single menu item was returned
            // Get the id of the sub-menu associated with the menu item
            subMenuId = menuItems.getAttribute('submenu');
                
            if (subMenuId)
            {   // Get a reference to the sub-menu
                subMenu = document.all(subMenuId);
                // Hide the sub-menu
                subMenu.style.visibility = 'hidden';
                // Move the sub-menu off the edge of the window to avoid 
                // interference with the placement of other elements
                subMenu.style.left = -65534;
            }
        }
    }
}

// OnMouseOver event handler for menus
function menuItem_onmouseover()
{   // Get the element that raised the event
    var menuItem = event.srcElement;
    // Get the id of the sub-menu associated with the menu item
    var subMenuId = menuItem.getAttribute('submenu');
    var subMenu;
    
    // Hide all sub-menus
    menuBar_hideMenus();
    
    if (subMenuId)
    {   // Get a reference to the sub-menu
        subMenu = document.all(subMenuId);

        // Make the sub-menu visible
        subMenu.style.visibility = 'visible';
        // Move the sub-menu below the source element
        subMenu.style.left = getElementX(menuItem);
        subMenu.style.top = getElementY(menuItem) + 24;
    }
}
// OnClick event handler for menus and menu-items
function menuItem_onclick()
{   // Get the element that raised the event
    var menuItem = event.srcElement;
    var href;
    
    // Get the URL of the link associated with the item
    href = menuItem.getAttribute('href');
    if (href)
    {   // Navigate to the specified URL
        self.location.href = href;
    }    
}

// Attach event handlers to menu objects
menuBar_attachEvents(true);
