﻿/**
 * jQuery.portfolio - Portfolio plugin using jQuery
 * Written by Adam Bauerle (adam AT caffeinetocode DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2010/2/2
 *
 * @author Adam Bauerle
 * @version 0.1
 
 * @description
 *   Plugin will create a portfolio of objects made from the children
 *   of the specified portfolio object (currently built to use UL->LI).
 *
 **/

(function(jQuery) {
    jQuery.fn.Portfolio = function(options) {
        var defaults = {
            slideTransitionSpeed: 1000,   // How fast the transition takes place
            slidePauseInterval: 6000      // How long a slide sits
        };

        var options = jQuery.extend(defaults, options);

        // Specify All Portfolio Events
        // ----------------------------
        nextSlide = function(obj) {
            first = obj.children('ul').children('li:first').clone(true);
            height = first.height();

            // Move the next slide into place (move the viewed one to the end,
            //  which will move the next slide to the top to view; hide it first)

            // Fade out the just viewed slide
            obj.children('ul').children("li:first").fadeOut(options.slideTransitionSpeed)
                                .find('img').fadeOut(options.slideTransitionSpeed);

            // Move the slides up
            obj.children('ul')
    	        .animate({ top: '-=' + height + 'px' }, options.slideTransitionSpeed, function() {
    	            obj.children('ul').children('li:first').remove();
    	            obj.children('ul').children('li:first').fadeIn(options.slideTransitionSpeed)
                                        .find('img').fadeIn(options.slideTransitionSpeed);
    	        });

            // Add the first slide to the end
            first.appendTo(obj.children('ul'));
        };

        prevSlide = function(obj) {
            last = obj.children('ul').children('li:last').clone(true);
            height = last.height();

            // Fade out the just viewed slide
            obj.children('ul').children('li:first').fadeOut(options.slideTransitionSpeed)
                                .find('img').fadeOut(options.slideTransitionSpeed);

            // Add the last slide to the beggining to view
            last.prependTo(obj.children('ul'));

            obj.children('ul')
    	        .animate({ top: '+=' + height + 'px' }, options.slideTransitionSpeed, function() {
    	            obj.children('ul').children('li:last').remove();
    	            obj.children('ul').children('li:first').fadeIn(options.slideTransitionSpeed)
                                        .find('img').fadeIn(options.slideTransitionSpeed);
    	        });
        };

        // Pause the portfolio transition
        pause = function(obj) {
            if (interval != null) {
                clearInterval(interval);
            }
        };

        // Resume the portfolio transition
        resume = function(obj) {
            if (obj.children('ul').children('li').size() > 1) {
                // Transition into the next slide:
                // ("obj" is the portfolio object)
                interval = setInterval('nextSlide(obj)', options.slidePauseInterval);
            }
        };

        // Setup the portfolio and run
        return this.each(function() {
            // Get the portfolio object (for each portfolio instance)
            obj = jQuery(this);

            // Add the "next" slide functionality
            obj.find("a.Next-Button").click(function() { nextSlide(obj); return false; });

            // Add the "prev" slide functionality
            obj.find("a.Prev-Button").click(function() { prevSlide(obj); return false; });

            // Setup pause on mouseover
            obj.mouseover(function() {
                //stop current animation (only if needed)
                pause(obj);
            });

            // Setup resume transition on mouseout
            obj.mouseout(function() {
                //resume animation (only if needed)
                resume(obj);
            });

            // Show first slide
            obj.children('ul').children('li:first').show();

            // Run the portfolio
            if (obj.children('ul').children('li').size() > 1) {
                interval = setInterval('nextSlide(obj)', options.slidePauseInterval);
            } else {
                // There are no slides to transition through, hide the controls
                obj.find("#ShowCase-Controls").hide();
            }
        });
    };
})(jQuery);

