﻿var speedPattern = new Array(5, 6, 9, 12, 14, 18, 25);
var currentMenuPanel = null;

function SlideExpand(panel, action)
{
    if (panel["slideExpanding"] == null)
        panel["slideExpanding"] = false;

    if (panel["originalHeight"] == null)
    {
        panel["originalHeight"] = panel.clientHeight;
        panel.style.height = 0;
    }

    if (panel["originalHeight"] > 30)
    {
        if (currentMenuPanel != null && currentMenuPanel != panel)
            CollapsePanelImmediately(currentMenuPanel);

        currentMenuPanel = panel;

        if (panel["closeInterval"] == null)
            panel["closeInterval"] = setInterval(function() { ClosePanel(panel); }, 10);

        if ((panel["slideExpanding"] && !action) || (!panel["slideExpanding"] && action))
        {
            panel["slideExpanding"] = true;

            var originalHeight = parseInt(panel["originalHeight"]);
            var currentHeight = panel.clientHeight;
            var currentSpeed = speedPattern[parseInt((currentHeight / originalHeight) * speedPattern.length)];

            if (currentHeight < originalHeight)
            {
                panel.style.visibility = 'visible';
                currentHeight += 10;
                panel.style.height = currentHeight + 'px';
                setTimeout(function() { SlideExpand(panel, false); }, currentSpeed);
            }
            else
                panel["slideExpanding"] = false;
        }
    }
}

function SlideCollapse(panel, action)
{
    if (!panel["hover"])
    {
        if (panel["slideCollapsing"] == null)
            panel["slideCollapsing"] = false;

        panel["slideExpanding"] = false;

        if ((panel["slideCollapsing"] && !action) || (!panel["slideCollapsing"] && action))
        {
            panel["slideCollapsing"] = true;

            var originalHeight = parseInt(panel["originalHeight"]);
            var currentHeight = panel.clientHeight;
            var currentSpeed = speedPattern[speedPattern.length - parseInt((currentHeight / originalHeight) * speedPattern.length)];

            if (currentHeight > 10)
            {
                currentHeight -= 10;
                panel.style.height = currentHeight + 'px';
                setTimeout(function() { SlideCollapse(panel, false); }, currentSpeed);
            }
            else
                CollapsePanelImmediately(panel);
        }
    }
}

function CollapsePanelImmediately(panel)
{
    panel["slideCollapsing"] = false;
    panel.style.height = 1;
    panel.style.visibility = 'hidden';
    clearInterval(panel["closeInterval"]);
    panel["closeInterval"] = null;
}

function ClosePanel(panel)
{
    if (!panel["hover"])
        SlideCollapse(panel, false);
}