$(document).ready(function(){
  // Hide other questions
  $('#question_container li:gt(1)').hide();
  $('#question_container').css('overflow','hidden');
  
  // Show any error messages
  setTimeout('$(\'.messages\').slideDown();',500);
  
  // Allow closing of the message window
  $('.messages').click(function(){
    $(this).slideUp('slow');
  });
  
  // Enable Superfish menu
  $('.sf-menu').superfish({delay: 200, autoArrows: false, dropShadows: false}).find('ul').bgIframe({opacity:false}); 
  
  // Rotate question list every N seconds
  if ('#question_container')
    setInterval('rotate_list(\'#question_container ul\', 2)', 8000);
  
  // Change links to open in new windows
  $('a.blank').click(function(){
    window.open(this.href,'_blank');
    return false;
  });
});

function rotate_list(elem, active) {
  // Default to one active list item
  var active = active || 1;
  
  // Don't rotate if there aren't enough elements in the list
  if (active >= $(elem).find('li').size())
    return false;
  
  // Fade the first list item out, slide it up, and fade in the next list item
  $(elem).find('li:first').fadeTo('slow', 0.01, function(){
    $(this).slideUp('slow', function(){
      $(this).appendTo(elem).css('opacity','');
      $(this).parents('ul:first').find('li:eq(' + (active - 1) + ')').fadeIn('slow');
    });
  });
  
  return true;
}
