/**
 * Sets up the share form
 */
var share = {
	/**
	 * Cancel button reference
	 */
	cancel_button : null,
	/**
	 * Contains reference to the send button
	 * @var jQuery
	 */
	send_button : null,
	/**
	 * The share link
	 * @var jQuery
	 */
	share_button : null,
	/**
	 * Contains the div that has the video and forms
	 */
	video_container : null,
	/**
	 * The movie player object
	 */
	video_object : null,
	/**
	 * Name of the slim video wrapper
	 */
	wrapper_small_class : 'video-wrapper-small',
	/**
	 * Sets up the share form
	 */
	init : function () {
		/* Get the video container */
		share.video_container = $('div.video-container');
		share.video_object = share.video_container.html();
		/* Set up the share button */
		share.share_button = $('a.share');
		share.share_button.click(share.on_share_click);
	},
	/**
	 * Reacts to clicking of the cancel button on the share form
	 */
	on_cancel_click : function () {
		/* Update the HTML of the container element */
		$('div#video-object').css('display', 'block');
		$('div#share-container').remove();
		$('div#content, div.video-wrapper').removeClass(share.wrapper_small_class);
		share.send_button.unbind('click', share.on_send_click);
		share.send_button = null;
		share.cancel_button.unbind('click', share.on_cancel_click);
		share.cancel_button = null;
		return false;
	},
	/**
	 * Reacts to clicking of the send button on the share form
	 */
	on_send_click : function () {
		var data_to_send = {
			field_from : $('input[name=field_from]').val(),
			field_to : $('input[name=field_to]').val(),
			field_note : $('textarea[name=field_note]').val()
		};
		$.post(request_prefix+'reel/summer2009_share_process', data_to_send, share.on_send_response, 'html');
		return false;
	},
	/**
	 * Reacts to the response after submission of the form
	 */
	on_send_response : function (data, textStatus) {
		$('div#share-container').remove();
		share.video_container.append(data);
		share.video_container
			.css({'cursor' : 'hand', 'cursor' : 'pointer'})
			.click(share.on_send_response_click);
	},
	/**
	 * Reacts to the user clicking on the response div
	 */
	on_send_response_click : function () {
		$('div#video-object').css('display', 'block');
		$('div#share-container').remove();
		share.video_container
			.css({'cursor' : 'auto'})
			.unbind('click', share.on_send_response_click);
		$('div#content, div.video-wrapper').removeClass(share.wrapper_small_class);
	},
	/**
	 * Reacts to clicking of the share link
	 */
	on_share_click : function () {
		/* Load in the share form */
		$.get(request_prefix+'reel/summer2009_share_ajax', {}, share.on_share_load, 'html');
		return false;
	},
	/**
	 * Reacts to loading of the share form
	 */
	on_share_load : function (data, textStatus) {
		/* Set up the cancel and send buttons */
		$('div#video-object').css('display', 'none');
		share.video_container.find('div#share-container').remove();
		share.video_container.append(data);
		share.send_button = share.video_container.find('a.button-send');
		share.cancel_button = share.video_container.find('a.button-cancel');
		share.send_button.click(share.on_send_click);
		share.cancel_button.click(share.on_cancel_click);
		$('div#content, div.video-wrapper').addClass(share.wrapper_small_class);
		/* Set up focus and blur events on the form */
		var field_to = share.video_container.find('input[name=field_to]'),
			field_from = share.video_container.find('input[name=field_from]'),
			field_note = share.video_container.find('input[name=field_note]');
		field_from
			.focus(function () {
				if ($(this).val() == 'Your email address') {
					$(this).val('');
				}
			})
			.blur(function () {
				if ($(this).val() == '') {
					$(this).val('Your email address');
				}
			});
		field_to
			.focus(function () {
				if ($(this).val() == 'Friend\'s email address') {
					$(this).val('');
				}
			})
			.blur(function () {
				if ($(this).val() == '') {
					$(this).val('Friend\'s email address');
				}
			});
		field_from.val('Your email address');
		field_to.val('Friend\'s email address');
	}
}


/* Listen to page load */
$(function () {
	share.init();
})