/*
	js for the share panel
	Steve 10th August 09
	
	12th August
	- Utilized callback functions to perform smoother transition and effects
*/

$(document).ready(function() {
	$("#stf-done").hide();
	$("#stf-sending").hide();
	$("#stf-error").hide();
	$('#share_button_link_anchor').click(function() {
		sharePanelShow();
	});
	$('#share_button_link').click(function() {
		if ($('#share_panel').is(":hidden")) {
			sharePanelShow();
		} else {
			$("#share_right").fadeOut('normal', function() {
				$("#share_left").fadeOut('normal', function() {
					$('#share_panel').slideUp('normal', function() {
						$('#share_panel_wrapper').slideUp('slow');
					});
				});
			});
		}
	});
});

function sharePanelShow() {
	$('#share_panel_wrapper').hide().css({height: 200}).slideDown('slow', function() {
		$('#share_panel').slideDown("slow", function() {
			$("#share_left").fadeIn('slow', function() {
				$("#share_right").fadeIn('slow');
			});
		});
	});
}

function sendToFriend(url,title) {
	if($("#stf-email-friend").val() == '' || 
		$("#stf-email-friend").val() == 'Your friend\'s email address'
		|| !is_email($("#stf-email-friend").val())) {
			alert("Please check your friends email address");
			$("#stf-email-friend").select();
			return false;
	}
	if($("#stf-email-your").val() == '' || 
		$("#stf-email-your").val() == 'Your email address'
		|| !is_email($("#stf-email-your").val())) {
			alert("Please check your email address");
			$("#stf-email-your").select();
			return false;
	}
	var post_to = '/home/share?_frameset=true';
	var data = { 
		 ajax: 1,
		 m: 'send_to_friend',
		 c: 'share',
		 email_friend: $("#stf-email-friend").val(),
		 email_your: $("#stf-email-your").val(),
		 msg: $("#stf-message").val(),
		 url: url,
		 title: title
	}
	$("#stf-form").slideUp('normal', function() {
		$("#stf-sending").slideDown('normal', function() {
			$.post(post_to, data, function(response) {
				$("#stf-sending").slideUp('normal', function() {
					if (response == 'true') {
						$("#stf-done").slideDown();
					} else {
						$("#stf-error").slideDown().html(response);
					}
				});
			});
		});
	});
}

function sendAnother(div) {
	$("#stf-" + div).slideUp('fast', function() {
		$("#stf-email-friend").val("Your friend\'s email address").select();
		$("#stf-email-your").val("Your email address");
		$("#stf-message").val("Message");
		$("#stf-form").slideDown('normal');
	});
}

function is_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
   	return false;
   }
   else {
   	return true;
   }
}