$(document).ready(function() { 
  var slides = "#slides .slide";
  
  $("#issues-nav ul").jcarousel();
  $("#issues-nav li:last").addClass("last");
  
  $("#issues-nav li a").click(function(){
    
    var targetSlide = $(this).attr('href');
    var $targetSlide = $(targetSlide);
    
    if ($targetSlide.is(':hidden')) {
      $(slides + '.active').fadeOut("fast").removeClass('active');
      $targetSlide.fadeIn("slow").addClass('active');
    }  
    
    $("#issues-nav li a.active").removeClass('active');
    $(this).addClass('active');
    return false;
  });
  
  // figure out how many widgets there are
  // always end with a black one
  var numWidgets = $("#sidebar .widget").length;
  if (numWidgets % 2 == 0) {
    // odd number of widgets
    $("#sidebar .widget:odd").addClass('odd');
  } else {
    // even number of widgets
    $("#sidebar .widget:even").addClass('odd');
  }
  
  // Save Form Field Prompts 
	// If you have form fields with default values like "Enter search term"
	// this code will save that value, hide it when a user focuses on the field
	// and restores it when they leave the field if they didn't enter anything.
	
	// Select the fields you want to save
    var $fields = $(".search-form #s");
    
    // No need to change anything below here
    $fields.each(function(){
		$(this)
			.data("val",$(this).val()) 	// save the initial value
			.focus(function(){
				$(this).val(''); 		// clear the field on focus
			})
			.blur(function(){
				// reset the field to the original value if the user didn't input anything
				if ($(this).val()=='') {
	    			$(this).val($(this).data("val")); 
	    		}
			});
    });
  
  $('.body .toggle')
    .wrapInner('<div class="body"></div>')
    .append('<a class="togglecontrol">Learn More</a>');
    
  $('.togglecontrol').click(function(){
    if ($(this).hasClass('open')) {
      $(this).html('Learn More');
    } else {
      $(this).html('Close');
    }
    
    $(this)
      .siblings('.body').slideToggle()
      .end()
      .toggleClass('open');
  });
  
  $("#sidebar h3:contains('Visit the wire')").html('Visit the w<span>i</span>re');
  
  // hide jump to top link if the page is short
  var height = $(document).height();
  if (height < 1600 && $(window).height() < 1600) {
    $('.jumptotop').hide();
  }
}); 

