$(document).ready(function() {	
	// Expands the login tab
	$("#login_expand").click(function() {
		$("#login_content").slideDown("slow");
		$(this).hide();
		$("#login_close").show();
	});
	
	// Closes the login tab
	$("#login_close").click(function() {
		$("#login_content").slideUp("slow");
		$(this).hide();
		$("#login_expand").show();
	});
	
	// Expands/Closes the login tab
	$("#login_lnk").click(function() {
		var login_expand = $("#login_expand");
		var login_close = $("#login_close");
		if (login_expand.css("display") == "block") {
			$("#login_content").slideDown("slow");
			login_expand.hide();
			login_close.show();
		}
		else {
			$("#login_content").slideUp("slow");
			login_close.hide();
			login_expand.show();
		}
	});
	
	//$("#register_lnk").overlay({effect: 'apple', target: '#register'});
	$("#register_lnk").click(function(){window.location = '/index.php/register/';});
	$("#fp_lnk").overlay({effect: 'apple', target: '#fp'});	
	
	// Generate tooltips
	jQuery.each($('.help'), function() {
		// The tipSelector is the id minus '_help'
		tipSelector = $(this).attr('id');
		tipSelector = '#' + tipSelector.substring(0, tipSelector.length-5) + '_tip';
		$(this).tooltip({
			tip: tipSelector,
			direction: 'left',
			effect: 'slide',
			offset: [0, 20],
			position: "center right",
			relative: true,
			bounce: true,
			lazy: true
		});
	});
	
	// Bind to the form's submit event 
	$('#login_frm').submit(function() { 
		// Bind ajax submit
		$(this).ajaxSubmit({ 
			beforeSubmit:	loginRequest,   // pre-submit callback 
			success:		loginResponse,  // post-submit callback
			dataType:		'json'     // 'xml', 'script', or 'json' (expected server response type)
		}); 
				
		// !!! Important !!! 
		// always return false to prevent standard browser submit and page navigation 
		return false; 
	});
	
	// Bind to the form's submit event 
	$('#fp_frm').submit(function() { 
		// Bind ajax submit
		$(this).ajaxSubmit({ 
			beforeSubmit:	fpRequest,   // pre-submit callback 
			success:		fpResponse,  // post-submit callback
			dataType:		'json'     // 'xml', 'script', or 'json' (expected server response type)
		}); 
				
		// !!! Important !!! 
		// always return false to prevent standard browser submit and page navigation 
		return false; 
	});
	
	// Bind to the form's submit event 
	/*
	if($('#register_frm').length > 0)
	{
		$('#register_frm').submit(function() { 
			// Bind ajax submit
			$(this).ajaxSubmit({ 
				beforeSubmit:	registerRequest,   // pre-submit callback 
				success:		registerResponse,  // post-submit callback
				dataType:		'json'     // 'xml', 'script', or 'json' (expected server response type)
			}); 
					
			// !!! Important !!! 
			// always return false to prevent standard browser submit and page navigation 
			return false; 
		});
	}
	*/
	
});

/**
 * Pre-submit callback 
 * 
 * @param	Object	formData
 * @param	Object	jqForm
 * @param	Object	options
 */
function loginRequest(formData, jqForm, options) {	

	// Hide the success
	$('#login_response_wrapper').hide();
	
	// Show the loading status
	$('#login_loading').show();
	
	return true; 
} 


/**
 * Post-submit callback 
 * 
 * @param	Object	responseText
 * @param	Object	statusText
 */
function loginResponse(response, status) { 
	// Hide the loading status
	$('#login_loading').hide();
	
	// Successful request?
	if (status == "success") {				
		if (response.success) {			
			// Redirect
			window.location = response.success_url;
		}
		else {
			$('#login_response').html(response.details);
			$('#login_response_wrapper').show();
		}
	}
}

/**
 * Pre-submit callback 
 * 
 * @param	Object	formData
 * @param	Object	jqForm
 * @param	Object	options
 */
function fpRequest(formData, jqForm, options) {	

	// Hide the success and error
	$('#fp .response .success').hide();
	$('#fp .response .error').hide();
	
	// Show the loading status
	$('#fp .response .loading').show();
	
	return true; 
} 


/**
 * Post-submit callback 
 * 
 * @param	Object	responseText
 * @param	Object	statusText
 */
function fpResponse(response, status) { 
	// Hide the loading status
	$('#fp .response .loading').hide();
	
	// Successful request?
	if (status == "success") {
		if (response.success) {
			$('#fp .response .success').show();
		}				
		else {
			$('#fp .response .error').show();
		}
	}
}

/**
 * Pre-submit callback 
 * 
 * @param	Object	formData
 * @param	Object	jqForm
 * @param	Object	options
 */
 /*
function registerRequest(formData, jqForm, options) {	

	// Hide the success/error
	$('#register .response .success').hide();
	$('#register .response .error').hide();
	
	jQuery.each($('#register .field .details'), function() {
		$(this).hide();
	});
		
	// Show the loading status
	$('#register .response .loading').show();
	
	return true; 
} 

*/
/**
 * Post-submit callback 
 * 
 * @param	Object	responseText
 * @param	Object	statusText
 */
 /*
function registerResponse(response, status) { 
	// Hide the loading status
	$('#register .response .loading').hide();
	
	// Successful request?
	if (status == "success") {
		if (response.success) {
			$('#register .response .success').show();
			// Clear the form
			$('#register_frm input').each(function() {$(this).val('');});
		}
		else {
			// Display the errors
			$('#register .response .error').show();
			jQuery.each(response.errors, function(key, val) {
				$('#register_'+key+'_details').html(val).show();
			});
		}		
		
	}
}
*/


