jQuery.fn.wImageSwitch = function (params) {
  var $this = $(this);
  if (!params.hasOwnProperty('images')) {
    alert('no params');
    return $this;
  }
  params = $.extend({
    width: '640px',
    height: '480px',
    zIndex: 1,
    delay: 3000,
    animation: 1000
  }, params, {});
  var wImageCore = function ($this, params) {
    return {
      index: 0,
      size: 0,
      addImage: function ($this, src, zIndex) {
        $this.append(['<img src="', src, '" style="position: absolute; top: 0; left: 0; z-index: ', ++zIndex, '; opacity: 0;" />'].join(''));
        return zIndex;
      },
      init: function ($this, params) {
        $this.css({
          width: params.width,
          height: params.height,
          zIndex: params.zIndex,
          border: '0',
          padding: '0',
          position: 'relative'
        });
        var zIndex = params.zIndex;
        $(params.images).each(function (idx, el) {
          zIndex = wImageCore.addImage($this, el, zIndex);
        });
        wImageCore.addImage($this, params.images[0], zIndex);
        wImageCore.index = 0;
        wImageCore.size = $(params.images).length;
        $('img:eq(0)', $this).css({
          opacity: 1
        });
        if (1 == wImageCore.size) {
          return false;
        }
        setInterval(function() {
          wImageCore.loop($this, params.animation);
        }, params.delay + params.animation);
      },
      loop: function ($this, animation) {
        var idxOld = wImageCore.index;
        var idxNew = parseInt(idxOld) + 1;
        $('img:eq(' + idxNew + ')', $this).animate({
          opacity: 1
        }, animation, function () {
          //po objevení se nového obrázku smaž starý:
          $('img:eq(' + idxOld + ')', $this).css({
            opacity: 0
          });
          // pokud je nově objevený obrázek poslední, zobraz první a schovej poslední:
          if (idxNew == wImageCore.size) {
            wImageCore.index = 0;
            $('img:first()', $this).css('opacity', 1);
            $('img:last()', $this).css('opacity', 0);
          }
        });
        wImageCore.index = idxNew;
      }
    };
  }();
  wImageCore.init($this, params);
  return $this;
};
