(function($) {
    
    /**
     * Displays an expandable testimonial gallery.
     */
    $.fn.testimonialGallery = function() {
        $(this).each(function() {
            var gallery = $(this);
            var itemContainer = gallery.find(".testimonial-items");
            var items = gallery.find(".testimonial-item");
            var moreTestimonials = gallery.find(".more-testimonials a");
            // Hide excess testimonials.
            var originalHeight = itemContainer.height();
            var compactHeight = Math.max(items.eq(0).height(), items.eq(1).height());
            itemContainer.height(compactHeight);
            // Add the show behaviour.
            moreTestimonials.toggle(function() {
                itemContainer.animate({
                    height: originalHeight
                }, {
                    complete: function() {
                        moreTestimonials.html("Less testimonials").addClass("open");
                    }
                });
                return false;
            }, function() {
                itemContainer.animate({
                    height: compactHeight
                }, {
                    complete: function() {
                        moreTestimonials.html("More testimonials").removeClass("open");
                    }
                });
                return false;
            });
        });
    }
    
    /**
     * Displays a scrollable media gallery.
     */
    $.fn.mediaGallery = function() {
        $(this).each(function() {
            var gallery = $(this);
            var currentPage = 0;
            var pages = gallery.find(".media-page");
            var previousButton = gallery.find(".previous");
            var nextButton = gallery.find(".next");
            var items = gallery.find(".media-item");
            // Set up the initial positions.
            pages.each(function(n) {
                var page = $(this);
                page.css("left", n * 840);
            });
            // Shows the given page.
            function showPage(dest) {
                pages.each(function(n) {
                    var page = $(this);
                    page.animate({
                        left: (n - dest) * 840
                    }, "slow");
                });
                currentPage = dest;
            }
            // Shows the next page.
            function next() {
                if (currentPage == pages.length - 1) {
                    showPage(0);
                } else {
                    showPage(currentPage + 1);
                }
                return false;
            }
            // Shows the previous page.
            function prev() {
                if (currentPage == 0) {
                    showPage(pages.length - 1);
                } else {
                    showPage(currentPage - 1);
                }
                return false;
            }
            // Tie in the buttons.
            previousButton.click(prev);
            nextButton.click(next);
        });
    }
    
    $(function() {
        // Activate the products dropdown.
        var navItemProducts = $("#nav-item-products a");
        var navProducts = $("#nav-products");
        navProducts.hide();
        navItemProducts.toggle(function() {
            navItemProducts.addClass("here");
            navProducts.slideDown("fast");
        }, function() {
            navItemProducts.removeClass("here");
            navProducts.slideUp("fast");
        })  
    });
    
}(jQuery))
