(function($) {

    $.fn.banner = function(options) {

        options = $.extend($.fn.banner.defaults, options);
        var imgContainerId = $(this).attr('id');
        
        return this.each(function() {

            setInterval(function(){slideSwitch(imgContainerId, options.fadeSpeed)}, options.slideSpeed);

        });        

    }

    function slideSwitch(imgContainerId, fadeSpeed) {
        var $active = $('#'+imgContainerId+' img.show');

         if ( $active.length == 0 ) $active = $('#'+imgContainerId+' img:last');

         // use this to pull the images in the order they appear in the markup
         var $next = $active.next().length ? $active.next()
        : $('#'+imgContainerId+' img:first');

         // uncomment the 3 lines below to pull the images in random order

         // var $sibs = $active.siblings();
         // var rndNum = Math.floor(Math.random() * $sibs.length );
         // var $next = $( $sibs[ rndNum ] );


         $active.addClass('last-show');

         $next.css({opacity: 0.0})
         .addClass('show')
         .animate({opacity: 1.0}, fadeSpeed, function() {
         $active.removeClass('show last-show');
         });
    }

    function slideShow(imgContainerId, fadeSpeed) {

        var current = $('#'+imgContainerId+' .show');
        var nextImg = current.next('img').length?current.next('img'):current.parent().children('img:first');


        nextImg.addClass('show');
        current.fadeOut(fadeSpeed,function(){
            $(this).removeClass('show');
        });
        
    }

    $.fn.banner.defaults = {
      slideSpeed:  5000,
      fadeSpeed:   1500
    };

})(jQuery);


