// JQuery Actions
$(document).ready(function(){

	$("#loaderForm").hide();
	$("#onePersonForm").hide();
	$("#twoPersonForm").hide();
	$("#errorForm").hide();

	$(".rsvpName").focus();

	// Now do the submit buttons
	$(".submitResponse").hide();
	$(".submitResponse").before('<a class="submitResponseButton" onclick="submitRSVP();"></a>');
	$(".submitResponseButton").hover(
		function() { $(this).css('background-position', '0 100%'); },
		function() { $(this).css('background-position', '0 0'); }
	);

	$("#nextButton").hide();
	$("#nextButton").before('<a id="nextButtonLink" href="#" onclick="retrieveInvitation(); return false;"></a>');
	$("#nextButtonLink").hover(
		function() { $(this).css('background-position', '0 100%'); },
		function() { $(this).css('background-position', '0 0'); }
	);

	replaceRadioButton();

	// Should we skip this step
	if (myName && myName != "")
	{
		$("input[name=askName]").val(myName);
		retrieveInvitation();
	}
});

function replaceRadioButton()
{

	// Create the properties for the checkbox stuff
	$(".rsvpButtons input[type=radio]").each(function(i){

		// alert($(this).val());
		// alert($(this).attr('checked'));
		$(this).before('<div class="weddingCheckBox"></div>');
		$(this).hide();

		// Now add the hoverstuff, which is (of course) semi-dependent
		// on whether or not the item is checked. The format for the 
		// hover actions is  $(selector).hover(on(), off());
		var buttonSelector = ".rsvpButtons .weddingCheckBox:eq("+i+")";
		var radioSelector = ".rsvpButtons input[type=radio]:eq("+i+")";
		var labelSelector = ".rsvpButtons label:eq("+i+")";
		$(buttonSelector).css('background-position', ($(radioSelector).attr('checked') ? '0 0' : '100% 0'));
		$(labelSelector).hover(
			function () {
				// the roll-over button depends on which state thisi
				if ($(radioSelector).attr('checked'))
					$(buttonSelector).css('background-position', '0 100%');
				else
					$(buttonSelector).css('background-position', '100% 100%');

			}, 
			function () {
				// the roll-over button depends on which state thisi
				if ($(radioSelector).attr('checked'))
					$(buttonSelector).css('background-position', '0 0');
				else
					$(buttonSelector).css('background-position', '100% 0');
			}
		);

		$(labelSelector).bind('click', function(){
			var checkedState = $(radioSelector).attr('checked');
			$(radioSelector).attr('checked', !checkedState);
			resetButtonBgs();
			return false;
		});

	});

	$(".rsvpButtons img").each(function(i){
		$(this).css('margin-top', '8px');
	});


}

function resetButtonBgs()
{
	// Create the properties for the checkbox stuff
	$(".rsvpButtons input[type=radio]").each(function(i){
		var buttonSelector = ".rsvpButtons .weddingCheckBox:eq("+i+")";
		var radioSelector = ".rsvpButtons input[type=radio]:eq("+i+")";
		if ($(radioSelector).attr('checked'))			
		{
			$(buttonSelector).css('background-position', '0 0');
		}
		else
		{
			$(buttonSelector).css('background-position', '100% 0');
		}
	});
}

function retrieveInvitation()
{
	var myName = $("input[name=askName]").val();
	if (myName == '')
	{
		alert('Sorry, but you must enter something for a name. Anything. Please.');
	}
	else
	{
		$("#askForm").hide('slow', function(){$("#loaderForm").show('slow', function(){
				// Send the AJAX request, then deal with the results
				$.get('/index.php?moduleId=hdgye99s&verify='+myName, function(data){

					// First, see if there is an error, if there is, we'll send them
					// somewhere.
					var errorCode = $("error", data).text();
					if (errorCode && errorCode != '')
					{
						$("#loaderForm").hide('slow', function(){
							$("#errorForm").show('slow', function(){
								// Add the event handlers for the links
								$("#contactUsLink").click(function(){window.location.href="http://www.muah.ca/contact"; return false;});	
								$("#tryAgainLink").click(function(){
									$("#errorForm").hide('slow', function(){
										$("#askForm").show('slow');
									});
									return false;
								});	
							});
						});

					}
					else
					{
						// We know it's a valid invitee, so let's proceed to the results page
						var inviteeName =  $("invitee", data).text();
						var guestName =  $("guest", data).text();

						// And let's make sure we set the invitee id
						var inviteeId = $("id", data).text();
						$("#inviteeId").val(inviteeId);

						// If there is, indeed a second person, show the two-person form. 
						// Otherwise show the one-person form.
						$("#loaderForm").hide('slow', function() {
							if (guestName)
							{
								$("#guestName").attr('value', guestName);
								$("#inviteeName2").attr('value', inviteeName);
								$("#twoPersonForm").show('slow');								
							}
							else
							{
								$("#inviteeName").attr('value', inviteeName);
								$("#onePersonForm").show('slow');
							}
						});
					}
				}, 'xml');
		});});
	}
}


function submitRSVP()
{
	// First, determine which of the forms we have
	var twoPeople = $("#twoPersonForm").css('display') == 'block';
	var onePerson = $("#onePersonForm").css('display') == 'block';

	// Get the id of the invitee
	var inviteeId = $("#inviteeId").val();

	// Get the names of the invitee (and guest?)
	if (onePerson)
	{
		var myRequestObject = {
			"id" : $("#inviteeId").val(),
			"invitee" :  $("#inviteeName").val(),
			"inviteeAttending" : $(".rsvpButtons input[name=rsvp]:checked").val()
		};
	}
	else
	{
		var myRequestObject = {
			"id" : $("#inviteeId").val(),
			"invitee" :  $("#inviteeName2").val(),
			"guest" :  $("#guestName").val(),
			"inviteeAttending" : $(".rsvpButtons input[name=rsvp2]:checked").val(),
			"guestAttending" : $(".rsvpButtons input[name=rsvp2]:checked").val()
		};
	}

	// Send the AJAX request, then deal with the results
	$("#twoPersonForm:visible,#onePersonForm:visible").hide('slow', function(){$("#loaderForm").show('slow', function(){

		$.post('/index.php?moduleId=hdgye99s&respond=1', myRequestObject, function(data){
			var statusMessage = $("status", data).text();
			if (statusMessage == "success")
			{
				window.location.href="http://www.muah.ca/rsvpThanks";
			}
			else
			{
				$("#loaderForm").hide('slow', function(){
					$("#errorForm").show('slow', function(){
						// Add the event handlers for the links
						$("#contactUsLink").click(function(){window.location.href="http://www.muah.ca/contact"; return false;});	
						$("#tryAgainLink").click(function(){
							$("#errorForm").hide('slow', function(){
								$("#askForm").show('slow');
							});
							return false;
						});	
					});
				});
			}
		}, 'xml');

	});});

}
