jQuery(document).ready( function($) {
  if ( $('.front .intro').length > 0 ) {
    play_opening_animation();
  }
  
  $('.collapsible').each( function() {
    $(this).children().not('h3').wrapAll('<div class="collapsing-wrapper" />').parent().hide();
  });
  $('.collapsible h3').css({cursor: 'pointer'}).click( function() {
    if ( !$(this).parent().hasClass('expanded') ) {
      $('.collapsible.expanded').removeClass('expanded')
        .children('.collapsing-wrapper').slideUp(1000);
      $(this).parent().addClass('expanded')
        .children('.collapsing-wrapper').slideDown(1000);
    } else {
      $(this).parent().removeClass('expanded')
        .children('.collapsing-wrapper').slideUp(1000);
      console.log($(this).parent().children('.collapsing-wrapper'));
    }
  });
});

function play_opening_animation() {
  $ = jQuery;
  $('.front .intro h2').fadeIn(1000, function() {
    $('.front .intro h3').fadeIn(2000, function() {
      show_next_frame($('.front .intro li:first-child'));
    });
  });
}
function show_next_frame(el) {
  $ = jQuery;
  el.fadeIn(1000, function() {
    if ( el.next('li').length > 0 ) {
      el.animate( { opacity: 1 }, {duration: 1000, complete: function() {
        el.fadeOut(500, function() {
          show_next_frame(el.next('li'));
        });
      }});
    } else {
      el.animate( { opacity: 1 }, {duration: 1000, complete: function() {
        $('.front .intro *').fadeOut( 2000, function() {
          $('.front .intro').animate( { opacity: 0 }, {duration: 2000, complete: function() {
            $('.front .final').fadeIn(1000);
          }});
        });
      }});
    }
  });
}