$(function () {
	$('.date').datepicker();
	$('.showee').hide();
	$('.shower').change(refreshVisibility);
	$('.shower').click(refreshVisibility);
	refreshVisibility();

	$('.repeater input').live('blur', function () {
		refreshRepeater();
	});

	$('ul.nav').superfish();
});

function refreshRepeater()
{
	// Clean up empty rows

	$('.repeater').find('tr').each(function () {
		// Is there an item after this TR?
		if (!$(this).next().next().length) return;
		// Should we remove this TR?
		window.remove = true
		// input elments on this row
		window.count = 0
		$(this).find('input').each(function () {
			window.count++;
			if ($(this).val().length) window.remove = false
		});
		if (window.remove && window.count > 0) $(this).remove();
	});

	// Append a new row if needed

	window.add = false
	$('.repeater').find('tr:last input').each(function () {
		if ($(this).val().length) window.add = true
	});

	if (window.add)
	{
		obj = $('.repeater').find('tr:last').clone()
		obj.find('input').val('');
		obj.appendTo('.repeater')
	}
}

function refreshVisibility()
{
	$('.showee').hide();
	$('.shower:checked').each(function () {
		$('#'+$(this).attr('id')+'-target').show();
	})
}
