var duration = 700;
var current_index = 1;
var max_index = 1;
var aux = 0;

$(document).ready(function() {
  max_index = $('.large_pics li').length;
  
  $('#debug').html(current_index + ' - ' + max_index)
  
  $('.small_pics li, .large_pics li, #pics_description li').not($('.active_slide')).css('opacity', 0);

  $('#instructors_slideshow .arrow_next').click(function() {
    navigateNext('.small_pics');
    navigateNext('.large_pics');
    navigateNext('#pics_description');
      
    if(current_index < max_index) {
      current_index++;
    }
    
    $('#debug').html(current_index + ' - ' + max_index + ' - ' + aux)
    
    return false;
  });
  
  $('#instructors_slideshow .arrow_prev').click(function() {
    navigatePrev('.small_pics');
    navigatePrev('.large_pics');
    navigatePrev('#pics_description');

    if(current_index > 1) {
      current_index--;
    }
    
    $('#debug').html(current_index + ' - ' + max_index)
    
    return false;
  });
});

function navigateNext(el) {
  var t = $('.active_slide', el);
  $(t).css('opacity', 1);
  $(t).removeClass('active_slide');
  
  var next_slide = '';
  if(current_index == max_index) {
    next_slide = $(el).children(':first-child');
    aux++;
  } else {
    next_slide = $(t).next();
    aux = 0;
  }
  
  if(aux == 3) {
    current_index = 1;
  }
  
  $(t).animate({
    opacity: 0
  }, duration);
  
  $(next_slide).animate({
    opacity: 1
  }, duration, function() {
      $(next_slide).addClass('active_slide');
  });
}

function navigatePrev(el) {
  var t = $('.active_slide', el);
  $(t).css('opacity', 1);
  $(t).removeClass('active_slide');
  
  var prev_slide = '';
  if(current_index == 1) {
    prev_slide = $(el).children(':last-child');
    aux++;
  } else {
    prev_slide = $(t).prev();
    aux = 0;
  }
  
  if(aux == 3) {
    current_index = max_index + 1;
  }
  
  $(t).animate({
    opacity: 0
  }, duration);
  
  $(prev_slide).animate({
    opacity: 1
  }, duration, function() {
      $(prev_slide).addClass('active_slide');
  });
}