﻿
function initDropdownMenu () {
    $('li.main-link').hover(
	    function () {$('div', this).show();},
	    function () {$('div', this).hide();}
    );
}

function showTray(panelid) {
    var trayState = $('#slide-tray').css('display');
    if (trayState == 'block') {
        //tray is already open, if the panel open is the panel asked for, just close the tray
        //else close the tray before opening the other panel
        var panelOpen = $('#' + panelid).hasClass('on');

        if (panelOpen) {
            //close the tray
            toggleTray(function () { hideTrayPanel(panelid); });
        }
        else {
            //close the tray and open new panel
            toggleTray(function () { toggleTrayPanel(panelid); });
        }
    }
    else {
        //tray is closed, show the panel asked for and open the tray
        showTrayPanel(panelid);
        toggleTray();
    }
}
function toggleTrayPanel(panelid) {
    //close the open panel
    $('.slide-tray-panel, .on').css('display', 'none');
    $('.slide-tray-panel, .on').removeClass('on');
    showTrayPanel(panelid);
    toggleTray(); //open the tray
}
function toggleTray(callback) {
    $('#slide-tray').animate({ height: 'toggle' }, 1000, callback);
}
function hideTrayPanel(panelid) {
    $('#' + panelid).css('display', 'none');
    $('#' + panelid).removeClass('on');
}
function showTrayPanel(panelid) {
    $('#' + panelid).css('display', 'block');
    $('#' + panelid).addClass('on');
    setFocus(panelid);
}

function clearForm(formid) {
    $('#' + formid).find(':input').each(function () {
        switch (this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}

function setFocus(panelid) {
    // Delayed to let tray finish sliding
    var action = "$(\"input[type=text]\", \"#" + panelid + "\").focus()";
    setTimeout(action, 1100);       
}
