

jQuery( function() {
    
    /*
     * this handles the expand/collapse feature for the "left navigation"
     * on the "program offices" level... we couldn't use the jQuery.accordion()
     * plugin because the list items are all on the same level, not parent/children
     * 
    */
    var leftNavGroups = [];
    
    if ( _REDDOT_SMARTEDIT_ == false ) {
        
        var lastParent = '';
        
        $.each( $('#leftNav li'), function() {
            // this builds the parent/child relationship that doesn't exist in the DOM
            if ( $(this).hasClass('leftNavHeader') ) {
                leftNavGroups.push({
                    title:      $(this).text().trim(), // a little unsanitary, but it'll do
                    url:        $( $(this).find('a')[0] ).attr('href').trim(),
                    element:    $(this),
                    children:   []
                });
                //$(this).show();
                lastParent = $(this).text().trim();
            }
            else if ( $(this).hasClass('leftNavChild') ) {
                // add to the most recently found parent
                leftNavGroups[leftNavGroups.length - 1].children.push({
                    title:      $(this).text().trim(),
                    url:        $($(this).find('a')[0]).attr('href').trim(),
                    element:    $(this)
                });
                // wait a little bit before showing the user it's a collapsed menu
                // you can't do $.animate({}) with no parameters - it's a reported jQuery bug
                //$(this).animate({opacity: '+=0'}, 2000).slideUp( 800 );
                /* barry now says dont do this because it gives him annurisms */
                if ( document.title != lastParent ) {
                    $(this).slideUp(400);
                }
            }
            
            
        }).find('a').click( function(e) {
            // don't follow the actual link
            e.preventDefault();
            e.stopPropagation();
            //if ( !$(this).parent().hasClass('leftNavHeader') ) {
                window.location = $(this).attr('href');
            /*}
            else {
                // accordion-like functionality
                for ( var i = 0; i < leftNavGroups.length; i++ ) {
                    if ( leftNavGroups[i].title != $(this).text() ) {
                        //console.log('hiding element: ' + leftNavGroups[i].title);
                        for ( var j = 0; j < leftNavGroups[i].children.length; j++ ) {
                            $(leftNavGroups[i].children[j].element).slideUp(400);
                        }
                    }
                    else {
                        //console.log('SHOWing element: ' + leftNavGroups[i].title);
                        for ( k = 0; k < leftNavGroups[i].children.length; k ++ ) {
                            $(leftNavGroups[i].children[k].element).slideDown(400);
                        }
                    }
                }
            }*/
        }); // end the $.each leftNav loop
        
        /*
            
            EXAMPLE OF SINGLETON SCHEMA
            leftNavGroups[1] = {
                title:      'Communications By Type",
                url:        'comm_by_type.htm',
                children:   [
                    (0): {
                        title:  'Bulletin Inserts',
                        url:    '#'
                    }
                    (1): {
                        title:  'Prayers',
                        url:    '#'
                    }
                    (2): {
                        title:  'Parish Resources',
                        url:    '#'
                    }
                ]
            }
            
            WORKING TEST CASE
            for ( var i = 0; i < leftNavGroups.length; i++ ) {
                console.log( leftNavGroups[i].title);
                console.log( leftNavGroups[i].children.length );
                for ( var child in leftNavGroups[i].children ) {
                    console.log("\t" + leftNavGroups[i].children[child].title);
                }
            }
            
        */
        
    } // end RedDot CMS check
    
}); // end document ready wrapper