$(document).ready(function() {
	// 'Book all'
	function book_all_ticked(wholeWeekTickbox) {
		if ($(wholeWeekTickbox).is(':checked')) {
			$(wholeWeekTickbox).parents('tr').find('td').not('.whole_week.ticket, .good_friday, .easter_monday').find('input:checkbox').attr('checked', 'checked');
		} else {
			$(wholeWeekTickbox).parents('tr').find('td').not('.whole_week.ticket').find('input:checkbox').removeAttr('checked');
		}
		
		$(wholeWeekTickbox).parents('tbody').find('.whole_week.am_club input:checkbox, .whole_week.pm_club input:checkbox').change();
		ccp_check($(wholeWeekTickbox).parents('table'));
		refresh_am_pm_clubs($(wholeWeekTickbox).parents('table'));
		grey_out_disabled_labels();
	}

	$('td.whole_week.ticket input:checkbox').change(function() {
		book_all_ticked(this);
	});
	
	// 'Select all'
	$('.whole_week.am_club input:checkbox, .whole_week.pm_club input:checkbox').change(function() {
		if ($(this).is(':checked')) {
			$(this).parents('tr').find('td').not('.am_club.ticket, .pm_club.ticket, .good_friday, .easter_monday').find('input:checkbox').not(':disabled').attr('checked', 'checked');
		} else {
			$(this).parents('tr').find('td').not('.am_club.ticket, .pm_club.ticket, .good_friday, .easter_monday').find('input:checkbox').removeAttr('checked');
		}
		
		grey_out_disabled_labels();
	});
	
	// Day ticket
	$('.day.ticket input:checkbox').change(function() {
		if ($(this).is(':checked')) {
			if ($(this).parents('tr').find('.day.ticket').not('.good_friday, .easter_monday').find('input:checkbox').not(':checked').length == 0) {
				$(this).parents('tr').find('.whole_week.ticket input:checkbox').attr('checked', 'checked');
				$(this).parents('tr').find('.whole_week.ticket input:checkbox').change();
			}
		} else {
			$(this).parents('tr').find('.day.ticket input:checkbox:disabled').removeAttr('checked');
			$(this).parents('tr').find('.whole_week.ticket input:checkbox').removeAttr('checked');	
		}
		
		refresh_am_pm_clubs($(this).parents('table'));
		ccp_check($(this).parents('table'));
		grey_out_disabled_labels();
	});

	$('body:not(.bookings.update):not(.bookings.admin_update):not(.bookings.admin_remove) td.ticket input:checkbox').change(function() {
		update_skill_builder($(this).parents('table'));
	});

	// Init
	$('div.week table').each(function() {
		refresh_am_pm_clubs($(this));
		ccp_check($(this));
		
		// Only run the following for new bookings
		if ($('body:not(.bookings.update):not(.bookings.admin_update):not(.bookings.admin_remove)').length > 0) {
			update_skill_builder($(this));
		}
	});
	
	grey_out_disabled_labels();

	// Grey out everything on the old booking viewing screen
	$('body.bookings.admin_view input, body.bookings.admin_view select').attr('disabled', 'disabled');
});

function ccp_check(ticket_table) {
	if ($(ticket_table).find('.day.ticket input:checkbox:checked').length > 0) {
		$(ticket_table).parents('div.week').find('div.skill select').removeAttr('disabled');
		$(ticket_table).parents('div.week').find('div.cpp input').removeAttr('disabled');
	} else {
		$(ticket_table).parents('div.week').find('div.skill select').attr('disabled', 'disabled').removeAttr('checked');
		$(ticket_table).parents('div.week').find('div.cpp input').attr('disabled', 'disabled').removeAttr('checked');
	}
}

function grey_out_disabled_labels() {
	$('input:checkbox').each(function() {
		if ($(this).is(':disabled')) {
			$(this).siblings('label').addClass('disabled');
		} else {
			$(this).siblings('label').removeClass('disabled');
		}
	});
}

function refresh_am_pm_clubs(ticket_table) {
	// Run through each of the book row's tds
	ticket_table.find('tr').eq(1).find('td').not('.whole_week').each(function() {
		var cur_checkbox_row_1 = ticket_table.find('tr').eq(2).find('td').eq($(this).index()).find('input[type=checkbox]');
		var cur_checkbox_row_2 = ticket_table.find('tr').eq(3).find('td').eq($(this).index()).find('input[type=checkbox]');

		if ($(this).find('input[type=checkbox]').is(':checked')) {            
			cur_checkbox_row_1.add(cur_checkbox_row_2).removeAttr('disabled').siblings('label').removeClass('disabled');
		} else {
			cur_checkbox_row_1.add(cur_checkbox_row_2).attr('disabled', 'disabled').siblings('label').addClass('disabled');
			cur_checkbox_row_1.add(cur_checkbox_row_2).removeAttr('checked');
		}
	});

	// Fully paid bookings can't have their clubs altered by the customer.
	$('.fully_paid .am_club.checkbox input[type=checkbox]').attr('disabled', 'disabled');
	$('.fully_paid .pm_club.checkbox input[type=checkbox]').attr('disabled', 'disabled');
}

function update_skill_builder(cur_table) {
	// When viewing old bookings, you can't change the skills
	if ($('body').hasClass('admin_view') == true) {
		return;
	}

	campWeek = $(cur_table).attr('id');
	campWeek = campWeek.substr(10);
	bookingWeek = $(cur_table).parents('div.week').attr('data-booking-week-id');
	customerDayCombo = 0;
	var skillDropdownToChange = $(cur_table).parents('div.week').find('.skill.select select');
	var cur_val = skillDropdownToChange.val();

	if ($(cur_table).find('tr').eq(1).find('td.monday input:checkbox').is(':checked') == true) {
		customerDayCombo += 16;
	}

	if ($(cur_table).find('tr').eq(1).find('td.tuesday input:checkbox').is(':checked') == true) {
		customerDayCombo += 8;
	}

	if ($(cur_table).find('tr').eq(1).find('td.wednesday input:checkbox').is(':checked') == true) {
		customerDayCombo += 4;
	}

	if ($(cur_table).find('tr').eq(1).find('td.thursday input:checkbox').is(':checked') == true) {
		customerDayCombo += 2;
	}

	if ($(cur_table).find('tr').eq(1).find('td.friday input:checkbox').is(':checked') == true) {
		customerDayCombo += 1;
	}

	if (bookingWeek != undefined && allBookingWeekSkills != undefined) {
		skills = allBookingWeekSkills[bookingWeek][customerDayCombo];
	} else {
		skills = allCampWeekSkills[campWeek][customerDayCombo];
	}

	options = '<option value="">None</option>';

	$.each(skills, function(skillID, skillName) {
		options += '<option value="' + skillID + '">' + skillName + '</option>';
	});

	skillDropdownToChange.html(options);
	skillDropdownToChange.val(cur_val);
}

