//  -----------
//  PREFERENCES
//  -----------
    var COLOR_CURRENTTAB_BACKGROUND         = '#B0BCBC';
    var COLOR_CURRENTTAB_TEXT               = 'black';
    var COLOR_MAINMENU_INACTIVE_BACKGROUND  = '#606A74';
    var COLOR_MAINMENU_INACTIVE_TEXT        = 'white';
    var COLOR_MAINMENU_ACTIVE_BACKGROUND    = '#798A8D';
    var COLOR_MAINMENU_ACTIVE_TEXT          = 'white';
    var COLOR_SUBMENU_INACTIVE_BACKGROUND   = 'white';
    var COLOR_SUBMENU_INACTIVE_TEXT         = '#89939C';
    var COLOR_SUBMENU_ACTIVE_BACKGROUND     = '#DDE5E7';
    var COLOR_SUBMENU_ACTIVE_TEXT           = 'black';
    var COLOR_SUBMENU_SPACER_BACKGROUND     = '#B0BCBC';
    var COLOR_SUBMENU_BORDER                = '#B0BCBC';
    var TIME_HIDE_MENU_DELAY_IN_MS          = 100;
//    var MENU_WIDTH                          = '128px';
//    var MENU_WIDTH2                         = '130px';
    var MENU_WIDTH                          = '136px';
    var MENU_WIDTH2                         = '137px';


//  -----------------------------
//  Define the Menus and Items
//
//  FORMAT: Name,LinkURL[,Parent]
//  -----------------------------
    var aMainMenuItems  = [
        [ 'home',        'index.jsp'        ],
        [ 'about',       'aboutus.jsp'      ],
        [ 'register',    'register.jsp'     ],
        [ 'faq',     	 'faq.jsp'          ],
        [ 'sample maps', 'samples.jsp'      ],
        [ 'contact us',  'contact.jsp'      ]
    ];
    var aSubMenuItems   = [

        //  These are temporary items to show the multi-dimensional hierarchy in action. Comment out when not required.
        /*
        [ 'media centre2',  'aboutus_media.jsp',    'media centre'  ],
        [ 'media centre3',  'aboutus_media.jsp',    'media centre'  ],
        [ 'media centre4',  'aboutus_media.jsp',    'media centre2' ],
        */

        //  These are the proper menu links
        [ 'goview.co.uk',       'aboutus.jsp',              'about'     ],
        [ 'corporate',          'aboutus_corporate.jsp',    'about'     ],
        [ 'media centre',       'aboutus_media.jsp',        'about'     ]
        /*
        [ 'faq',                'faq.jsp',                  'support'   ],
        [ 'user guide',         'userguide.jsp',            'support'   ]
		*/
    ];


//  ----------------
//  Global Variables
//  ----------------
    var sCursor             = is_ie ? 'hand' : 'pointer';
    var sActiveMainMenuItem = null;
    var iNetscapeXOffsetA   = 0;
    var iNetscapeYOffsetA   = 0;
    var iNetscapeXOffsetB   = 1;
    var iNetscapeYOffsetB   = 1;
    var iOffsetXA            = -2;
    var iOffsetYA            = 0;
    var iOffsetXB            = 0;
    var iOffsetYB            = -1;
    var aActiveMenus        = new Array();
    var iHideMenusTimeout   = 0;


//  ----------------------
//  Mouse Event Aesthetics
//  ----------------------
    function showCurrentTab( oMenuItem )
    {
        //  Set this item's aesthetics
        with ( oMenuItem.style )
        {
            height      = '21px';
            fontWeight  = 'bold';
            width       = MENU_WIDTH;
            background  = COLOR_CURRENTTAB_BACKGROUND;
            color       = COLOR_CURRENTTAB_TEXT;
        }
    }
    function showMenuOver( oMenuItem )
    {
        //  Check this isn't an active menu
        if ( !isCurrentTab(oMenuItem) )
        {
            //  Set this item's aesthetics
            with ( oMenuItem.style )
            {
                height      = '21px';

                if ( isMainMenuItem(oMenuItem) )
                {
                    width       = MENU_WIDTH;
                    background  = COLOR_MAINMENU_ACTIVE_BACKGROUND;
                    color       = COLOR_MAINMENU_ACTIVE_TEXT;
                }
                else
                {
                    fontWeight  = 'normal';
                    width       = MENU_WIDTH2;
                    background  = COLOR_SUBMENU_ACTIVE_BACKGROUND;
                    color       = COLOR_SUBMENU_ACTIVE_TEXT;
                }
            }
        }
    }
    function showMenuOff( oMenuItem )
    {
        //  Check this isn't an active menu
        if ( !isActiveMenu(oMenuItem) && !isCurrentTab(oMenuItem) )
        {
            //  Set this item's aesthetics
            with ( oMenuItem.style )
            {
                fontWeight  = 'normal';
                height      = '21px';

                if ( isMainMenuItem(oMenuItem) )
                {
                    width       = MENU_WIDTH;
                    background  = COLOR_MAINMENU_INACTIVE_BACKGROUND;
                    color       = COLOR_MAINMENU_INACTIVE_TEXT;
                }
                else
                {
                    width       = MENU_WIDTH2;
                    background  = COLOR_SUBMENU_INACTIVE_BACKGROUND;
                    color       = COLOR_SUBMENU_INACTIVE_TEXT;
                }
            }
        }
    }


