jQuery(document).ready(function($)
{
  var ewCarousel_methods=
  {
    init: function(options)
    {
      return this.each
      (
        function()
        {
          var $this=$(this); //deklarace objektu
          var settings= //výchozí hodnoty
          {
            'height': 400, //výška
            'speed': 1000, //rychlost
            'width': 400 //šířka
          };
          $.extend(settings, options);
          
          var current_width=0; //aktuální šířka prvků v kolotoči
          
          $this.height(settings["height"]); //nastavení výšky
          $this.width(settings["width"]); //nastavení šířky
          
          //ovládací prvky předchozí/další
          $this.append ('<ul class="controls"><li class="item previous">Předchozí</li><li class="item next">Další</li></ul>');
          
          var overflow=false;
          $this.children(".media").each(function()
          {
            if(current_width+$(this).width()>settings["width"])
              overflow=true;
            else
              current_width+=$(this).width();

            if(overflow)
              $(this).hide(); //zmizení ostatních snímků
            else
              $(this).addClass("active");
          });
          
          $this.children('.controls').children('.item').bind('click', {carousel: $this, options: settings}, ewCarousel_methods.update);
        }
      );
    },
    update: function(event)
    {
      var delay=250;
      var target=$(event.target);
      if(!target.is(".item"))
        target=$(event.target).parents(".item");
      
      var carousel=event.data.carousel;
      var direction=target.hasClass("next") ? "next" : "previous";
      
      switch(direction)
      {
        case "previous":
          var count=carousel.children(".media.active").size();
          var first=carousel.children(".media.active:first").index();
          var last=carousel.children(".media.active:last").index();
          if(first===carousel.children(".media:first").index())
            break;
          carousel.children(".media.active").each
          (
            function()
            {
              $(this).delay((last-$(this).index())*delay).fadeOut(
                event.data.options["speed"],
                function()
                {
                  if($(this).index()===last)
                  {
                    var current_width=0;
                    carousel.children(".media:lt("+first+")").each
                    (
                      function(index)
                      {
                        var overflow=false;
                        if(current_width+$(this).width()>event.data.options["width"])
                          overflow=true;
                        else
                          current_width+=$(this).width();
            
                        if(overflow)
                          $(this).hide(); //zmizení ostatních snímků
                        else
                        {
                          $(this).addClass("active");
                          $(this).delay(delay*($(this).index()+count)).fadeIn(event.data.options["speed"]);
                        }
                      }
                    );
                  }
                  $(this).removeClass("active");
                }
              );
            }
          );
          break;
        case "next":
          var count=carousel.children(".media.active").size();
          var first=carousel.children(".media.active:first").index();
          var last=carousel.children(".media.active:last").index();
          if(last===carousel.children(".media:last").index())
            break;
          carousel.children(".media.active").each
          (
            function()
            {
              $(this).delay((last-$(this).index())*delay).fadeOut(
                event.data.options["speed"],
                function()
                {
                  if($(this).index()===last)
                  {
                    var current_width=0;
                    carousel.children(".media:gt("+last+")").each
                    (
                      function(index)
                      {
                        var overflow=false;
                        if(current_width+$(this).width()>event.data.options["width"])
                          overflow=true;
                        else
                          current_width+=$(this).width();
            
                        if(overflow)
                          $(this).hide(); //zmizení ostatních snímků
                        else
                        {
                          $(this).addClass("active");
                          $(this).delay(delay*($(this).index()-last+count)).fadeIn(event.data.options["speed"]);
                        }
                      }
                    );
                  }
                  $(this).removeClass("active");
                }
              );
            }
          );
          break;
      }
      
      /*
      carousel.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");
      */
    }
  };

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

