/*
 * Handles language selection and animations for the AACCWD song site.
 * Requires jQuery v1.4.2.
 *
 * Coded by Stephen Uy <stephenu@alumni.uci.edu>
 */

$( document ).ready( function() {
    // Animations should complete in this time (in ms).
    var animationTime = 250;

    // Show help text, since we're using Javascript.
    $( "#helptext" ).html( "<h5>Click on an event's name to show its songs; the cursor will change to show that an event can be clicked.  Click on a song's name to show its list of sheet music and audio files.  The sheet music and audio files can be downloaded by clicking on the appropriate icons.</h5>" );

    // By default, all song and variation lists should be hidden.
    $( ".songlist" ).hide();
    $( ".variationlist" ).hide();

    // Mark all expandable nodes as such.
    $( ".hasChildren" )
        .css( "list-style-image", "url( images/cross.gif )" );

    // Change pointer for nodes with children.
    $( ".hasChildren" )
        .children( ".feastname" )
        .css( "cursor", "pointer" );
    $( ".songChild" )
        .css( "cursor", "pointer" );
    $( ".hasLink" )
        .css( "cursor", "pointer" );

    // Clicking on a feast's name should show its songlist.
    $( ".feastname" ).click( function() {
        var theParent = $( this ).parent();

        // Highlight selected feasts
        if( theParent.css( "background-color" ) != "transparent" ) {
            theParent.css( "background-color", "transparent" );
        } else {
            // Background is #fff9ea
            theParent.css( "background-color", "#cfc9ba" );
        }

        toggleList( $( this ), ".songlist" );

        // Collapse all this feast's children
        $( this ).siblings( ".songlist" )
            .children( ".hasChildren" ).each( function() {
                $( this ).css(
                    "list-style-image",
                    "url( images/cross.gif )" );
                $( this ).children( ".variationlist" ).hide();
            } );

        // Collapse all siblings
        theParent.siblings().each( function() {
            // Re-transparent-ify the item
            $( this ).css( "background-color", "transparent" );

            // Extra work for siblings with songs
            if( $( this ).children( ".songlist" ).size() > 0 ) {
                // Make the icon into a cross
                $( this ).css(
                    "list-style-image",
                    "url( images/cross.gif )" );

                // Collapse all this sibling's children
                $( this ).children( ".songlist" )
                    .children( ".hasChildren" ).each( function() {
                        $( this ).css(
                            "list-style-image",
                            "url( images/cross.gif )" );
                        $( this ).children( ".variationlist" ).hide();
                    } );
                $( this ).children( ".songlist" ).hide();
            }
        } );
    } );

    // Clicking on a song's name should show its variations.
    $( ".songChild" ).click( function() {
        toggleList( $( this ), ".variationlist" );
    } );

    // Toggles a list's visibility.
    // Note: we say "theParent" because "parent" is a JS keyword.
    function toggleList( element, childClass ) {
        // Ignore empty lists.
        if( !element.parent().has( "ul" ).length ) {
            return;
        }

        // We have a non-empty list, so get our parent.
        var theParent = element.parent();

        // TODO: Figure out why this is inverted!
        theParent.css( "list-style-image",
            (theParent.children().is( ":hidden" )) ?
                "url( images/minus.gif )" :
                "url( images/cross.gif )" );

        theParent.children( childClass )
            .slideToggle( animationTime );
    };
} );

// vim: ai ts=4 sts=4 et sw=4 tw=79


