$(function(){
  var btn    = $("#btn li");
  var img    = $("#img #img_other div");
  var count  = 0;
  var images = 3;
  var time   = 5000;
  var flag   = 0;
  btn.click(function() {
    if (count != btn.index(this) + 1 && !(flag == 1 && count == 0 && images == btn.index(this) + 1)) {
      $('#img_first').fadeOut();
      count = btn.index(this);
      img.css('z-index', 0);
      img.eq(count).css('z-index', 1);
      img.fadeOut();
      img.eq(count).fadeIn();
      btn.removeClass();
      $(this).addClass('active');
      count++;
      if (count == images) {
        count = 0;
      }
      flag = 1;
    }
  });
  btn.mouseup(function() {
    clearInterval(timerID);
    timerID = setInterval(function() {
      btn.eq(count).click();
    }, time);
  });
  timerID = setInterval(function() {
    btn.eq(count).click();
  }, time);
});

