//changing products (index.php)
var allLinks; 
var allThings;
var lastEl;
var prevElem, nextElem;
var cykl;
var currentEl = 0;

function animuj(elem) {

  lastEl = allThings.length -1;
  prevElem = elem - 1;
  nextElem = elem + 1;
  
  if(prevElem < 0) {
    prevElem = lastEl;
  }
  
  if(nextElem > lastEl){
    nextElem = 0;
  }
  
  $(allThings).fadeOut(500);
  $(allThings[nextElem]).show(500);
  
  elem = nextElem;
  
  //zmiana koloru linku
  $(allLinks).removeClass('current');
  $(allLinks[elem]).addClass('current');
  //end.zmiana koloru linku
  
  cykl = setTimeout("animuj(" + elem +" )", 5000); 
}



$(document).ready(function(){
  allLinks = $('.stuff-btn');
  allThings = $('.thing');
  lastEl = allThings.length -1; 
  
  $(allThings).hide();
  $(allThings[lastEl]).show();
  
  $.each(allLinks, function(i, el){
   
     $(el).click(function(){
       if($(this).hasClass('current')) {
        return;
       }
       else {
         $(allLinks).removeClass('current');
         $(this).addClass('current');
         $(allThings).fadeOut(500);
         $(allThings[i]).show(500);
       }
       
       clearTimeout(cykl);
       cykl = setTimeout('animuj(' + i +')', 7000);
       currentEl = i;    
     });
  
  });
  
  cykl = setTimeout('animuj(' + lastEl + ')', 4000);
  
  $('#changing-stuff').mouseover(function(){
    clearTimeout(cykl);
  })
  
  $('#changing-stuff').mouseout(function(){
    cykl = setTimeout('animuj(' + currentEl +')', 4000);
  }) 
    
});
