/*

User Tools in right sidebar
 - Send to Friend
 - Print Page


*/



// To validate email addresses
var emailRegex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i ;

// Bind events on ready
$(document).ready(function() {
	
	
	$('#send_to_friend').click(function(){
		$('#send_to_friend_form').slideDown("slow");
	});
	
	$('#send_to_friend_button').click(function(){
		var from 	= $('#send_to_friend_from').val();
		var to   	= $('#send_to_friend_to').val();
		var fromName= $('#send_to_friend_from_name').val();
		var toName 	= $('#send_to_friend_to_name').val();
		
		// Validate
		if (!fromName) {
			return send_to_friend_error('Please enter your name');
		}
		if (!from || !emailRegex.test(from)) {
			return send_to_friend_error('Please enter your email address');
		}
		if (!toName) {
			return send_to_friend_error('Please enter your friend\'s name');
		}
		if (!to || !emailRegex.test(to)) {
			return send_to_friend_error('Please enter your friend\'s email address');
		}
		
		// Post to and from address and close box on success
		$.post(baseURL+'share/sendtofriend',
				{
					to			: to,
					to_name		: toName,
					from_name	: fromName,
					from		: from,
					url			: document.URL,
					title		: $('.content-body h1').text()
				},
				function(data, stat) {
					if (data.success) {
						send_to_friend_error('Your message was sent!');
						$('#send_to_friend_form').slideUp("slow", function(){
							$('#send_to_friend_error').css('visibility','hidden');
						});
					} else {
						send_to_friend_error('There was an error. Please try again.');
					}
				},
				'json'			
		);
	});
	
	$('#send_to_friend_close').click(function(){
		$('#send_to_friend_form').slideUp("slow", function(){
			$('#send_to_friend_error').css('visibility','hidden');
		});
	});	
	
	// Print page on click
	$('#print_page').click(function(){
		window.open(printLink, 'print', 'menubar=0,resizable=1,width=850,height=450,status=0');
	});

});

// Set error message for Send to Friend
function send_to_friend_error( message ) {
	$('#send_to_friend_error').text(message).css('visibility','visible');
}
