/*jslint white: false, browser: true */
/*global window jQuery console */

(function( $ ) {
    $( document ).ready( function() {
        var $sections, $p, $h2,
            current = 1,
            $postcards = $( '#postcard1, #postcard2' );

        if ($postcards.length === 2) {
            window.setInterval(
                function() {
                    current = current < 3 ? current + 1 : 1;

                    if (current < 3) {
                        $postcards.fadeOut( 'slow' );
                        $( '#postcard' + current ).fadeIn( 'slow' );
                    }
                },
                4000
            );
        }

        if ($( 'div.communication' ).length) {
            $sections = $( 'div.section' );
            $p = $sections.find( 'p' ).hide();

            $h2 = $sections.find( 'h2' ).bind( 'click', function( evt ) {
                $p.hide();
                $h2.siblings( 'h3' ).andSelf().removeClass( 'active' );

                $( evt.target ).siblings( 'h3' ).andSelf().addClass( 'active' )
                    .siblings( 'p' ).show();
            } );
        }

        $( 'p.email' ).each( function() {
            var $email = $( this ),
                email = $.trim( $email.text() ).replace( / at /, '@' ).replace( / dot /, '.' ),
                name = $.trim( $email.siblings( '.name' ).text() ),
                href = name ? name + ' &lt;' + email + '&gt;' : email;

            $email.empty().append( '<a href="mailto:' + href + '">' + email + '</a>' );
        } );

    } );
}( jQuery ));
