
var $cameFrom;
var $inviteClone;

jQuery(function($) {
	
	// Init the form fields
	$('#invite, #login').initInputs().hide();
	
	// Show the "Login" button
	$('#login_link').show();
	
	// If the user submitted a form (most likely the login form) and got redirected
	// here with errors, we should show that form by default
	if ($('#errorExplanation').length)
		$('#errorExplanation').closest('form').show().siblings().hide();
	
	
	// Give action to the "Login" link
	$('#login_link').click(function (event) {
	
		event.preventDefault();
		
		$('#login').bringIn();
	
	});
	
	// Give some action to the "Request an Invitation" link
	$('#request_invite').click(function (event) {
	
		event.preventDefault();
		
		// Bail here if we're already at the request form
		// or store it for the cancel functionality
		$cameFrom = $('#topright > :visible');
		if ($cameFrom.get(0) == $('#invite').get(0)) return false;
		
		$('#invite').bringIn();
	
	});
	
	
	// Give functionality to the cancel invite link
	$('#cancel_invite').click(function (event) {
	
		event.preventDefault();
		
		$cameFrom.bringIn();
	
	});
	
	
	// AJAX the request invite form
	$('#invite').submit(function () {
	
		// Show loading type stuff
		$('#invite').append('<img src="/images/splash/ajax-loader.gif" alt="Loading..." />')
		.children('img').centerStage();
		
		$('#invite').children(':not(img, p, a)').css('opacity', .5).attr('disabled', true);
		
		// Gather inputs
		var $email = $('#invitation_email');
		var $website = $('#invitation_website');
		var $message = $('#invitation_message');
	
		// Send AJAX request
		$.ajax({
			type: "POST",
			url: $(this).attr('action') + '.xml',
			dataType: "xml",
			data: {
				'invitation[email]': $email.val(),
				'invitation[website]': $website.val(),
				'invitation[message]': $message.val()
			},
			complete: function (data, textStatus) {

				// Remove loading type stuff
				$('#invite').children('img').remove();
				$('#invite').children().css('opacity', 1).removeAttr('disabled');

				// Style the paragraph to look like an error
				$('#invite > p').removeAttr('class').addClass(textStatus)
				
				// and fill it with the error message
				.text($(data.responseXML).find('response').text());
				
				// Mark the email address as error-ridden
				if (textStatus == 'error') {
					
					$email.css('border-color', 'red');
					
				} else {
				
					// Do sucess stuff here
					$('#invite').children(':not(p)').remove();
					
					// Change the text of the cancel link to close
					$('#invite').append('<a href="#">Close</a>').click(function (event) {
					
						event.preventDefault();
					
						$('#invite').replaceWith($inviteClone.clone(true));
						$('#invite').initInputs();
		
						$cameFrom.bringIn();
						
					});
				
				}

			}
		});
		
		
		// Stop the page from reloading
		return false;
		
	});
	
	// Clone the invite form
	$inviteClone = $('#invite').clone(true);
		
	
});


(function ($) {	

	$.fn.bringIn = function () {
	
		// Hide the old content
		$('#topright').children().hide();
		
		// Show the new content
		$(this).show();
		
		return $(this);
	
	}


	$.fn.initInputs = function () {
		
		$(this).find(":input.defaultvalue").each(function() {
			
			if ($(this).attr("value")) {
				$(this).val($(this).attr("value"));
				$(this).css("color", "#a1a1a1").data("default", $(this).attr("value"))
				$(this).focus(function() {
					if ($(this).attr("value") == $(this).data("default") || $(this).attr("value") == "") {
						$(this).attr("value", "")
							.css("color", "#333");
					}
				}).blur(function() {
					if ($(this).attr("value") == $(this).data("default") || $(this).attr("value") == "") {
						$(this).css("color", "#888")
							.attr("value", $(this).data("default"));
					}
				});
			}
			
		});
		
		return $(this);
		
	}
	
	// Centers all elements passed vertically and horizontally
	$.fn.centerStage = function (bindResize) {
		
		$actors = $(this);
		
		$actors.each(function () {
			
			if ($(this).is(':hidden')) return true;
			
			$(this).css({
				top: ($(this).parent().height() / 2) - ($(this).outerHeight() / 2),
				left: ($(this).parent().width() / 2) - ($(this).outerWidth() / 2)
			});
		});
		
		if (typeof(bindResize) == 'undefined') bindResize = true;
		
		if (bindResize) {
		
			$actors.parent().resize(function () {
			
				$actors.centerStage(false);
			
			});
		
		}
		
		return $(this);
		
	}

})(jQuery);
