// Stupidity here due to IE Bug (http://support.microsoft.com/kb/276228)
var selects = new Object();
$(document).ready(function() {
	selects.group = document.getElementById('lessonlist-chooser-group-div').innerHTML;
	selects.subcategory = document.getElementById('lessonlist-chooser-subcategory-div').innerHTML;
	var date = document.getElementById('lessonlist-chooser-date-div');
	
	if (date) // If running without date chooser, it will be null
		selects.date = date.innerHTML;
});

/**
 * Toggles visibile tablerows based on selections in the tablerowlist-group-chooser
 * <select>
 * @param select name of select that is modified
 * @param toggle_selects <select>s to toggle all items back to selected
 */ 
function group_chooser_onchange(modify_select, toggle_selects) {
	/* Due to a bug in IE, Chrome and Safari, we cannot hide <option>s.  Instead
	 * the only option is to remove them.  As such, we store the original lists at
	 * the document load and recreate the lists as needed.*/
	if (toggle_selects) {
		for (i = 0; i < toggle_selects.length; i++) {
			var select = document.getElementById('lessonlist-chooser-' + toggle_selects[i] + '-div');
			if (select) // If running without date chooser, it will be null
				select.innerHTML = selects[toggle_selects[i]];
		}
	}
	
	var search = '.lesson-summary';

	// See comment above...
	if (modify_select) {
		search += ', ' + '#lessonlist-chooser-' + modify_select + ' option';
		
		orig_select = document.getElementById('lessonlist-chooser-' + modify_select + '-div');
		if (orig_select)
			orig_select.innerHTML = selects[modify_select];
	}

	// Iterate through all the rows and hide/delete elements to match the exception
	var tablerows = $(search);
	for (i = 0; i < tablerows.length; i++) {
		if (!tablerows[i].id)
			continue;
		var id = tablerows[i].id;
		var row = $('[id^="' + id + '"]');
		if (row.length == 0)
			continue;
		var classes = row.attr('class').split(' ');

		row.show();
		for (j = 0; j < classes.length; j++) {
			if (classes[j] == 'lesson-summary' || classes[j] == '')
				continue;

			var opt = $('#' + classes[j]);
			if (opt.length != 0 && !opt.attr('selected')) {
				if (row[0].nodeName.toLowerCase() == 'option')
					row.remove();
				else
					row.hide();
			}
		}
	}
}
