﻿// Faq.js
// ------
// Handles expanding / collapsing of FAQ questions. 

// FUNCTIONS
        
// Collapses all FAQ questions.
function CollapseAll() {

    // For each answer...
    $( '.faq-answer' ).each( function() {
    
        // Collapse the answer
        $( this ).slideUp( 200 );
        
        // Switch icon to +
        SetIcon( this, '+' );
        
    } );
}



// Expands all FAQ questions.
function ExpandAll() {

    // For each answer...
    $( '.faq-answer' ).each( function() {
    
        // Expand the answer
        $( this ).slideDown( 200 );
        
         // Switch icon to -
        SetIcon( this, '&ndash;' );
    } );
}



// Returns a click handler that toggles the given answer element.
function GetQuestionClickHandler( answerElement ) {
    return function() {
        
        // Toggle answer visibility
        $( answerElement ).slideToggle( 200 );
        
        // Toggle icon
        SetIcon( answerElement, null );
    }
} // end GetQuestionClickHandler()



// Sets the 'icon' (first character) of the given answer's question
// to the given value. If icon is null, it is toggled.
function SetIcon( answerElement, icon ) {

    // Get the question
    var question = $( answerElement ).prev( '.faq-question' )[ 0 ];
    var text = $( question ).html();
    
    // Use provided icon - if none given, determine toggle character
    var newIcon;
    if ( icon != null ) {
        newIcon = icon;
    }
    else {
        if ( text.charAt( 0 ) == '+' ) {
            newIcon = '&ndash;';
        }
        else {
            newIcon = '+';
        }
    }
    
    // Change the icon
    text = text.slice( 1 );
    text = newIcon + text;
    $( question ).html( text );

} // end SetIcon()



// MAIN SCRIPT

// When the document is ready...
$( document ).ready( function() {

    // For each question...
    $( '.faq-question' ).each( function() {
    
        // Find the associated FAQ answer
        var question = this;
        var answer = $( this ).next( '.faq-answer' )[ 0 ];
    
        // Assign click-toggle handler
        $( question ).click( GetQuestionClickHandler( answer ) );
        
        // Hide answer by default and add a plus-sign
        $( answer ).hide();
        $( question ).prepend( '+ ' );
        
    } ); // next question
    
} ); // end when document ready