﻿var menuTimeOut = null;
var hideOnWindowClick = null;

Event.observe(document, 'click', OnWindowClick);

function Highlight(event, control)
{
    if(control.className != 'ArchiveHeaderTab Selected' && control.className != 'ArchiveHeaderTab Selected1')
    {
        if(control.oldClassName == null)
        {
            if(event.type == 'mouseover')
            {            
                control.oldClassName = control.className;
                control.className = control.className + ' Selected2';            
            }
        }
        else
        {    
            control.className = control.oldClassName;
            control.oldClassName = null;
        }
    }
}

function HideDropDown(event, control, id)
{
    var menu = $(id);
    
    if(menu != null)
    {
        menu.toggleArrow = control;
        
        var parentTab = menu.parentNode;
        
        clearTimeout(menuTimeOut);
        
        hideOnWindowClick = null;
        
        if(menu.isVisible == null || !menu.isVisible)
        {
            hideOnWindowClick = null;
            
            Effect.BlindDown(menu, { duration: .15, afterFinish: function() { hideOnWindowClick = HideOnWindowClick.bindAsEventListener(event, control, id, menu, parentTab); } });
            
            menu.isVisible = true;
            menu.observe('mouseover', OnMenuMouseOver.bindAsEventListener(event, menu, control));
            menu.observe('mouseout', OnMenuMouseOver.bindAsEventListener(event, menu, control));
            
            menuTimeOut = setTimeout(   
                function() {
                    HideDropDown(null, control, id); 
                 
                    if(parentTab.className != 'ArchiveHeaderTab Selected')
                    {
                        parentTab.oldClassName = null; 
                        parentTab.className = 'ArchiveHeaderTab'; 
                    }                    
                }, 4000);
            
            if(parentTab.className != 'ArchiveHeaderTab Selected')
            {
                parentTab.className = 'ArchiveHeaderTab Selected1';
            }
        }
        else
        {
            Effect.BlindUp(menu, { duration: .15 });
            
            menu.isVisible = false;

            if(parentTab.className != 'ArchiveHeaderTab Selected')
            {        
                parentTab.className = 'ArchiveHeaderTab Selected2';    
            }
        }
        
        if(control.oldClassName == null)
        {
            control.oldClassName = control.className;
            control.className = control.className + ' Selected';
        }
        else
        {
            control.className = control.oldClassName;
            control.oldClassName = null;
        }
        
        var allDropDowns = $$('.MenuDropDown');

        for (var i = 0; i < allDropDowns.length; i++)
        {
            var aDropDown = allDropDowns[i];

            if (aDropDown != menu && aDropDown.isVisible)
            {   
                Effect.BlindUp(aDropDown, { duration: .1 });
                
                aDropDown.isVisible = false;          
                
                var toggleControl = aDropDown.toggleArrow;
                var aParentTab = $(aDropDown).parentNode;
            
                if(aParentTab.className != 'ArchiveHeaderTab Selected')
                {        
                    aParentTab.className = 'ArchiveHeaderTab';  
                    aParentTab.oldClassName = null;  
                }
            
                if(toggleControl != null)
                {
                    if(toggleControl.oldClassName == null)
                    {
                        toggleControl.oldClassName = toggleControl.className;
                        toggleControl.className = toggleControl.className + ' Selected';
                    }
                    else
                    {
                        toggleControl.className = toggleControl.oldClassName;
                        toggleControl.oldClassName = null;
                    }
                }
            }
        }
    }  
}

function HideOnWindowClick(event, control, id, menu, parentTab)
{
    HideDropDown(event, control, id);
    
    if(parentTab.className != 'ArchiveHeaderTab Selected')
    {
        parentTab.oldClassName = null; 
        parentTab.className = 'ArchiveHeaderTab'; 
    }  
}

function OnWindowClick(event)
{
    if(hideOnWindowClick != null)
    {
        hideOnWindowClick();
        
        hideOnWindowClick = null;
    }
}

function OnMenuMouseOver(event, menu, toggle)
{
    clearTimeout(menuTimeOut);
    
    if(event.type == 'mouseout')
    {
        menuTimeOut = setTimeout(
            function() { 
                HideDropDown(null, toggle, menu.id); 
                
                var parentTab = menu.parentNode;
                
                if(parentTab.className != 'ArchiveHeaderTab Selected')
                {
                    parentTab.className = 'ArchiveHeaderTab';
                    parentTab.oldClassName = null;
                }
            }, 4000);
    }
}