// Common CVM 2008 theme Javascript
// Based on jQuery

// Initialize
jQuery(document).ready(
    function()
    {
        // Find all content palettes that need to be hidden
        //jQuery(".paletteContent:not(.initVis)").hide();
        jQuery(".paletteContent:not(.initVis)").css('position','absolute');
        jQuery(".paletteContent:not(.initVis)").css('left','-3000px');

        // Show sub nav on global nav hover
        jQuery('.gnavTopLevel').hover(
            function(){
                jQuery(this).children('ol.globalNavSecondary').show();
            },
            function(){
                jQuery(this).children('ol.globalNavSecondary').hide();
            }
        );
    }
); // End initialization

// Manage palette swapping

function swapPalette(rootElement,hideClass,showId,showControl)
{

    // hide all canvas elements in the group
    var hideElements = jQuery('#' + rootElement + ' .' + hideClass);
    hideElements.css('position','absolute');
    hideElements.css('left','-3000px');

    // Show the selected active canvas
    jQuery('#' + showId).css('position','relative');
    jQuery('#' + showId).css('left','0px');

    // Get affected control links
    controlsToDim = jQuery('#' + rootElement + ' .paletteControlLink');
    // Remove selected class
    controlsToDim.removeClass('selected');
    // Light up the hovered link
    if ( !jQuery(showControl).hasClass('selected') )
    {
        jQuery(showControl).addClass('selected')
    }

    // Return false to avoid activating link
    return false;

}

/* Directory */

function updateLiveDirectory()
{
    // Set minimum number of characters to start searching
    var minSearch = 2;

    // Check for adequate number of characters in search box
    if ( document.liveDirectoryForm.liveDirectoryBox.value.length >= minSearch )
    {
        // Gather together post variables
        var liveDirectoryFn = (jQuery('#liveDirectoryIncFirstName').is(':checked')) ? 1 : 0;
        var liveDirectoryLn = (jQuery('#liveDirectoryIncLastName').is(':checked')) ? 1 : 0;
        var searchString = jQuery('#liveDirectoryBox').val();
        // Require at least a search on first or last name
        if (!liveDirectoryFn && !liveDirectoryLn )
        {
            jQuery('#liveDirectoryListWrapper').html('<p>You need to search on first name, last name, or both.</p>');
        }
        // Assemble post string
        var liveDirectoryPost = 'a=ld&fn='+liveDirectoryFn+'&ln='+liveDirectoryLn+'&s='+searchString;

        // Do AJAX business
        jQuery.post(
            '3775',
            liveDirectoryPost,
            function(data,textStatus){
                if ('success' == textStatus)
                {
                    jQuery('#liveDirectoryListWrapper').html(data);
                }
                else
                {
                    jQuery('#liveDirectoryListWrapper').html('<p>Directory error.</p>');
                }
            },
            'html'
        );
    }
    else
    {
        jQuery('#liveDirectoryListWrapper').html('<p>Search requires at least two characters.</p>');
    }
}