$(document).ready( function() {
	var mouseInIconBox = false;
	// Print page button
	$('#printPage').click( function() {
		window.print();
		return false;
	});
	// Slide out behaviour
	$(document).ready( function () {
		$('.socialIconsBox').hide();
		$('.socialIcons').hover( function() {
			if(!$('.socialIconsBox:visible').length) {
				$('.socialIconsBox').show('slide', {
					direction: 'right'
				}, 500);
			}
		}, function() {
			setTimeout( function() {
				if(!mouseInIconBox) {
					$('.socialIconsBox').hide('slide', {
						direction: 'right'
					}, 500)
				}
			},100);
		});
		$('.socialIconsBox').hover( function() {
			mouseInIconBox = true;
		}, function() {
			mouseInIconBox = false;
			$('.socialIconsBox').hide('slide', {
				direction: 'right'
			}, 500)
		});
	});
	// Bookmark button
	$('#bookmarkPage').click( function() {
		bookmarksite($('title').text(), document.URL);
		return false;
	});
	// Popup email form
	openDialog = function(w,h,t,m,e) {
		jQuery.fn.dialog.open({
			title: t,
			element: e,
			width: w,
			modal: m,
			height: h
		});
	}
	$('#emailPage').click( function() {
		$('#dialogForm').html($('#emailPageFormWrapper').html());
		$('#dialogForm form').addClass('dynamicForm');
		$('.dynamicForm #yourEmail').val(null);
		$('.dynamicForm #theirEmail').val(null);
		$('.dynamicForm #notes').html(null);
		openDialog('300','250','Email a friend',false,'#dialogForm .emailPageForm');
		$('.dynamicForm').validate();
	});
	$('.dynamicForm .submit').live('click', function() {
		// validation is automatic
		$.ajax({
			url: CCM_TOOLS_PATH + '/emailpage',
			data: $('.dynamicForm').serialize(),
			success: function(result) {
				if (result == 'sent') {
					$('.dynamicForm .result').addClass('sent');
					$('.dynamicForm .result').html('Thank you.<br />A link to this page has been sent to your friend.<br /><br /><a href="#" class="jbx-dialog-close" title="Close this window">Close this window</a>');
				} else {
					$('.dynamicForm .result').addClass('failed');
					$('.dynamicForm .result').html('Unfortunately, something seems to have gone wrong.<br />Please try again later.<br /><br /><pre>' + result + '</pre><br /><br /><a href="#" class="ccm-dialog-close" title="Close this window">Close this window</a>');
				}
			}
		});

		return false;
	});
	$('.jbx-dialog-close').live('click', function() {
		$('.dynamicForm #cancel').trigger('click');
	});
});

