/*
 * checkHideMenu:
 *      this function is called via the MouseCoords mouse move handlers (see implementation
 *      in Common.js).  It is called to determine whether the active sub menu should be
 *      hidden
 * Params:
 *      evnt - a reference to the mouse move event
 * Returns:
 *      true
 */
function checkHideMenu(evnt)
{
    if ( ActiveSubMenu )
    {
        var main_menu = document.getElementById(CTL_MAIN_MENU);
        
        if ( main_menu )
        {
            var main_menu_pos = getControlPosition(main_menu);
            var sub_menu_pos  = getControlPosition(ActiveSubMenu);
            
            if ( ((MouseCoords.X < main_menu_pos.x) && (MouseCoords.X < sub_menu_pos.x)) ||
                 ((MouseCoords.X > (main_menu_pos.x + main_menu.clientWidth)) && (MouseCoords.X > (sub_menu_pos.x + ActiveSubMenu.clientWidth))) ||
                 (MouseCoords.Y < main_menu_pos.y) ||
                 (MouseCoords.Y > (sub_menu_pos.y + ActiveSubMenu.clientHeight)) )
            {
                ActiveSubMenu.className = CSS_HIDDEN;
                ActiveSubMenu = null;
            }
            else if ( (MouseCoords.Y > (main_menu_pos.y + main_menu.clientHeight)) &&
                      ((MouseCoords.X < sub_menu_pos.x) || (MouseCoords.X > (sub_menu_pos.x + ActiveSubMenu.clientWidth))) )
            {
                ActiveSubMenu.className = CSS_HIDDEN;
                ActiveSubMenu = null;
            }
        }
    }
    
    return true;
}

/*
 * onImgClick:
 *      called to navigate to a different category as part of a menu click operation
 * Params:
 *      page - the page to navigate to
 *      level - the category level (parent or child)
 *      categoryId - the category ID being navigated to
 * Returns:
 *      true
 */
function onImgClick(page, level, categoryId) {
    var url = "/" + page + "?level=" + level + "&categoryId=" + categoryId;
    window.location=url;
    return true;
}

/*
 * onImgOut:
 *      called on a mouse out event for the top level categories
 *      in the page menu
 * Params:
 *      img - a reference to the img object that is firing the event
 *      origUrl - the non-selected image to set the img object to
 * Returns:
 *      nothing
 */
function onImgOut(img, origUrl)
{
    if ( (img) && (origUrl) )
    {
        img.src = origUrl;
    }
}

/*
 * onImgOut:
 *      called on a mouse over event for the top level categories
 *      in the page menu
 * Params:
 *      img - a reference to the img object that is firing the event
 *      altUrl - the highlight image to set the img object to
 * Returns:
 *      nothing
 */
function onImgOver(img, altUrl, parentId)
{
    if ( (img) && (altUrl) )
    {
        img.src = altUrl;
    }
    
    if ( parentId )
    {
        var parent_div = document.getElementById(PRE_CAT + parentId);
        var submenu = document.getElementById(PRE_SUB + parentId);
        
        if ( (parent_div) && (submenu) )
        {
            if ( (ActiveSubMenu) && (ActiveSubMenu != submenu) )
            {
                ActiveSubMenu.className = CSS_HIDDEN;
            }

            onMenuOver(parent_div, submenu);
        }
    }
}

/*
 * onMenuOver:
 *      called when a top-level menu item is moused over.  This method is responsible
 *      for displaying the appropriate sub-menu options for the parent
 * Params:
 *      parentDiv - a reference to the top-level menu item
 *      submenu - a reference to the div containing the sub menu items
 * Returns:
 *      true
 */
function onMenuOver(parentDiv, submenu)
{
    // getControlPostion is defined in Common.js
    var pos = getControlPosition(parentDiv);
    
    if ( ActiveSubMenu )
    {
        if ( (ActiveParent) && (ActiveParent != parentDiv) && (submenu) && (submenu != ActiveSubMenu) )
        {
            ActiveSubMenu.className = CSS_HIDDEN;
        }
    }
    
    ActiveParent  = parentDiv;
    ActiveSubMenu = submenu;
    
    submenu.className  = CSS_SUBMENU;
    submenu.style.top  = (pos.y + parentDiv.clientHeight) + STR_PX;
    submenu.style.left = pos.x + STR_PX;
    
    var client_width = (submenu.style && submenu.style.width) ? submenu.style.width : submenu.clientWidth;
    
    if ( client_width < parentDiv.clientWidth )
    {
        submenu.style.width = parentDiv.clientWidth + STR_PX;
    }
    
    // this next block is to get around an oddity with IE browsers.
    // With certain sub-menus, for some reason, the text wraps across lines instead
    // of showing up on a single line.  Although I'm sure this is one of the finer
    // points of CSS that I'm not missing, looping through the items in the list and
    // setting their backgroundImage property hides this problem.  The issue
    // doesn't appear in Mozilla based browsers
    
    // only need to execute this once - the firstPass attribute will be non-existent
    // if we've never opened the sub-menu
    if ( !submenu.firstPass) {
        if ( submenu.childNodes ) {
            // the first child is link - it's the first child of the link that
            // we need to target
            for (var outer=0; outer < submenu.childNodes.length; outer++) {
                var link_item = submenu.childNodes[outer];
                
                if ( (link_item) && (link_item.childNodes) )
                {
                    // loop through the child nodes until we find an item with a
                    // style of "subMenuItem" - it's this node we want
                    for (var inner=0; inner<link_item.childNodes.length; inner++) {
                        if ( (link_item.childNodes[inner].className) && (CSS_SUBMENU_ITEM == link_item.childNodes[inner].className) ) {
                            link_item.childNodes[inner].style.backgroundImage = SUB_MENU_BG_SEL;
                            link_item.childNodes[inner].style.backgroundImage = SUB_MENU_BG_NO_SEL;
                            break;
                        }
                    }
                }
            }
            
            // set our flag so that we never have to execute this block of
            // code more than once per sub-menu
            submenu.firstPass = true;
        }
    }
    
    return true;
}

/*
 * onSubMenuOut:
 *      called when a mouseout event occurrs on a sub-menu item.  This
 *      method changes the background color of the item back to its
 *      default color
 * Params:
 *      obj - a reference to the object that has lost the mouse focus
 * Returns:
 *      true
 */
function onSubMenuOut(obj)
{
    if (obj)
    {
        obj.style.backgroundImage = SUB_MENU_BG_NO_SEL;
    }
    
    return true;
}

/*
 * onSubMenuOver:
 *      called when a mouseover event occurrs on a sub-menu item.  This
 *      method changes the background color of the item
 * Params:
 *      obj - a reference to the object that has gained the mouse focus
 * Returns:
 *      true
 */
function onSubMenuOver(obj)
{
    if (obj)
    {
        obj.style.backgroundImage = SUB_MENU_BG_SEL;
    }
    
    return true;
}


var CSS_HIDDEN         = "hidden";
var CSS_SUBMENU        = "subMenu";
var CSS_SUBMENU_ITEM   = "subMenuItem";
var CTL_MAIN_MENU      = "mainMenu";
var SUB_MENU_BG_NO_SEL = "url(/images/ToolbarGradient.jpg)";
var SUB_MENU_BG_SEL    = "url(/images/ToolbarGradientSel.jpg)";
var PRE_CAT            = "cat";
var PRE_SUB            = "sub_";

var ActiveParent  = null;
var ActiveSubMenu = null;

// MouseCoords is defined in Common.js
MouseCoords.addMouseMoveHandler(checkHideMenu);