$(function() {
	// Hide rows and day groups that have been deselected using filter
	function runFilter() {
		// Reset visibility of all the entries
		$('.table-ui1 [data-day], .table-ui1 tr').show();

		// Hide any groups that have been deselected
		$('.date-choice input').not(':checked').each(function() {
			$('.table-ui1 [data-day="' + $(this).val() + '"]').hide();
		});

		// Hide rows that do not match the respective dropdown selector
		$('.table-select select').each(function() {
			var $this = $(this)
			  , val   = $this.val()
			  , name  = $this.attr('name');

			if (val == 'all') {return;}

			$('.table-ui1 [data-' + name + ']')
				.not('[data-' + name + '="' + val + '"]')
				.hide();
		});

		// Hide groups with no visible children
		var areClasses = false;

		$('.table-ui1 tbody').each(function() {
			var $this = $(this);

			if ($this.find('tr:visible').length > 0) {
				areClasses = true;
				return;
			}
			$this.parent()
				.find('[data-day="' + $this.data('day') + '"]')
				.hide();
		});

		if (!areClasses) {$(".no-classes").show();} else {$(".no-classes").hide();}

		// Manually colour table rows, as hiding some will have broken :nth-child
		$('.table-ui1 tbody tr').css('background', 'inherit');
		$('.table-ui1 tbody').each(function() {
			$(this).find('tr:visible:odd').css('background', '#f1f1f1');
		})

	}
	runFilter();

	$('.date-choice input').change(function() {runFilter();});

	$('.table-select select').change(function() {runFilter();})
});
