var nb_shown = 4;
var allowAnimate = nb_shown + 1;
var timer;
var timer_interval = 7000;

function offsetWithTime() {
  offsetCarrousel(1);
  timer = setTimeout('offsetWithTime()', timer_interval);
}

function offsetCarrousel(x) {
  var w = 103;
  var nb = 6;

  if(allowAnimate != nb_shown + 1) {
    return;
  }

  var maxL = 5;
  var minL = -2;
  
  $('#pictures_carousel_img_slider img').each(function(i, el) {
    allowAnimate = 0;
    var l = ($(el).position().left / w);
    l = parseInt(l, 0);
    var from = l;
    l -= x;

    var animate = true;

    if(l == minL) {
      l += 6;
      animate = false;
      
    } else if(l == maxL) {
      l -= 6;
      animate = false;

    }
    
    
    if(animate) {

      $(el).animate({
        left: l*w
      }, 'fast', function() {
        allowAnimate ++;
      });
      
    } else {
      $(el).css('left', l*w);      

    }
  });
  
}

$(function() {
  
  $('#left_arrow').click(function() {
    offsetCarrousel(-1);
    return false;
  });
  
  $('#right_arrow').click(function() {
    offsetCarrousel(1);
    return false;
  });
  
  timer = setTimeout('offsetWithTime()', timer_interval);
});
