// JavaScript Document
/*
	i require prototype

	i manage selections of rows in a table with checkbox input.select
	i will handle a select all input with class="select_all" which will toggle ...
	handle checkbox inputs with class="select", if all are selected will trigger input.select_all
	if no items are selected, i will disable any input.button
	if there is a tr.selected css i will apply/remove it to a row on checkbox toggle
	
	what i need to be smarter about is selecting the row and not going two nodes up (sometimes i only select the td and not the tr)
*/

var cs2_table = {	

	tr_select_clickhandler:function() { cs2_table.tr_select(this); },
	tr_select:function(n) {
		var tr = n.up('tr');
		(n.checked === true) ? tr.addClassName('selected') : tr.removeClassName('selected');
		//cs2_table.update_select_all();
		//cs2_table.update_buttons();
	},
		
	select_all_clickhandler:function() { cs2_table.select_all(this); },
	select_all:function(element) {
		//console.log('select_all');
		value = ( element.checked ) ? true : false;
		cs2_table.people_selectors.each( function(n) { 
			//console.log('select_all each'); 
			n.checked = element.checked; 
		});
	},
	
	update_buttons:function() {
		//console.log('update_buttons');
		var one_checked = false;
		cs2_table.people_selectors.each( function (n) { one_checked = ( n.checked ) ? true : one_checked; } );
		$$('input.button').each( function (n) { (one_checked) ? n.enable() : n.disable(); });
	},
	
	update_select_all:function() {
		cs2_table.people_selectors.each( function(cbx) { 
			if ( ! cbx.checked ) { 
				$('select_all').checked = false;
			} 
		}); 

	},
	
	init:function() {
		// setup event handlers
		if ( $('select_all') ) { 
			$('select_all').observe('click', cs2_table.select_all_clickhandler);
			$('people').observe('click', function (e) { 
				if ( e.target.type == 'checkbox' && e.target.hasClassName('select') && !e.target.checked ) {
					// console.log('uncheck the select all control');	
					$('select_all').checked = false;
				}
			});
			
			cs2_table.people_selectors = $$('input.select');
					
			//cs2_table.update_buttons();
			// cs2_table.update_select_all();
		}
	
	}
}

Event.observe(window, 'load', cs2_table.init);