/* == Lead Capture Form Javascript == */

/**
 * A LeadCapture object
 *
 * @param string saveCallbackFunction	a callback function or function name to utilise when saving the Lead Capture data
 * @param string closeCallbackFunction	a callback function (or function name) to utilise when closing the lead capture hover
 */
var LeadCapture = function( saveCallbackFunction, closeCallbackFunction )
{
	// Private properties
	var _saveCallbackFunction;
	var _closeCallbackFunction;


	/**
	 * Initialisation
	 *
	 * @param string saveCallbackFunction	a callback function or function name to utilise when saving the Lead Capture data
	 * @param string closeCallbackFunction	a callback function (or function name) to utilise when closing the lead capture hover
	 */
	this.init = function( saveCallbackFunction, closeCallbackFunction )
	{
		_saveCallbackFunction = saveCallbackFunction;
		_closeCallbackFunction = closeCallbackFunction;
	}


	/**
	 * Displays the hover for the lead capture form
	 */
	this.show = function()
	{
		var self = this;

		$('#hoverContainer').hide();

		$('#hoverContainer').load('/lead/hover/', function()
		{
			// Center the container using the jquery.center plugin
			// http://andreaslagerkvist.com/jquery/center/
			//$("#hoverContainer").center();
			centerPanel($("#hoverContainer"));

			// Hide the Role text field by default
			$(".yourRole").hide();

			// Fade in the container for the form
			$('#hoverContainer').fadeIn(1200);

			// On submit, save the data via an ajax submission if the form is valid.
			// Then use javascript to send the response to flash.
			$('#leadCapture').submit(function()
			{
				// If the form is valid, then save the data
				if( $('#leadCapture')[0].onsubmit() )
				{
					// Save the lead capture
					leadCapture.save();
				}

				// Return false so the form won't be posted
				return false;
			});

			// Ensure the close button closes the hover
			$("#btnClose").click( function()
			{
				self.close();
			});

			// Ensure the link to the terms of use hides the form and shows the terms of use
			$("#linkTermsOfUse").click( function()
			{
				self.showTerms('use');

				return false;
			});

			// Ensure the link to the terms of use hides the form and shows the terms of use
			$("#linkTermsAndConditions").click( function()
			{
				self.showTerms('competition');

				return false;
			});

			// Ensure the role field is shown/hidden for certain 'I am' (type) selections
			$("#typeId").change( function()
			{
				self.toggleRole($(this).val());
			});

			// Set the default visibility of the 'Role' field
			self.toggleRole($("#typeId").val());

			// Oninute tracking
			omnitureTracking.leadCapture();
		});
	}


	/**l
	 * Closes the hover of the lead capture form
	 */
	this.close = function()
	{
		$('#hoverContainer').fadeOut(800, function()
		{
			$('#hoverContainer').empty();
		});

		// Issue the callback function if exists
		if( typeof _closeCallbackFunction == 'function' )
		{
			_closeCallbackFunction();
		}

	}


	/**
	 * Save the referrals and then informs the flash that the action is completed.
	 */
	this.save = function()
	{
		$('#hoverContainer').fadeOut(800, function()
		{
			// Save the data using ajax
			var requestData = $('#leadCapture').serialize();

			$.post('/lead/ajax/', requestData, function(responseData)
			{
				if( typeof _saveCallbackFunction == 'function' )
				{
					_saveCallbackFunction(responseData);
				}
			}, 'json');
		});
	}


	/**
	 * Displays the terms of use within the lead capture hover
	 *
	 * @param string type	'competition'|'use'
	 */
	this.showTerms = function( type )
	{
		var self = this;

		$('#termsContainer').load('/lead/hover/terms/' + type, function()
		{
			// Fade out the form content
			$('form#leadCapture').fadeOut('slow', function()
			{
				// Fade in the container for the terms of use
				$('#termsContainer').fadeIn('slow', function()
				{
					// Make the terms content scrollable
					$('#termsContent').jScrollPane({scrollbarWidth:39, scrollbarMargin:10, topCapHeight: 16, bottomCapHeight: 16, dragMaxHeight: 52});

					// Cause the heading to be redrawn for the sake of IE6
					$('#termsContainer h2').css('margin-left', '0px');

					// Ensure the close button removes the terms and reveals the form again
					$("#btnCloseTermsOfUse, #btnCloseTermsAndConditions").click( function()
					{
						// Fade out the container for the terms of use
						$('#termsContainer').fadeOut('slow', function()
						{
							// Fade in the form content
							$('form#leadCapture').fadeIn('slow');
						});
					});
				});
			});
		});
	}

	/**
	 * Toggles the visibility of the 'Your Role' text field
	 *
	 * @param int type	value indicating the current Type selected in the form
	 */
	this.toggleRole = function( type )
	{
		switch( parseInt(type) )
		{
			case 1:		// Murdoch Staff
			case 3:		// Murdoch Alumni
			case 4:		// 18 Years +
			case 12:	// Other

				$(".yourRole").show();
				break;
		default:

				$(".yourRole").hide();
				break;
		}
	}


	// Initialise
	this.init(saveCallbackFunction, closeCallbackFunction);
}

