jQuery(document).ready(function($){

  // Settings
  curImg = 0;
  curTesti = 0;
  slideAmount = 200;
  timeAnimImg = 1500;
  timeAnimTesti = 800;
  timeWaitImg = 4000;
  timeWaitTesti = (timeWaitImg + timeAnimImg) * 2 - timeAnimTesti * 2;
  
  // Images
  $('#slider .image:eq('+curImg+')')
    .css({ opacity: 1, backgroundPosition: '0 0' });
  $('#slider .image:not(:eq('+curImg+'))')
    .css({ opacity: 0, backgroundPosition: slideAmount + 'px 0' });
  timerImg = setTimeout(
    function() {
      nextImg = curImg + 1;
      if(nextImg >= $('#header .image').length) nextImg = 0;
      $('#header .tab ul a:eq('+nextImg+')').trigger('click');
    },
    timeWaitImg
  );
  $('#header .tab ul a').click(function(){
    if(!$(this).hasClass('active')) {
      clearTimeout(timerImg);
      $('#header .tab ul a').removeClass('active');
      $(this).addClass('active');
      $('#slider .image:eq('+curImg+')')
        .stop()
        .animate({ opacity: 0, backgroundPosition: -slideAmount + 'px 0' }, timeAnimImg, 'swing');
      nextImg = $('#header .tab ul a').index(this);
      curImg = nextImg;
      $('#slider .image:eq('+nextImg+')')
        .stop()
        .css({ opacity: 0, backgroundPosition: slideAmount + 'px 0' })
        .animate({ opacity: 1, backgroundPosition: '0 0' }, timeAnimImg, 'swing', function(){
          timerImg = setTimeout(
            function() {
              nextImg = curImg + 1;
              if(nextImg >= $('#slider .image').length) nextImg = 0;
              $('#header .tab ul a:eq('+nextImg+')').trigger('click');
            },
            timeWaitImg
          );
        });
    }
  });
  
  // Testimonials
  $('#slider blockquote:gt(0)').css({
    opacity: 0
  });
  timerTesti = setTimeout(
    function() {
      nextTesti = curTesti + 1;
      if(nextTesti >= $('#header blockquote').length) nextTesti = 0;
      $('#header blockquote:eq('+nextTesti+')').trigger('click');
    },
    timeWaitTesti + timeAnimTesti
  );
  $('#header blockquote').click(function(){
    clearTimeout(timerTesti);
    nextTesti = curTesti + 1;
    if(nextTesti >= $('#header blockquote').length) nextTesti = 0;
    $('#header blockquote:eq('+curTesti+')')
      .stop()
      .animate({ opacity: 0 }, timeAnimTesti, 'swing', function(){
        $('#header blockquote:eq('+nextTesti+')')
          .stop()
          .animate({ opacity: 1 }, timeAnimTesti, 'swing', function(){
            curTesti = nextTesti;
            timerTesti = setTimeout(
              function() {
                nextTesti = curTesti + 1;
                if(nextTesti >= $('#header blockquote').length) nextTesti = 0;
                $('#header blockquote:eq('+nextTesti+')').trigger('click');
              },
              timeWaitTesti
            );
          });
      });
  });

});
