(function($) {
    $.fn.rotator = function() {
      $e = $(this);
      if ($e.length < 2)
        return;
      data = $e.filter(":visible").metadata();
      duration = 10000;
      if (data.duration)
        duration = data.duration;
      setTimeout(function() {
        $e.rotate(function() {
          $e.rotator();
        });
      }, duration);
    };

    $.fn.toggleTo = function($to, callback) {
      $(this).fadeOut("slow", function(){
        $img = $to.find("img");
        if ($img.length > 0) {
          src = $img.attr("src");
          $img.attr("src", "/.grafika/pusty.gif");
          $img.attr("src", src);
        }
        $to.fadeIn("slow", callback);
      });
    };

    $.fn.rotate = function(callback) {
      $visible = $e.filter(":visible");
      if ($visible.get(0) == $e.filter(":last").get(0)) {
        $visible.toggleTo($e.filter(":first"), callback);
      }
      else {
        $visible.toggleTo($visible.next(":hidden"), callback);
      }
    };
})(jQuery);

$(document).ready(function(){
  $(".rotate").rotator();
});

