jQuery(document).ready(function($)
{
  var ewFader_methods=
  {
    init: function(options)
    {
      return this.each
      (
        function()
        {
          var $this=$(this); //deklarace objektu
          var settings= //výchozí hodnoty
          {
            'height': 400, //výška
            'speed': 2000, //rychlost
            'width': 400 //šířka
          };
          $.extend(settings, options);
          
          var controls=false;
          
          if($this.find(".controls").get(0))
            controls=true;
          
          $this.height(settings["height"]); //nastavení výšky
          $this.width(settings["width"]); //nastavení šířky
          
          if(!controls)
            $this.append('<ul class="controls"></ul>'); //element pro tvorbu ovládacího panelu
       
          $this.children(".media").each(function(index)
          {
            var active="";
            if(index)
              $(this).hide(); //zmizení ostatních snímků
            else
              active=" active";
            index++;
            
            if(!controls)
              $(this).siblings('ul.controls').append('<li class="item'+active+'">'+index+'</li>');
          });
          
          var timeout;
          $options={fader: $this, options: settings, timer: timeout};
          timeout=window.setTimeout
          (
            function()
            {
              $('.fader .controls .item').eq(1).trigger('click', $options);
            },
            5000
          );
          $this.children('.controls').children('.item').bind('click', $options, ewFader_methods.update);
        }
      );
    },
    update: function(event)
    {
      var target=$(event.target);
      if(!target.is(".item"))
        target=$(event.target).parents(".item");
      
      var fader=event.data.fader;
      var item=target.index();
      fader.children(".media").each(function()
        {
          if($(this).index()!=item)
            $(this).fadeOut(event.data.options["speed"]);
          else
            $(this).fadeIn(event.data.options["speed"]); 
        }
      );
      target.addClass("active");
      target.siblings().removeClass("active");
      
      $children=$('.fader .controls').children().size();
      
      clearTimeout(event.data.timeout);
      event.data.timeout=window.setTimeout
      (
        function()
        {
          fader.find('.controls .item').eq((item+1)%$children).trigger('click', $options);
        },
        5000
      );
    }
  };

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

