﻿/***

Author David Schweickhard

Purpose
    Makes a slide show by looping through all the elements within a provided container

***/


(function($) {


    $.fn.slideshow = function() {
        return this.each(function() {   
            $.slideshow(this);
        });
    };


    //runs the slide show against a provided container
    $.slideshow = function(container) {

        //get the child elements
        var elements = $(container).children();
        
        //hide all the elements and stager teir z-index ontop of each other and set position to ablolute
        for (var i = 0; i < elements.length; i++) {
            $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
        };
        
        //show the first one
        $(elements[0]).fadeIn(1000);
        
        //use set time out to advance to next slide and wait 3000 milliseconds
        setTimeout( function() {
                $.slideshow.next(elements, 0);
            },
            3000
        );
     

    };


    //advances to next slide
    $.slideshow.next = function (elements, currentIndex) {

        //assign the next index
        var nextIndex = currentIndex + 1;
        
        // set to 0 if beyond the limit of the container
        if (nextIndex == elements.length)
        {
            nextIndex = 0;
        }   
        
        //fade out the old fade in the new
        $(elements[currentIndex]).fadeOut(1000);
        $(elements[nextIndex]).fadeIn(1000);
		
        
        //recursivly execute against the next index
        setTimeout( function() {
                $.slideshow.next(elements, nextIndex);
            },
            3000
        );
        

    };
    
    
})(jQuery);



