jQuery(document).ready(function($)
{
  var ewRotator_methods=
  {
    init: function(options)
    {
      return this.each
      (
        function()
        {
          var $this=$(this); //deklarace objektu
          var settings= //výchozí hodnoty
          {
            'height': 400, //výška
            'mod': 4, // počet obrázků na stránku
            'speed': 1000, //rychlost
            'width': 400 //šířka
          };
          $.extend(settings, options);
          $this.initiated=false;

          $this.height(settings.height); //nastavení výšky
          $this.width(settings.width); //nastavení šířky
          $this.mod=settings.mod; //nastavení dělitele
          
          $n=1;
          $this.children('.media').hide();
          $this.children('.media').each
          (
            function()
            {
              if($n++<=$this.mod)
              {
                $(this).fadeIn(settings.speed);
              }    
            }
          );
          
          var timeout;
          $options={rotator: $this, options: settings, timer: timeout};
          timeout=window.setTimeout
          (
            function()
            {
              $this.find('.controls .item.next').trigger('click', $options);
            },
            5000
          );
          $this.children('.controls').children('.item').bind('click', $options, ewRotator_methods.update);
        }
      );
    },
    update: function(event)
    {
      var target=$(event.target);
      if(!target.is(".item"))
        target=$(event.target).parents(".item").eq(0);
      var rotator=event.data.rotator;
      var mod=event.data.options.mod;
      clearTimeout(event.data.timeout);
      
      items=target.attr("class").match(/items-(\d+)/i);
      $i=items[1];
        
      var count=Math.ceil(rotator.children(".media").length/mod);
      var next=$i, prev=$i;

      if(++next>count)
      {
        next=1;
      }
      if(--prev<1)
      {
        prev=count;
      }
        
      rotator.find(".controls .item.previous").attr("class", "item previous items-"+prev);
      rotator.find(".controls .item.next").attr("class", "item next items-"+next);
        
      rotator.children(".media").each
      (
        function()
        {
          $j=$(this).attr("class").match(/media-(\d+)n/i);
          $j=$j[1];
          $(this).stop(true, true);
          if($(this).is(".media-"+$i+"s"))
          {
            $(this).delay($j*500).fadeIn
            (
              event.data.options.speed
            );
          }
          else
          {
            $(this).delay($j*500).fadeOut
            (
              event.data.options.speed
            );
          }
        }
      );
      
      event.data.timeout=window.setTimeout
      (
        function()
        {
          rotator.find('.controls .item.next').trigger('click', $options);
        },
        5000
      );
    }
  };

  $.fn.ewRotator=function(method)
  {
    if(ewRotator_methods[method])
      return ewRotator_methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    else if(typeof method==='object'||! method)
      return ewRotator_methods.init.apply(this, arguments);
    else
    {
      $.error("Neplatná metoda.");
      return false;
    }
  };
});

