﻿// This handles the hiding and showing of expanded panels

// Variables
var hideExpandedPanelTimer = 0;
var hideExpandedPanelTimeout = 500;
var blockHideExpandedPanelTimer;

var isWithinExpandedPanel = false;

// Collection of expandedPanels.
// Used when an showing an expandedPanel, to close other expandedPanels
var expandedPanelRegistry = [];

function registerExpandedPanel(expandablePanelSelector) {   
    expandedPanelRegistry[expandedPanelRegistry.length] = expandablePanelSelector;
}

function showExpandedPanel(expandedPanelSelector) {

    // Loops through the collection of expandedPanels, slides down the expandedPanel with the matching ID
    // Hides the rest of the expandedPanels

    for (var index = 0; index < expandedPanelRegistry.length; index++) {

        var currentSelector = expandedPanelRegistry[index];

        if (currentSelector == expandedPanelSelector) {
            $(currentSelector).slideDown("fast");
        }
        else {
            hideExpandedPanel(currentSelector);
        }
    }
}

function hideExpandedPanel(expandedPanelSelector) {
    $(expandedPanelSelector).hide();
}

function clearHideExpandedPanelTimer() {
    window.clearTimeout(hideExpandedPanelTimer);
}

// "Drop Down List" with Expanded List attached
function bindDropDownExpandedList(dropDownListID, expandedPanelID) {

    registerExpandedPanel(expandedPanelID);

    $(expandedPanelID).hover(
        function () { clearHideExpandedPanelTimer(); },
        function () { dropDown_StartTimerToHideExpandedPanel(expandedPanelID); }
    );

    $(dropDownListID + " li.closed").click(function () {

        if ($(expandedPanelID).css("display") == "block")
            hideExpandedPanel(expandedPanelID);
        else
            showExpandedPanel(expandedPanelID);
    });

    $(dropDownListID + " li.closed").hover(function () { clearHideExpandedPanelTimer(); });
}

function dropDown_StartTimerToHideExpandedPanel(expandedPanelID) {
    hideExpandedPanelTimer = window.setTimeout(function () { hideExpandedPanel(expandedPanelID) }, hideExpandedPanelTimeout);
}

// TextBox with Expanded List attached
function bindTextBoxExpandedList(textBoxID, expandedPanelID) {

    registerExpandedPanel(expandedPanelID);

    $(expandedPanelID).hover(
        function () {
            isWithinExpandedPanel = true;
            clearHideExpandedPanelTimer();
        },
        function () {
            isWithinExpandedPanel = false;
            textBox_StartTimerToHideExpandedPanel(expandedPanelID);
        }
    );

    $(textBoxID).keydown(function () {
        textBox_ShowExpandedPanel(expandedPanelID)
    });

    $(textBoxID).focusin(function () {
        textBox_ShowExpandedPanel(expandedPanelID)
    });

    $(textBoxID).focusout(function () {
        blockHideExpandedPanelTimer = false;

        if (!isWithinExpandedPanel)
            textBox_StartTimerToHideExpandedPanel(expandedPanelID);
    });

}

function textBox_ShowExpandedPanel(expandedPanelID) {

    clearHideExpandedPanelTimer();
    blockHideExpandedPanelTimer = true;

    if ($(expandedPanelID).css("display") != "block")       
        showExpandedPanel(expandedPanelID);
}

function textBox_StartTimerToHideExpandedPanel(expandedPanelID) {

    if (!blockHideExpandedPanelTimer)
        hideExpandedPanelTimer = window.setTimeout(function () { hideExpandedPanel(expandedPanelID) }, hideExpandedPanelTimeout);
}