//  -----------------------------
//  Menu initialisation functions
//  -----------------------------
    function menuInit()
    {
        //  Make the currently-active tab be permanently "mouse-overed"
        if ( typeof sCurrentTab != 'undefined' && sCurrentTab != '' )
            showCurrentTab( getMenuObjectFromContent(sCurrentTab) );
    }


//  ---------------------------
//  Main menu-utility-functions
//  ---------------------------
    function doMenuClick( oMenuItem )
    {
        window.location.href    = getLink( oMenuItem );
    }
    function showSubMenu( oMenuItem )
    {
        //  Deactivate the hide timeout
        window.clearTimeout( iHideMenusTimeout );

        //  Variables
        var iMenuLevel  = getMenuLevel( oMenuItem );

        //  Hide all menus below this one
        hideAllMenusBelowLevel( iMenuLevel );

        //  Update the active-menu-array
        aActiveMenus.length = iMenuLevel - 1;
        aActiveMenus[aActiveMenus.length] = getMenuText( oMenuItem );

        //  Set all same-level menus apart from this one to be off
        setActiveItemAtLevel( getMenuText(oMenuItem) );

        //  Break out if no sub-menu available
        if ( !hasSubMenu(oMenuItem) )
            return false;

        //  Check for a valid submenutable and create one if none exists
        if ( document.getElementById('table_submenu'+iMenuLevel) == null )
        {
            createNewSubMenuTable( iMenuLevel );
        }
        var oSubMenuTable   = document.getElementById( 'table_submenu' + iMenuLevel );

        //  More Variables
        var aThisMenuItems  = getSubMenuItems( oMenuItem );
        var iThisLeft       = getLeft( oMenuItem );
        var iThisTop        = getTop( oMenuItem );
        var iThisHeight     = oMenuItem.offsetHeight;
        var iThisWidth      = oMenuItem.offsetWidth;
        var oSubMenuTbody   = oSubMenuTable.getElementsByTagName( 'TBODY' )[0];

        //  Clear the table
        while ( oSubMenuTbody.hasChildNodes() )
            oSubMenuTbody.removeChild( oSubMenuTbody.childNodes[0] );

        //  Build the table
        for ( var iRow = 0; iRow < aThisMenuItems.length; iRow++ )
        {
            //  Make the objects
            var oNewTR      = document.createElement( 'TR' );
            var oNewTD      = document.createElement( 'TD' );
            var sNewTEXT    = aSubMenuItems[ aThisMenuItems[iRow] ][0];
            var oNewTEXT    = document.createTextNode( sNewTEXT );

            //  Add attributes/events to the objects
            oNewTD.onmouseover      = new Function( 'showSubMenu(this); showMenuOver(this);' );
            oNewTD.onmouseout       = new Function( 'startHideTimeout(); showMenuOff(this);' );
            oNewTD.onclick          = new Function( 'doMenuClick(this)' );

            //  Style the cell
            with ( oNewTD.style )
            {
                width       = MENU_WIDTH2;
                height      = '21px';
                fontWeight  = 'normal';
                cursor      = sCursor;
                textAlign   = 'center';
                background  = COLOR_SUBMENU_INACTIVE_BACKGROUND;
                color       = COLOR_SUBMENU_INACTIVE_TEXT;
                borderTop   = ( iRow > 0 ) ? '1px solid ' + COLOR_SUBMENU_SPACER_BACKGROUND : 'none';
            }

            //  Add objects to the table
            oNewTD.appendChild( oNewTEXT );
            oNewTR.appendChild( oNewTD );
            oSubMenuTbody.appendChild( oNewTR );
        }

        //  Show the table
        with ( oSubMenuTable.style )
        {
            display = 'block';
            if ( is_opera )
            {
                width   = '0px';
                display = 'table';
            }

            if ( iMenuLevel == 1 )
            {
                top     = is_gecko ? (iThisTop+iThisHeight+iNetscapeYOffsetA+iOffsetYA) + 'px' : (iThisTop+iThisHeight+iOffsetYA) + 'px';
                left    = is_gecko ? (iThisLeft+iNetscapeXOffsetA+iOffsetXA) + 'px' : (iThisLeft+iOffsetXA) + 'px';
            }
            else
            {
                top     = is_gecko ? (iThisTop+iNetscapeYOffsetB+iOffsetYB) + 'px' : (iThisTop+iOffsetYB) + 'px';
                left    = is_gecko ? (iThisLeft+iThisWidth+iNetscapeXOffsetB+iOffsetXB) + 'px' : (iThisLeft+iThisWidth+iOffsetXB) + 'px';
            }

        }
    }
    function setActiveItemAtLevel( sMenuItem )
    {
        //  Get the menu cell
        var oMenuItem   = getMenuObjectFromContent( sMenuItem );

        //  Get the TBODY
        var oThisTbody  = oMenuItem;
        while ( oThisTbody.tagName != 'TBODY' )
        {
            oThisTbody  = oThisTbody.parentNode;
        }

        //  Turn off the inactive menus
        var aSameLevelMenus = oThisTbody.getElementsByTagName( 'TD' );
        for ( var i = 0; i < aSameLevelMenus.length; i++ )
        {
            var oNextMenuItem   = aSameLevelMenus[i];
            if ( !isSpacerCell(oNextMenuItem) && !isActiveMenu(oNextMenuItem) )
            {
                showMenuOff( oNextMenuItem );
            }
        }
    }
    function createNewSubMenuTable( iSubMenuLevel )
    {
        //  Set up variables
        var oNewTABLE   = document.createElement( 'TABLE' );
        var oNewTBODY   = document.createElement( 'TBODY' );
        var oNewTR      = document.createElement( 'TR' );
        var oNewTD      = document.createElement( 'TD' );
        var oNewTEXT    = document.createTextNode( 'Flibble' );

        //  Define the attributes of the new table
        with ( oNewTABLE )
        {
            id              = 'table_submenu' + iSubMenuLevel;
            border          = '0';
            cellSpacing     = '0';
            cellPadding     = '0';
            onselectstart   = returnFalse;
        }

        //  Define the style of the new table
        with ( oNewTABLE.style )
        {
            border      = '1px solid ' + COLOR_SUBMENU_BORDER;
            position    = 'absolute';
            left        = '0px';
            top         = '0px';
            display     = 'none';
        }

        //  Add the objects to the document
        oNewTD.appendChild( oNewTEXT );
        oNewTR.appendChild( oNewTD );
        oNewTBODY.appendChild( oNewTR );
        oNewTABLE.appendChild( oNewTBODY );
        document.body.appendChild( oNewTABLE );
    }
    function hideAllMenusBelowLevel( iLevel )
    {
        var oMenuTable  = document.getElementById( 'table_submenu' + iLevel );
        while ( oMenuTable != null )
        {
            //  Hide table
            oMenuTable.style.display    = 'none';

            //  Get next table
            oMenuTable  = document.getElementById( 'table_submenu' + ++iLevel );
        }
    }
    function hideAllMenus()
    {
        //  Make no menus active
        aActiveMenus.length = new Array();

        //  Note that because the active-menus-array length is zero, this will turn all items off
        setActiveItemAtLevel( aMainMenuItems[0][0] );

        //  There is no zero submenu table, so set to 1 and start from there
        hideAllMenusBelowLevel( 1 );
    }
    function startHideTimeout()
    {
        //  Start the countdown to menu hiding
        iHideMenusTimeout   = ( is_mac && is_ie ) ? window.setTimeout( 'hideAllMenus()', TIME_HIDE_MENU_DELAY_IN_MS ) : window.setTimeout( hideAllMenus, TIME_HIDE_MENU_DELAY_IN_MS );
    }


