$(function(){
	$('.header .sendafriend').click(function(){
		if($(this).hasClass('selected')){
			hideSendaFriend();
		}else{
			showSendaFriend();
		}
	});
	$('.header .sendafriend-speechballoon, .header .sendafriend').click(function(event){
		 event.stopPropagation();
	});
	
	$('.header .send').click(function(){
		var error = false;						  
		if($('#youremail').val() == "" || isEmail($('#youremail').val())){
			$('#youremail').closest('.field').addClass('error');
			error = true;
		}else{
			$('#youremail').closest('.field').removeClass('error');
		}
		
		if($('#friendsemail').val() == "" || isEmail($('#friendsemail').val())){
			$('#friendsemail').closest('.field').addClass('error');
			error = true;
		}else{
			$('#friendsemail').closest('.field').removeClass('error');
		}
		
		$('.sendafriend-speechballoon .content').height($('.sendafriend-speechballoon .content').height());
		
		if(!error){
			var youremail = $("input[name='youremail']").val();
			var friendsemail = $("input[name='friendsemail']").val();
			var url = $("input[name='url']").val();
			var pagetitle = $('.description .page').html();
			$.ajax({
				type: "POST",
				url: '/sendafriend/sendmail',
				data: ({
				url:url, 
				youremail:youremail, 
				friendsemail:friendsemail,
				pagetitle:pagetitle
				}),
				success: function(data) {	

				}
			});	
			
			$('.sendafriend-speechballoon .thanks span').html(friendsemail);
			$('.sending-overlay').fadeIn();
			$('.sendafriend-speechballoon .content .innercontent').animate({opacity:.1},{duration:500});
			setTimeout(showThanksMessage, 1000);
		}
	});
})


function showThanksMessage(){
			$('.sendafriend-speechballoon .content .innercontent').fadeOut();
			$('.sendafriend-speechballoon .thanks').fadeIn();
			$('.sendafriend-speechballoon .content').animate({height:'60px'}, {duration:600});	
			$('.sending-overlay').fadeOut();
}


function hideSendaFriend(){
		$('.header .sendafriend').removeClass('selected');
		$('.sendafriend-speechballoon').fadeOut();
		
}

function showSendaFriend(){
		$('.header .sendafriend').addClass('selected');
		$('.sendafriend-speechballoon').fadeIn();
		hideCountrySelect();
		hideLogin();
}

