jQuery.fn.animation = function(options){
      // настройки по умолчанию
      var options = jQuery.extend({
        interval: 5000, // интервал смены картинок
      },options);

      var imgCount = 0; // количество кадров (картинок)
      var animationContainer = jQuery(this);
      var uniqueID = jQuery(this).attr('id') + 'Img';
      var zIndex;

      function change_frame(i){
            var prevIndex = i;
            if (i >= (imgCount - 1)) {
                i = 0;
            }
            else {
                i++;
            }
            var nextImg = animationContainer.find("img").eq(i);

            nextImg.css('z-index', zIndex + 1).css({opacity: 0}).show();

            nextImg.animate({opacity: 1}, 500, function(){
                animationContainer.find("img").eq(prevIndex).hide();
                nextImg.css('z-index', zIndex - 1);
            });

            setTimeout(function(){ change_frame(i); }, options.interval);
        }

        function start_animation() {
          var firstImg = animationContainer.find("img").eq(0)
          firstImg.show();
          zIndex = firstImg.css('z-index');

          setTimeout(function(){ change_frame(0); }, options.interval);
        }

      return this.each(function() {
          imgCount = animationContainer.find("img").length;
          if (imgCount) {
              start_animation();
          }
      });
};
