$(document).ready(function ()
{
	// Hide all dropdown boxes + paticipants text
	$('select.PersonSelect').hide();

	// Display dropdown when a selection is made
	$(":radio:checked, :checkbox:checked").parent().parent().children().children("select").css('border', '2px solid green').show();
	
	// Show/hide the selectboxes of the three grouped checkboxes
	$('input[type=\'checkbox\']').click(function ()
	{
		if ($(this).attr('checked'))
		{
			$(this).parent().parent().children().children("select").show();
		}
		else
		{
			$(this).parent().parent().children().children("select").hide();
		}
	});
	
	// Show dropdown next to the radio button selected trip
	$('input[name$=\'2010\']').click(function ()
	{
		var rel = $(this).attr('rel');
		var relAlt = rel.substring(0, rel.length - 2);

		// RegExp if the last charater is 1
		if (rel.match(/1$/)) 	// First option is selected
		{
			$('#' + rel).show();
			$('#' + relAlt + "-2").hide();
		}
		else if (rel.match(/2$/))	// Second option is selected
		{
			$('#' + rel).show();
			$('#' + relAlt + "-1").hide();
		}
		else if (rel.match(/3$/))
		{
			$('#' + relAlt + "-3").show(); // Third option is selected
		}
		
		// Hide selection for checkboxes that aren't checked
		if ($(this).attr('type') == 'checkbox' && !$(this).attr('checked'))
		{
			$('#' + rel).hide();
		}
	});

	// Hide dropdown next to a checkbox
	$('#FridayTheHague').click(function ()
	{
		if (!$(this).attr('checked'))
		{
			$('#' + $(this).attr('name')).hide();
		}
	});

	// Return from callback disable the right checkboxes
	if ($('#TheHague').attr('checked'))
	{
		$('#HeartOfHolland').attr('disabled', 'disabled');
	};
	if ($('#HeartOfHolland').attr('checked')) 
	{
		$('#TheHague').attr('disabled', 'disabled');
	}

	// Switch for a custom selection of three checkboxes
	$('#TheHague').click(function ()
	{
		if ($('#TheHague').attr('checked'))
		{
			$('#HeartOfHolland').attr('disabled', 'disabled');
		}
		else
		{
			$('#HeartOfHolland').removeAttr('disabled');
		}

		if (!$('#HeartOfHolland').attr('checked') && !$('#TheHague').attr('checked') && !$('#WalkingDinnerThehague').attr('checked'))
		{
			$('#' + $(this).attr('name')).hide();
		}
	});
	$('#HeartOfHolland').click(function ()
	{
		if ($('#HeartOfHolland').attr('checked'))
		{
			$('#TheHague').attr('disabled', 'disabled');
		}
		else
		{
			$('#TheHague').removeAttr('disabled');
		}

		if (!$('#HeartOfHolland').attr('checked') && !$('#TheHague').attr('checked') && !$('#WalkingDinnerThehague').attr('checked'))
		{
			$('#' + $(this).attr('name')).hide();
		}
	});
	$('#WalkingDinnerThehague').click(function ()
	{
		if (!$('#HeartOfHolland').attr('checked') && !$('#TheHague').attr('checked') && !$('#WalkingDinnerThehague').attr('checked'))
		{
			$('#' + $(this).attr('name')).hide();
		}
	});
	// End switch

	// Reset the form with a confirm message
	$('#btnReset').click(function ()
	{
		if (confirm("Are you sure you wan't to reset the form?"))
		{
			$('#HeartOfHolland').removeAttr('disabled');
			$('#TheHague').removeAttr('disabled');
			$('select.PersonSelect').hide();
		}
		else
		{
			return false;
		}
	});

	// Dim overlay settings
	$('#dim').css('height', $(document).height());
	$('#dim').css('width', $(window).width());
	$('#dim').css('opacity', 0.8);

	// When the link that triggers the message is clicked fade in overlay/msgbox
	$('.DetailAnchor').click(function ()
	{
		loadContent($(this).attr('href'));
		$('#dim').fadeIn();
		$('.msgbox').fadeIn();
		return false;
	});
	
	// When user clicks on the dark background, fade out
	$('#dim').click(function ()
	{
		$('#dim').fadeOut();
		$('.msgbox').fadeOut();
		return false;
	});
	
	// Don't fade out when the user clicks the window
	$('.msgbox').click(function ()
	{
		return false;
	});
	
	// When the message box is closed, fade out
	$('.close').click(function ()
	{
		$('#dim').fadeOut();
		$('.msgbox').fadeOut();
		return false;
	});


	// Simple validation
	$('.okknop').click(function ()
	{
		var errorMessage = '';
		
		// Check for participants selection
		$(':radio:checked, :checkbox:checked').each(function () 
		{
			if($(this).parent().parent().children().children('select').val() == '')
			{
				errorMessage += '- You have to select atleast 1 person for ' + $(this).parent().children('a').html().toString().replace(/&amp;/g,'&') + '\n';
			}
		});
		
		// Check for terms acception
		if ($('#terms:checkbox:checked').val() == null)
		{
			errorMessage += '- You have to accept the general conditions\n';
		};
		
		// If errors show error messages
		if (errorMessage != '')
		{
			alert(errorMessage);
			return false;
		}
	});

	// Remove red borders on change
	$('select.PersonSelect').change(function ()
	{
		if($(this).val() != '')
		{
			$(this).css('border', '2px solid green');
		}
		else
		{
			$(this).css('border', '2px solid red');
		}
	});

});

// Adjust height of overlay to fill screen when viewport gets resized
$(window).bind('resize', function ()
{
   $('#dim').css('height', $(document).height());
   $('#dim').css('width', $(document).width());
});

// Load content with AJAX
function loadContent(link)
{
	$('#msgboxContent').load(link);
	// Hides the loader image behind the content
	$('#msgboxContent').css('height', '610px');
}
