$(document).ready(function() {

/* ========================================
       Home Page
   ======================================== */

    if ( $('.page-template-page-home-php').length ) {
        /* Client list rotator */
        var timer = setInterval(function() {
            // If the active list is the last one
            if ( $('.client-list.active').is(':last-child') ) {
                // Set the next client list to the first one
                var $nextClient = $('.client-list:first-child');
            }
            // If it isn't
            else {
                // Set the next client to the next one
                var $nextClient = $('.client-list.active').next();
            }
            // Fade out the active list
            $('.client-list.active').fadeOut(1500).removeClass('active');
            // Fade in the next list
            $nextClient.fadeIn(1500).addClass('active');
        }, 4000); // Cycle every 4 seconds
    }

/* ========================================
       About Page
   ======================================== */

    if ( $('.page-template-page-about-php').length ) {
        /* Switching Between Mantra and Team */
        // When you click "our mantra"
        $('.our-mantra-link').click(function() {
            $('.our-team-link').removeClass('current');
            $('.our-mantra-link').addClass('current');

            $('.our-team').fadeOut();
            $('.our-mantra').fadeIn();
            return false;
        });

        // When you click "our link"
        $('.our-team-link').click(function() {
            $('.our-mantra-link').removeClass('current');
            $('.our-team-link').addClass('current');

            $('.our-mantra').fadeOut();
            $('.our-team').fadeIn();
            return false;
        });

        /* Selecting team members */
        // When you click a team member that isn't currently selected
        $('.team-member-select').on('click', ':not(.current)', function() {
            $('.team-member-info.current').fadeOut('slow', function() {
                $(this).removeClass('current');
            });
            $('.team-member-info.' + $(this).attr('id')).fadeIn('slow', function() {
                $(this).addClass('current');
            });
            $(this).siblings('.current').removeClass('current');
            $(this).addClass('current');
        });

        /* Rewind/Forward buttons */
        // When you click rewind
        $('.rewind').click(function() {
            $(this).siblings('img:not(.young)').fadeOut();
            $(this).fadeOut();
            $(this).siblings('.forward').fadeIn();
            return false;
        });

        // When you click forward
        $('.forward').click(function() {
            $(this).siblings('img:not(.young)').fadeIn();
            $(this).fadeOut();
            $(this).siblings('.rewind').fadeIn();
            return false;
        });
    }
    
/* ========================================
       Work Page
   ======================================== */

    if ( $('.page-template-page-work-php').length ) {
        /* Scrolling through work items */
        // Add current class to the first in the list
        $('.work-samples li:first-child').addClass('current');
        
        // When you click the next arrow
        $('.work-samples-viewport-container .next-work').click(function() {
            //
            // If you were advancing from the first one
            if ( $('.work-samples li.current').is('.work-samples li:first-child') ) {
                // Fade in the back button (because it was hidden)
                $('.work-samples-viewport-container .prev-work').fadeIn();
            }
            
            // Move the current class to the next item
            $('.work-samples li.current').removeClass('current').next().addClass('current');
            
            // If you are advancing to the last item 
            if ( $('.work-samples li.current').is('.work-samples li:last-child') ) {
                // Fade out the next button
                $('.work-samples-viewport-container .next-work').fadeOut();
            }

            // Animate the work 682px to the left
            $('.work-samples').animate({
                left: '-=682'
            }, 1000, function() {
            });

        });

        // When you click the prev arrow
        $('.work-samples-viewport-container .prev-work').click(function() {
            
            // If you are going back from the last item
            if ( $('.work-samples li.current').is('.work-samples li:last-child') ) {
                // Fade in the next arrow
                $('.work-samples-viewport-container .next-work').fadeIn();
            }

            // Move the current class to the prev item
            $('.work-samples li.current').removeClass('current').prev().addClass('current');

            // If you advancing to the first item
            if ( $('.work-samples li.current').is('.work-samples li:first-child') ) {
                // Fade out the first arrow
                $('.work-samples-viewport-container .prev-work').fadeOut();
            }

            // Animate the list 682px right
            $('.work-samples').animate({
                left: '+=682'
            }, 1000, function() {
            });

        });

        /* Jump to video if linked from Homepage */
        // Get the video ID from the URL
        var videoID = (location.hash).substring(1);
        // If the video ID exists
        if ( videoID ) {
            // Get the object of the video
            var $video = $('#' + videoID);
            // Remove the current class from the first video
            $('.work-samples li.current').removeClass('current');
            // Get the index of the clicked video
            var index = $video.parent().index();
            // Make the clicked video the current one
            $video.parent().addClass('current');;
            // Scroll to focus the clicked video
            $('.work-samples').animate({
                left: '-=' + (index * 682)
            }, 1000, function() {
                // If it is the last video
                if ( $('.work-samples li.current').is('.work-samples li:last-child') ) {
                    // Fade out the "next" arrow
                    $('.work-samples-viewport-container .next-work').fadeOut();
                }
                // If it not the first video
                if ( $('.work-samples li.current').is(':not(.work-samples li:first-child)') ) {
                    // Fade in the "prev" arrow
                    $('.work-samples-viewport-container .prev-work').fadeIn();
                }
            });
        }
    }

/* ========================================
       Blog Page
   ======================================== */

    if ( $('.page-template-page-blog-php').length || $('.single-blog-post') ) {

        /* Paginated post archive */
        // when you click a pagination link
        $('.pagination li a').click(function() {
            // Calculate what posts to fetch
            var offset = 1 + (parseInt($(this).html()) - 1) * 4;
            // Fade out current posts
            $('.recent-posts').animate({
                opacity: 0
            }, function() {
                // Load new posts from the file
                $('.recent-posts').load('/blog-post-load-more/?offset=' + offset, function() {
                    // Fade in new posts
                    $('.recent-posts').animate({
                        opacity: 1
                    });
                });
            });
            // Remove current class from previously selected page
            $('.pagination li a.current').removeClass('current');
            // Add to this page
            $(this).addClass('current');
            return false;
        });

        /* Photo gallery */
        // If you click a thumb
        $('.all-attached li a').click(function() {
            // Get the url of the image without the size
            var imageURL = $(this).children('img').attr('src').slice(0, -10);
            // Add the full size params
            $('.attachment-featured-gallery').attr('src', imageURL + "451x262.jpg");
            return false;
        });
    }

    $('a[rel=external]').attr("target", "_blank");
});

