$(function(){
	
	$.fn.emailLinkToAFriend = function(){
		
		// email a friend lightbox and form initialization
		var emailAFriendLink = $(this);
		emailAFriendLink.click(function(e) {
			e.preventDefault();
		});
		emailAFriendLink.colorbox({
			opacity: .5,
			scrolling: false,
			onComplete: function() {
				$.fn.colorbox.resize();
				//_gaq.push(['_trackPageview', $(this).val('action')]);
				$('#emailAFriendForm').submit(function(e) {
					e.preventDefault();
					var form = $(this);
					var formFields = form.find('ul.form-fields');
					form.hide();
					var loadingMsg = $('#cboxLoadingGraphic');
					loadingMsg.show();
					$.ajax({
						url: form.attr('action'),
						data: form.serialize(),
						complete: function(html) {
							loadingMsg.hide();
							
							if ($('.errorMsg', html.responseText).size() > 0) {
								var validatedForm = $(html.responseText).find('form ul.form-fields');
								formFields.after(validatedForm);
								formFields.remove();
							} else {
								var validatedForm = $(html.responseText).find('form');
								form.after(validatedForm);
								form.remove();
								$('button.submit').remove();
							}
							form.show();
							$.fn.colorbox.resize();
						}
					});
				});
			}
			, maxWidth: '100%'
		});
	};
})

