// -----------------------------------------------------------------------------

jQuery(document).ready(function() {
  
  // ---------------------------------------------------------------------------
  
  // Slider tabs
  function changeSliderTab(index) {
    var tabs = jQuery('#slider .nav li');
    var items = jQuery('#slider .items');
    if (tabs.eq(index).hasClass('active') || items.is(':animated')) return;
    tabs.removeClass('active');
    items.animate({left: -index*jQuery('#slider .item:first').width()}, 'slow', function() {
      tabs.eq(index).addClass('active');
      jQuery('#slider .preview.images.active')
        .removeClass('active')
        .find('.play').css('visibility', 'visible').parent()
        .find('img').stop(true, true).hide().eq(0).show();
      jQuery('#slider .preview.video.played .player iframe').each(function() {
        Froogaloop(jQuery(this).get(0)).api('pause');
      });
    });
  }
  jQuery('#slider .items .item').each(function(num, item) {
    num++;
    var bgcolor = jQuery(item).css('background-color');
    jQuery('<li></li>')
      .text(num < 10 ? '0'+num : num)
      .css('background-color', bgcolor)
      .css('border-bottom-color', bgcolor)
      .addClass(num == 1 ? 'active' : '')
      .appendTo('#slider .nav')
      .click(function() {
        clearInterval(jQuery('#slider').data('itemsInterval'));
        changeSliderTab(jQuery(this).index());
      });
  });
  Cufon.replace('#slider .nav li', {fontFamily: 'AvantGarGotItcTEE'});
  
  // Slider images
  jQuery('#slider .preview.images').click(function() {
    clearInterval(jQuery('#slider').data('itemsInterval'));
    jQuery(this).filter(':not(.active)').addClass('active');
    jQuery('.play', this).css('visibility', 'hidden');
  });
  
  // Slider video
  jQuery('#slider .preview.video').click(function() {
    clearInterval(jQuery('#slider').data('itemsInterval'));
    jQuery(this).addClass('played');
    jQuery('.thumbnail', this).hide();
    jQuery('.player', this).css('visibility', 'visible');
    Froogaloop(jQuery('.player iframe', this).get(0)).api('play');
    jQuery('.play', this).css('visibility', 'hidden');
  });

  // Intervals
  jQuery('#slider')
    .data('itemsInterval', setInterval(function() {
      var tabs = jQuery('#slider .nav li');
      var current = tabs.filter('.active').index();
      var next = current < tabs.length-1 ? current+1 : 0;
      changeSliderTab(next);
    }, 5000))
    .data('imagesInterval', setInterval(function() {
      var images = jQuery('#slider .preview.images.active img');
      var current = images.filter(':visible').index();
      var next = current < images.length-1 ? current+1 : 0;
      images.eq(current).fadeOut();
      images.eq(next).fadeIn();
    }, 2000));
  
  // ---------------------------------------------------------------------------
  
  // Sections settings
  jQuery('#content .section').each(function() {
    jQuery(this).data('offset', jQuery(this).offset().top);
  });
  
  // Sections headers
  jQuery('#content .section > h2').click(function() {
    jQuery('#content .section.active .close:visible').click();
    jQuery('#content .section .content').slideUp(function() {
      jQuery(this).parent().parent().removeClass('active');
    });
    jQuery(this).parent().filter(':not(.active)').addClass('active').find('.content').slideDown();
  });
  
  // Menu sections
  jQuery('#menu li.item a').click(function() {
    var section = jQuery(jQuery(this).attr('href'));
    section.filter(':not(.active)').find('> h2').click();
    if (jQuery(window).height() < 840) {
      jQuery('html, body').animate({scrollTop: section.data('offset')}, 'normal');
    }
    return false;
  });
  
  // ---------------------------------------------------------------------------
  
  // Section panel
  jQuery('#content .section .nav > div').each(function() {
    jQuery(this).click(function() {
      var panel = jQuery(this).parent().parent().find('.panel');
      if (jQuery(this).hasClass('disabled') || panel.is(':animated')) return;
      var forward = jQuery(this).hasClass('next');
      var count = jQuery('.group', panel).length;
      var width = jQuery('.group:first', panel).outerWidth();
      panel.animate({left: (forward ? '-' : '+')+'='+width+'px'}, 'normal', function() {
        var nav = jQuery(this).parent().find('.nav');
        jQuery('> div', nav).removeClass('disabled');
        var left = jQuery(this).position().left;
        if (left == 0)                  jQuery('.prev', nav).addClass('disabled');
        if (left == -((count-1)*width)) jQuery('.next', nav).addClass('disabled');
      });
    });
  });
  
  // ---------------------------------------------------------------------------
  
  // Projects section header
  jQuery('#section-projects > h2').click(function() {
    jQuery(this).parent().find('.close:visible').click();
  });
  
  // Projects section thumbnail
  jQuery('#section-projects .thumbnail').click(function() {
    var section = jQuery('#section-projects');
    if (jQuery('.previews', section).is(':animated')) return;
    var preview = jQuery('#'+jQuery(this).parent().attr('id')+'-preview', section).show();
    jQuery('.previews', section).animate({left: '+=0'}, 300, function() {
      if ( ! preview.hasClass('played')) {
        Froogaloop(jQuery('iframe', preview).get(0)).api('play');
      }
      preview.addClass('played');
    }).animate({top: '0px'}, 'normal');
    jQuery('.close', section).fadeIn();
  });
  
  // Projects section close button
  jQuery('#section-projects .close').click(function() {
    var section = jQuery('#section-projects');
    jQuery('.previews .preview.played iframe', section).each(function() {
      Froogaloop(jQuery(this).get(0)).api('pause');
    });
    jQuery('.previews', section).animate({top: '-378px'}, 'normal', function() {
      jQuery('.preview', section).hide();
    });
    jQuery(this).fadeOut();
  });

});
