var toggleDurationSubMenu = 0.3;
var toggleDurationHighlight = 0.2;
var toggleDurationDropdownMenu = 0.2;
var lastOpenedDropdownItem = null;
var lastSelectedSubmenuItem = null;
var toggleAction = 'slide';
var subMenuActiveImage = 'url(/_images/layout/master/productmenu/submenu-active.png)';
var standardHighlightImage = 'url(/_images/layout/master/productmenu/standard-highlight.jpg)';
var highlightId = 'Highlight';
var subMenuId = 'SubMenu';

// Set visibility of sub menu
function ToggleSubMenu() {
    // Init controls
    var subMenu = document.getElementById(subMenuId);
    var highlight = document.getElementById(highlightId);

    // Open/close sub/highlight and dropdown menu
    Effect.toggle(subMenu, toggleAction, { duration: toggleDurationSubMenu });
    Effect.toggle(highlight, toggleAction, { duration: toggleDurationHighlight });

    // Reset hover image of last selected image
    if (CheckSubMenuStatus(subMenu) == 'none')
        ResetLastSelectedSubmenuItem(highlight);

    if (lastOpenedDropdownItem != null) {
        // Hide last open dropdownmenu item if submenu is hidden
        Effect.toggle(lastOpenedDropdownItem, toggleAction, { duration: toggleDurationSubMenu });
        // Reset last open menu item
        lastOpenedDropdownItem = null;
    }
	
    return false;
}

// Reset highlighted submenu item
function ResetLastSelectedSubmenuItem(highlight) {
    if (lastSelectedSubmenuItem != null)
        lastSelectedSubmenuItem.style.backgroundImage = '';

    // Reset highlight image
    if ((lastOpenedDropdownItem == null) && (highlight != null)) {
        highlight.style.backgroundImage = standardHighlightImage;
        highlight.style.backgroundRepeat = 'no-repeat';
    }
}

// Set visibility of dropdown menu
function ToggleDropdownMenu(dropdownMenu, img, category) {
    var highlight = document.getElementById(highlightId);

    dropdownMenu = document.getElementById(dropdownMenu);
    // First hide last open dropdownmenu item
    if (lastOpenedDropdownItem != null)
        Effect.toggle(lastOpenedDropdownItem, toggleAction, { duration: toggleDurationDropdownMenu });

    // Reset highlighted submenu item
    ResetLastSelectedSubmenuItem();

    if (lastOpenedDropdownItem != dropdownMenu) {
        // Show clicked menu only if different then last open menu item
        // Show dropdown menu
        Effect.toggle(dropdownMenu, toggleAction, { duration: toggleDurationDropdownMenu });
        lastOpenedDropdownItem = dropdownMenu;
        // Keep hover image of selected sub menu item
        img.style.backgroundImage = subMenuActiveImage;
        // Set highlight to empty (white) background
        highlight.style.backgroundImage = '';
        highlight.className = 'highlightEmpty';
    }
    else {
        // Otherwise reset last open menu item
        lastOpenedDropdownItem = null;
        // Reset highlight
        highlight.style.backgroundImage = standardHighlightImage;
        highlight.style.backgroundRepeat = 'no-repeat';
        // Reset hover image of selected sub menu item
        img.style.backgroundImage = '';
   }

    lastSelectedSubmenuItem = img;

    return false;
}

// Return visibility status of sub menu
function CheckSubMenuStatus(subMenu) {
    var subMenuStatus = subMenu.style.display;

    if (subMenuStatus == '')
        subMenuStatus = 'none';

    return subMenuStatus;
}
