$(document).ready(function(){
  /*
  $('#category-wrapper h3 a').click(function(){
    $(this).parent().unbind('click');
  });
  
  $('#category-wrapper h3')
    .css('cursor', 'pointer')
    .hover(function(){
      $(this).addClass('hover');
    }, function(){
      $(this).removeClass('hover');
    })
    .append('<span>Expand</span>')
    .click(function(){
      var $ul = $(this).parent().find('ul:first');
      if ($ul.is(":visible")) {
        $(this).find('span').text('Expand');
        $ul.slideUp();
      } else {
        $(this).find('span').text('Collapse');
        $ul.slideDown();
      }
    });
  */
  
  function setEqualHeights($items, limit) {
    var count = 0; 
    $.each($items, function(i){
      count++;
      if (((i+1) % limit == 0) || $(this).next().length == 0) {
        $items.slice((i - count) + 1, i + 1).equalHeights();
        count = 0;
      }
    });
  }
  
  function next(){
    if ($('.books-list').data("ready") == "true") {
      $('.books-list').data("ready", "false");
      var current = $('.books-list li:visible');
      if (current.next().length) { current.fadeOut("slow").next().fadeIn("slow", function(){ $('.books-list').data("ready", "true"); }); }
      else { current.fadeOut("slow"); $('.books-list li:first').fadeIn("slow", function(){ $('.books-list').data("ready", "true"); }); }
    }
  }
  function prev(){
    if ($('.books-list').data("ready") == "true") {
      var current = $('.books-list li:visible');
      $('.books-list').data("ready", "false");
      if (current.prev().length) { current.fadeOut("slow").prev().fadeIn("slow", function(){ $('.books-list').data("ready", "true"); }); }
      else { current.fadeOut("slow"); $('.books-list li:last').fadeIn("slow", function(){ $('.books-list').data("ready", "true"); }); }
    }
  }
  
  $('html').addClass('js');
  $('#the-month-wrapper').removeClass('noJS').addClass('yesJS')
  $(".the-month").simpleSlide({panelWidth: 948});
  
  
  $(window).load(function(){
    
    $('#front #fun-stuff, #front #books, #front #grown-ups').equalHeights();
    
    if ($('div#books').length) {
      var height = 0;
      
      $('.books-list').data("ready", "true");
      $('div#books .books-list li').each(function(){
          height = (height > $(this).height() ? height : $(this).height());
      });
      $('div#books ul').css({
        'height' : height,
        'overflow' : 'hidden'
      });
      $('div#books li:gt(0)').hide();
      $('div#books').append('<ul id="js-books-controls" class="no-bullets"><li><a href="#" id="prev-link">Previous</a></li><li><a href="#" id="next-link">Next</a></li></ul>');
      
      $('#prev-link').live('click', function(){
        prev();
        clearInterval(nextTimer);
        return false;
      });

      $('#next-link').live('click', function(){
        next();
        clearInterval(nextTimer);
        return false;
      });
      
      var nextTimer = setInterval(function(){ next(); }, 4000);
      
    }
    setEqualHeights($('.download-list li'), 4);
  });
  
});