(function($) {

$.fn.infiniteCarousel = function () {
   
   return this.each(function () {

      var $wrapper = $('> div', this).css('overflow', 'hidden'),
         $slider = $wrapper.find('> div'),
         $items = $slider.find('> span'),   
         wrapperWidth = $wrapper.innerWidth(),
         //wrapperWidth = 1000, 
         pages = 1,  //aktualna strana
         currentPage = 1, //pocet stran
         moveFirst = 0,//posun prvej strany 
         itemsWidth = 0;

      // 1. Vypocet poctu stran, naskladanie elementov na strany, praznych elementov
      $($items).each(function() {
         itemsWidth += $(this).outerWidth();
         if(itemsWidth > pages*wrapperWidth) {              
            var smallEmptyWidth = pages*wrapperWidth - (itemsWidth - $(this).outerWidth());
            var prevWidth = $(this).prev().outerWidth();
            /*if (pages == 1)
               moveFirst = wrapperWidth - prevWidth - smallEmptyWidth;*/
            $(this).before('<span class="empty" style="width: '+ smallEmptyWidth +'px"></span>'+$(this).prev().html());
            $(this).prev().find('a').css({padding: '1px 13px 2px 10px'}); //prvy element viac odsadit
            itemsWidth -=3;                             //pripocitanie sirky o odsadenie 38-10px
            itemsWidth += prevWidth;                    //pripocitanie sirky predchadzajuceho
            itemsWidth +=smallEmptyWidth;
            pages++;   
         }
      });
      
      // 2. Vypocet velkosti prazdneho elementu a zaradenie nakoniec
      if ((wrapperWidth) >= itemsWidth)
         var emptyWidth = wrapperWidth - itemsWidth;
      else
         var emptyWidth =  pages*(wrapperWidth) - itemsWidth;
                
      $slider.append('<span class="empty" style="width: '+ emptyWidth +'px"></span>');
      $items = $slider.find('> span');
        
      // 3. Prilepenie prazdnych elementov pred zoznam a za zoznam
      $items.filter(':first').before('<span class="empty" style="width: '+ wrapperWidth +'px"></span>');
      $items.filter(':last').after('<span class="empty" style="width: '+ wrapperWidth +'px"></span>');
      $items = $slider.find('> span'); // reselect
        
      // 4. Set the left position to the first 'real' item
      $wrapper.scrollLeft(wrapperWidth);

      // 5. paging function
      function gotoPage(page) {
         var dir = page < currentPage ? -1 : 1,
            n = Math.abs(currentPage - page),
            //left = singleWidth * dir * visible * n;
            left = dir * (wrapperWidth) * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 2000, function () {
                if (page == 0) {                                                                                              
                    $wrapper.scrollLeft((wrapperWidth) * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(wrapperWidth);
                    // reset back to start position
                    page = 1;
                } 
               currentPage = page;

               //skryvanie tlacitok
               if(currentPage == 1)
                  $backButton.css({display: 'none'});
               else
                  $backButton.css({display: 'block'});
               
               if(currentPage == pages)
                  $forwardButton.css({display: 'none'});
               else
                  $forwardButton.css({display: 'block'});
               
            });                
            
            return false;
        }
      //resetovanie zobrazenia sipok 
      if($(this).find('a.arrow').hasClass('arrow'))
         $(this).find('a.arrow').remove();
           
      $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
      
      
      var $backButton = $('a.back', this),
      $forwardButton = $('a.forward', this);
      
      if (pages > 1)
         $backButton.css({display: 'none'});
      else {
         $backButton.css({display: 'none'});
         $forwardButton.css({display: 'none'});
      }
              
      // 6. Bind to the forward and back buttons
      $backButton.click(function () {
         return gotoPage(currentPage - 1);                
      });
        
      $forwardButton.click(function () {
         return gotoPage(currentPage + 1);
      });
        
      // create a public interface to move to a specific page
      $(this).bind('goto', function (event, page) {
         gotoPage(page);
      });   
   });  
};

})(jQuery);
