﻿// photo_rotation.js
// -----------------
// Handles the rotating photo slideshow on the homepage.


// Hide private members...
( function( $ ) {

    // PRIVATE CONSTANTS & GLOBALS
            
    var FADE_TIME = 2000;
    var SLIDE_INTERVAL = 5000;
    var INITIAL_INTERVAL = 1000;
    var m_PhotoIndex = 0;
    var m_Photos;



    // PRIVATE FUNCTIONS

    // Fades in the next photo in the slideshow.
    function NextPhoto() {
    
        // If there's only one photo, exit
        if ( m_Photos.length < 2 ) {
            return;
        }
        
        // Move to the next photo - return to start if needed
        m_PhotoIndex++;
        if ( m_PhotoIndex >= m_Photos.length ) {
            m_PhotoIndex = 0;
        }
        
        // If we're showing the first photo again...
        if ( m_PhotoIndex == 0 ) {
            
            // Show the 1st, then fade the last out to reveal it
            $( m_Photos[ 0 ] ).show();
            $( m_Photos[ m_Photos.length - 1 ] ).fadeOut( FADE_TIME );
        }
        
        // Otherwise, if we're not showing the first...
        else {
            
            // Fade in the next, then hide this photo
            $( m_Photos[ m_PhotoIndex ] ).fadeIn( FADE_TIME, function() {
                $( m_Photos[ m_PhotoIndex - 1 ] ).hide();
            } );
        }
        
    } // end NextPhoto()



    // PRIVATE DATA



    // MAIN SCRIPT

    // When the document is ready, set up the slide-change interval
    $( function() {

        // Get the slideshow elements
        m_Photos = $( '#homeSlideshow > *' ).get();

        // Set to absolute positioning
        $( m_Photos ).css( { position: 'absolute', left: '0px', top: '0px' } );
            
        // Hide all but the first
        $( m_Photos ).not( ':eq(0)' ).hide();
        $( m_Photos ).eq( 0 ).show();
        

        // Make the initial delay short, then fade at the regular interval
        setTimeout( 
            function() {
                NextPhoto();
                setInterval( NextPhoto, SLIDE_INTERVAL );
            }, 
            INITIAL_INTERVAL
        );
        
    } ); // end when document is ready


} )( jQuery ); // end hide private members