//  ---------------------
//  'Discovery' functions
//  ---------------------
    function isActiveMenu( oMenuItem )
    {
        var bIsActive   = false;

        for ( var i = 0; i < aActiveMenus.length; i++ )
        {
            if ( aActiveMenus[i] == getMenuText(oMenuItem) )
                bIsActive   = true;
        }

        return bIsActive;
    }
    function isCurrentTab( oMenuItem )
    {
        var sMenuItem   = getMenuText( oMenuItem );
        return ( typeof sCurrentTab != 'undefined' ) && ( sCurrentTab == sMenuItem );
    }
    function isSpacerCell( oCell )
    {
        return ( oCell.getElementsByTagName('IMG').length > 0 );
    }
    function getMenuObjectFromContent( sMenuItem )
    {
        var oMenuItem       = null;
        var iMenuCheckLevel = 0;

        while ( oMenuItem == null )
        {
            if ( iMenuCheckLevel == 0 )
            {
                var oMenuTable      = document.getElementById( 'table_mainmenu' );
                var aMainMenuCells  = oMenuTable.getElementsByTagName( 'TD' );
                for ( var iCell = 0; iCell < aMainMenuCells.length; iCell++ )
                {
                    if ( getMenuText(aMainMenuCells[iCell]) == sMenuItem )
                        oMenuItem   = aMainMenuCells[iCell];
                }
            }
            else
            {
                var oMenuTable      = document.getElementById( 'table_submenu' + iMenuCheckLevel );
                var aMainMenuCells  = oMenuTable.getElementsByTagName( 'TD' );
                for ( var iCell = 0; iCell < aMainMenuCells.length; iCell++ )
                {
                    if ( getMenuText(aMainMenuCells[iCell]) == sMenuItem )
                        oMenuItem   = aMainMenuCells[iCell];
                }
            }

            iMenuCheckLevel++;
        }
        
        return oMenuItem;
    }
    function hasSubMenu( oMenuItem )
    {
        var bHasSubMenu = false;

        for ( var i = 0; i < aSubMenuItems.length; i++ )
        {
            if ( aSubMenuItems[i][2] == getMenuText(oMenuItem) )
                bHasSubMenu = true;
        }

        return bHasSubMenu;
    }
    function getSubMenuItems( oMenuItem )
    {
        var aReturnArray    = new Array();

        for ( var i = 0; i < aSubMenuItems.length; i++ )
        {
            if ( aSubMenuItems[i][2] == getMenuText(oMenuItem) )
                aReturnArray[aReturnArray.length]   = i;
        }

        return aReturnArray;
    }
    function isMainMenuItem( sMenuItem )
    {
        sMenuItem           = getMenuText( sMenuItem );
        var bMainMenuItem   = false;

        for ( var i = 0; i < aMainMenuItems.length; i++ )
        {
            if ( aMainMenuItems[i][0] == sMenuItem )
                bMainMenuItem   = true;
        }

        return bMainMenuItem;
    }
    function getMenuText( oSource )
    {
        var sMenuText   = oSource;

        if ( typeof sMenuText != 'string' )
        {
            //  The two possibilities are either that the text is directly inside a TD object, or buried within a NOBR tag within that object
            if ( oSource.childNodes[0].tagName == 'NOBR' )
            {
                sMenuText   = oSource.childNodes[0].childNodes[0].nodeValue;
            }
            else
            {
                sMenuText   = oSource.childNodes[0].nodeValue;
            }

            if ( typeof sMenuText != 'string' )
            {
                //  Something has obviously gone wrong, so just return an empty string
                sMenuText   = '';
            }
        }

        return sMenuText
    }
    function getParentMenuItemFromId( sMenuItem )
    {
        sMenuItem   = getMenuText( sMenuItem );
        var sParent = '';

        for ( i = 0; i < aSubMenuItems.length; i++ )
        {
            if ( aSubMenuItems[i][0] == sMenuItem )
                sParent = aSubMenuItems[i][2];
        }

        return sParent;
    }
    function getMenuLevel( oMenuItem )
    {
        var iMenuLevel  = 1;
        var sMenuItem   = getMenuText( oMenuItem );

        while ( !isMainMenuItem(sMenuItem) )
        {
            sMenuItem   = getParentMenuItemFromId( sMenuItem );
            iMenuLevel++;
        }

        return iMenuLevel;
    }
    function getLink( oMenuItem )
    {
        var sLinkURL    = '';
        var sMenuItem   = getMenuText( oMenuItem );

        //  Check main menu items first
        for ( var i = 0; i < aMainMenuItems.length; i++ )
        {
            if ( aMainMenuItems[i][0] == sMenuItem )
                sLinkURL    = aMainMenuItems[i][1];
        }

        //  Otherwise check submenu items
        if ( sLinkURL == '' )
        {
            for ( var i = 0; i < aSubMenuItems.length; i++ )
            {
                if ( aSubMenuItems[i][0] == sMenuItem )
                    sLinkURL    = aSubMenuItems[i][1];
            }
        }

        return sLinkURL;
    }