$(document).ready(function(){

// Toggle the drawer when a subscribe link is clicked
$("a.sub").click(function () {
	  toggleDrawer();
});

/*$('#breadcrumbs').stop().fadeTo("fast", 0.4);*/

if (!jQuery.support.leadingWhitespace){
//This tidies things in IE
$( "div.cover img" ).each(function(index){
	var top = ( 229 - $(this).height()) / 2;
	$(this).css('margin-top', top);
});

$("a.footnote").html("^");
}

// Move article images up on hover
$(".cover").mouseover(function(){
      	$(this).stop().animate({ 
		     marginTop: "-10px",
		     paddingBottom: "10px"
		 }, 200 );
    }).mouseout(function(){
      	$(this).stop().animate({ 
		     marginTop: "0px",
		     paddingBottom: "0px"
		 }, 200 );
});

// Dropcap the first letter of articles
//p1 = $("#copy p:first");
//p1.addClass("intro");
$('#copy p:first').fancyletter();



// Fade nav elements into the background when scrolling
$(window).scroll(function(){
	$('nav#main').stop(); 
  if($(window).scrollTop() <= 100){ 
	    /*$('nav#main').stop().animate({ 
		     marginTop: "0px",
		 }, 500 ).fadeTo("normal", 1.0);*/
		
		$('nav#main').stop().fadeTo("normal", 1.0);
		$('nav#next').stop().fadeTo("slow", 1.0);
		$('nav#previous').stop().fadeTo("slow", 1.0);
				
  } else {
  		/*$('nav#main').stop().fadeTo("slow", 0.35).animate({ 
		     marginTop: "-45px",
		 }, 800 );*/
		$('nav#main').stop().fadeTo("slow", 0.35);		
		$('nav#next').stop().fadeTo("slow", 0.55);
		$('nav#previous').stop().fadeTo("slow", 0.55);
		
		
 }


});

// Fade nav elements back up if moused over when they're faded out 

$('nav#next').mouseover(function(){
      	$('nav#next').stop().fadeTo("fast", 1.0);
});

$('nav#previous').mouseover(function(){
      	$('nav#previous').stop().fadeTo("fast", 1.0);
});

$('nav#main').mouseover(function(){
      	$('nav#main').stop().fadeTo("fast", 1.0);
});


$('nav#next').mouseout(function(){
	if($(window).scrollTop() > 100){ 
      	$('nav#next').stop().fadeTo("fast", 0.55);
    }
});

$('nav#previous').mouseout(function(){
	if($(window).scrollTop() > 100){ 
      	$('nav#previous').stop().fadeTo("fast", 0.55);
	}
});


$('nav#main').mouseout(function(){
	if($(window).scrollTop() > 100){ 
      	$('nav#main').stop().fadeTo("fast", 0.35);
	}
});

/*
$('#breadcrumbs').mouseover(function(){
	$('#breadcrumbs').stop().fadeTo("fast", 1.0);
});

$('#breadcrumbs').mouseout(function(){
	$('#breadcrumbs').stop().fadeTo("slow", 0.4);
});
*/


// Submit subscriber's email address to MailChimp via ajax
$(function()
{
	$(":submit").click(function() {	
		
		// First, disable the form from submitting
		$('form').submit(function() { return false; });
		
		// Validate email address with regex
		if (!checkEmail()) 
		{
			alert("Please enter a valid email address");
			return;
		}
		
		// Serialize form values to be submitted with POST
		var str = $("form").serialize();		
		
		// Add form action to end of serialized data
		final = str;
		
		// Submit the form via ajax
		$.ajax({
			url: "http://putthingsoff.com/lib/chimp.php",
			type: "POST",
			data: final,
			success: function(html){
				if (html == "1"){
					$("#theForm").hide(); // If successfully submitted hides the form
					$("#email").hide();
					$("#confirmation").fadeIn("slow");  // Shows "Thanks for subscribing" div
			    } else if (html == "214"){ // They've already subscribed
			    $("#theForm").hide(); 
				$("#email").hide();
				/*$("#confirmation").html("It looks like you've already subscribed.");*/
				$("#confirmation").fadeIn("slow");  // Shows "Thanks for subscribing" div
				} else { // Otherwise...
			    $("#theForm").hide(); 
				$("#email").hide();
				$("#confirmation").html("Uh oh. Something went wrong.");
		   }
		 }	
		});
	});
});
function checkEmail()
{	
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("#emailfield").val();
	return pattern.test(emailVal);
}

// Toggle the subscribe drawer
function toggleDrawer() {
      // ignore if already visible
      if ($("a#down").is('.active')) {
		 $("a#down").removeClass('active');
		 $("#subscribe-options").hide();
		 $("#subscribe-tray").animate({ 
		     height: "31px"
		 }, 200 );
		 $("#breadcrumbs").fadeIn("slow");		
		 $("#miniurl").fadeIn("slow");		
      } else {
		 $("#breadcrumbs").hide();
		 $("#miniurl").hide();
		 $("a#down").addClass('active'); //change the arrow link
		 $("#subscribe-tray").animate({ 
		     height: "81px"
		 }, 200 );
		 $("#subscribe-options").fadeIn("slow");
	  }
}

});

