// requires jQuery
// Laura Zucchetti, Andrew Goldstiver

 $(document).ready(function() {

   //You can make this even fancier! Take a look at http://fancybox.net/api
//   $("a.group").fancybox(); >> replaced with Shadowbox JS plugin
   
   //Laura added this in to make nth & last child css selectors work in IE. It just apends last to where needed.
   $("section.clients ul li:nth-child(5n+5)").addClass("last");
   $("ul.service-snippet li:last-child").addClass("last");


   $('.raptor').raptorize();
   $('.monkey').click(function(){
     window.location = "http://www.youtube.com/watch?v=5_sfnQDr1-o";
   });

 });


// fun stuff
$(document).ready(function() {

  var cardContainer = $('ul.people-list');
  var cards = $('ul.people-list li');

  if (cards.length) {
    $('body').prepend('<a href="#" class="cards-open">Labs</a>');
  }

  $('.cards-open').live('click', function() {
    $(this).hide();
    
    var padding = 25;
    var scrollTop = $(window).scrollTop();
    var theRow = 0;
    var theCol = 0;
    var numberOfCols = parseInt((cards.length + 1) / 2);
    var hSpacing = ($(window).width() - cards.width() - 2 * padding )/ (numberOfCols - 1);
    var vSpacing = cards.height() + padding;

    // store and set CSS for the original and new positions
    cards.each(function(){
      var originalOffset = $(this).offset();

      originalOffset.top = originalOffset.top - scrollTop;   

      $(this).data('original', originalOffset)
             .css(originalOffset);

      var newLeft = theCol * hSpacing + padding;
      theCol++;

      var newTop = theRow * vSpacing + padding;
      if (theCol === numberOfCols) {
        theRow++;
        theCol = 0;
      }

      originalOffset.top = originalOffset.top + scrollTop;
      $(this).data('new',{left: newLeft, top: newTop});
    });

    // prep for moving the cards
    $(".entry-page").height($(".entry-page").height())
                    .width($(".entry-page").width());

    // remove the cards from their normal position and add to the body
    cardContainer.css($('ul.people-list').offset())
                 .addClass('cards')
                 .appendTo('body');

    $('#container').fadeOut(1500, function(){
      $('body').prepend('<a href="#" class="cards-close">Close</a>');
    });

    // move the cards
    cards.each(function(index){
      $(this).addClass('card')
             .delay(1700)
             .delay(Math.random() * 2000)
             .animate($(this).data('new'), 1200);  
    });
    
    // set up ajax
    $('<div id="card-result"></div>').appendTo('body');
    $('<div class="loading"></div>').appendTo('body');

    $(cards).find('a').click(function(){
      $('.loading').show();
      $('#card-result').hide().load($(this).attr('href') + ' .people-introduction', function(){
        $('.loading').hide();
        $('.people-introduction').addClass('clearfix');
        $(this).fadeIn();
      });
      
      return false;
    });

    return false;
  });


  $('.cards-close').live('click', function() {
    $(this).remove();
    $(cards).find('a').unbind();
    $('.loading').remove();
    $('#card-result').fadeOut(function(){
      $(this).remove();
    });
   
    cards.each(function(index){
      $(this).stop()
             .delay(index * 20)
             .animate($(this).data('original'), 800 );  
    });

    $('#container').delay(800)
                   .fadeIn(1200, function(){

      cardContainer.removeClass('cards')
                   .removeAttr('style')
                   .appendTo(".entry-page");

      cards.removeClass('card')
           .removeAttr('style');

      $('.cards-open').show();
    });

    return false;
  });

});

