$j(document).ready(function(){
    
    // collapse all sections when page first loads
    $j('.accordion-section h2').addClass('collapsed');
    $j('.accordion-section h2').siblings('div').animate({height: 'toggle'}, 0);
    
    // expand section if its anchor is called in the url
    if(window.location.hash) {
        myAnchor = window.location.hash;
        $j('.accordion-section h2').filter(myAnchor).toggleClass('expanded').toggleClass('collapsed');
        $j('.accordion-section h2').filter(myAnchor).siblings('div').animate({height: 'toggle'}, 0);
    }
    
    // listen for click event to expand/collapse individual section
    animationInProgress = false;
    $j('.accordion-section h2 a').click(function(event){
        event.preventDefault();
        if( !animationInProgress ) {
            animationInProgress = true;
            $j(this).parent('h2').toggleClass('expanded').toggleClass('collapsed');
            $j(this).parent('h2').siblings('div').animate({height: 'toggle'}, 100, '', function(){ animationInProgress = false; });
        }
    });
    
    // listen for click event to expand all
    $j('.accordion-section a.expand-all').click(function(event){
        event.preventDefault();
        if( !animationInProgress ) {
            animationInProgress = true;
            $j('.accordion-section h2').each(function(){
                if($j(this).hasClass('collapsed')) {
                    $j(this).toggleClass('expanded').toggleClass('collapsed');
                    $j(this).siblings('div').animate({height: 'toggle'}, 100);
                }
            });
            animationInProgress = false;
        }
    });
    
    // listen for click event to collapse all
    $j('.accordion-section a.collapse-all').click(function(event){
        event.preventDefault();
        if( !animationInProgress ) {
            animationInProgress = true;
            $j('.accordion-section h2').each(function(){
                if($j(this).hasClass('expanded')) {
                    $j(this).toggleClass('expanded').toggleClass('collapsed');
                    $j(this).siblings('div').animate({height: 'toggle'}, 100);
                }
            });
            animationInProgress = false;
        }
    });
    
});
