/*
 * Select All & (Filter + Stacked Availability Buttons)
 * On search results allow checkboxes for select all, buildings & suites.
 * Enable / disable buttons depending on num checkboxes checked.
 *
 * TODO: currently select all functionality is just on search results. if we
 *			 also want to use this elsewhere then we may want to functionize it.
 *
 */

/*
 * BUG (IE): IE doesn't trigger a change event immediately as a box is
 *           checked / unchecked. Appears that you have to check the box AND
 *           then click somewhere else on the page for the change event to trigger.
 *
 *           So we have to use the 'click' event for IE. The problem is that a
 *           click event can trigger when a checkbox is clicked but nothing's
 *           changed. In this instance I don't think it's a problem. Unsure
 *           if there are any accessibility issues with using 'click' event.
 *
 * SOLUTION:	--> previous: $(this).change(function() {
 * 				--> changeto: $(this).bind(($.browser.msie ? 'click' : 'change'), function () {
 */

$(document).ready(function(){

	/*
	 * Generic
	 */
	//$('input.select-all').change(function() {
	$('input.select-all').bind(($.browser.msie ? 'click' : 'change'), function () {
		var checked = $(this).attr('checked');
		var group = $(this).attr('name');
		// label
		if (checked) {
			$(this).next().addClass('active');
		} else {
			$(this).next().removeClass('active');
		}
		// checkboxes
		$('input[type=checkbox][name=' + group + ']').each(function() {
			$(this).attr('checked', checked);
		});
	});

	/*
	 * Search Results
	 */
	var check_all = $('input#check-all');
	var results = $('#search-results');
	var filter = $('input#searchfilter');
	var stacked = $('input#stacked');
	var survey = $('input#searchaddcart');
	var surveydelete = $('input#searchdeletecart');

	/*
	 * Filter Button
	 * 3 states: disabled (no checkboxes selected)
	 *					 normal (ready to filter results)
	 *					 active (results are currently filtered)
	 *
	 * normal & active are set via the PHP code
	 * toggle between normal & disabled (only) depending on num checked
	 */

	/*
	 * Stacked Availibility Button
	 * 2 states: disabled (no checkboxes selected)
	 *			 normal (ready to display results)
	 *
	 * toggle between normal & disabled depending on num checked
	 */

	var enable_select = function(state) {
		check_all.attr('checked', state);
		if (state) {
			$('#select-all').addClass('active');
		} else {
			$('#select-all').removeClass('active');
		}
	};

	var enable_buttons = function(state) {
		var buttons = new Array (filter, stacked, survey, surveydelete);
		$.each(buttons, function() {
		  if (state) {
				this.removeAttr('disabled').removeClass('disabled');
			} else {
				// before disabling the button check that it doesn't have the 3rd state 'active'
				if (!this.hasClass('active')) {
					this.attr('disabled', true).addClass('disabled');
				}
			}
		});
	};

	var enable_stacked = function(state) {
		if (state) {
			stacked.removeAttr('disabled').removeClass('disabled');
		} else {
			stacked.attr('disabled', true).addClass('disabled');
		}
	}

	// set initial state button states
	if (results.find('input.form-checkbox:checked').length == 0) {
		enable_buttons(false);
	}

	/*
	 * Select All
	 * check / uncheck all buildings & suites
	 */

	/*
	 * BUG (IE): IE doesn't trigger a change event immediately as a box is
	 *           checked / unchecked. Appears that you have to check the box AND
	 *           then click somewhere else on the page for the change event to trigger.
	 *
	 *			So we have to use the 'click' event for IE. The problem is that a
	 *			click event can trigger when a checkbox is clicked but nothing's
	 *			changed. In this instance I don't think it's a problem. Unsure
	 *			if there are any accessibility issues with using 'click' event.
	 */

	//check_all.change(function() {
	check_all.bind(($.browser.msie ? 'click' : 'change'), function () {
		var checked = $(this).attr('checked');
		// label
		if (checked) {
			enable_select(true);
			enable_buttons(true);
			// stacked availibility has a max of 8 buildings, disable if more checked
			if (results.find('input.form-checkbox:checked').length > 8) {
				enable_stacked(false);
			}
		} else {
			enable_select(false);
			enable_buttons(false);
		}
		// checkboxes
		results.find('input.form-checkbox').each(function() {
			$(this).attr('checked', checked);
		});
	});

	/*
	 * Building
	 * check / uncheck all suites within a building
	 */
	//results.find('.building .title input.form-checkbox').change(function() {
	results.find('.building .title input.form-checkbox').bind(($.browser.msie ? 'click' : 'change'), function () {
		var checked = $(this).attr('checked');
		$(this).parents('.building').find('table.suites input.form-checkbox').each(function() {
			$(this).attr('checked', checked);
		});
	});

	/* update the select all & filter button display (everything checked, anything unchecked) */
	//results.find('input.form-checkbox').change(function() {
	results.find('input.form-checkbox').bind(($.browser.msie ? 'click' : 'change'), function () {
		if (!$(this).attr('checked')) {
			enable_select(false);
		}
	  // has everything been checked?
		if (results.find('input.form-checkbox:checked').length == results.find('input.form-checkbox').length) {
			enable_select(true);
		}
		// set button states depending on if anything's checked
		if (results.find('input.form-checkbox:checked').length == 0) {
			enable_buttons(false);
		} else {
			enable_buttons(true);
		}
		// stacked availibility has a max of 8 buildings, disable if more checked
		if (results.find('input.form-checkbox:checked').length > 8) {
			enable_stacked(false);
		}
	});
});
