$(document).ready(function() {

	$("#newspic").click(function() {

		if($('#newsletter').is(":hidden")){
			$('#newsletterSubmit').removeAttr('disabled');
			$('span').remove();
			$('#newsletterForm').show();
			$('#newsletter').slideDown('slow');
			$('#name').focus();
		} else {
			$('#newsletter').slideUp('slow');
		}
	});
	
	$('#newsletterSubmit').click(function() {
		
		// Some validation
		if($('#name').val() == ''){
			alert('You have to fill in your name');
			$('#name').focus();
			return false;
		} 
		
		if ($('#email').val() == '') {
			alert('You have to fill in your email');
			$('#email').focus();
			return false;
		} 
		
		
		email = $('#email').val();
		at=email.indexOf("@");
		lastat=email.lastIndexOf("@");
		dot=email.lastIndexOf(".");
		
		if( at<1 || at!=lastat || dot<at){
			alert("Your email address is not correct. Please try again.");
			$('#email').focus();
			return false;
		}
		
		if($('#country').val() == ''){
			alert('You have to fill in what country you\'re from');
			$('#country').focus();
			return false;
		} 
		
		// Disable submit-button
		$(this).attr('disabled', 'disabled');
		
		// Ajax post to mail right now but later with cURL to Apsis.se
		$.post('wp-content/themes/Equilibrium/newsletter/subscribe.php', $('#newsletterForm').serialize(), function(data){
			$('#newsletterForm').hide();
			$('#newsletter').append('<span class="newsThanks">Thanks for your subscribtion</span>');
			
			setTimeout(function(){ $('#newsletter').slideUp('slow') }, 2000)
		});
		return false;
	});

});