// diddling with forum group display, and save prefs

// note: now heavily relying on prototype.js library, especially its wonderful select()

function get_sort_key(element) {
	// given a cell of the table, return the key that we sort on.
	// if it has an x-sort-key attribute, use that.  We do that, because the "Last Topic" column
	// contains a lot of text (e.g. "yadda yadda (sloppy) [ Mon Jun 23 2008 8:39 AM ]")
	// but the embedded date is what we're really interested in.  Instead of having our javascript
	// try to parse that, I just polluted our HTML a bit.  If you don't like that, tough shit.
	if (!(retval=element.getAttribute('x-sort-key'))) {
		// otherwise, use the contents of the cell, cvted to numeric
		retval=Number(element.firstChild.firstChild.nodeValue);
	}
	return retval;
}

function sort_table(table,column,rev) {
	//table.style.cursor='wait'; // that would cool if it applied to the whole table.  But it doesn't, so don't bother.
	tbody=table.firstChild;

	row=tbody.firstChild; // # header row
	row.setAttribute('x-stop',1); // put a sentinel on the header row

	row=row.nextSibling; // 2nd row of the table, 1st row of data
	if (row)
		row=row.nextSibling; // start the sort on the 2nd row of data
		
	// insertion sort
	while (row) {
		next=row.nextSibling; // remember next row before we start juggling
		
		thiskey=get_sort_key(row.childNodes[column]);
		
		// look backwards in the list to the appropriate re-insertion point
		targbefore=row.previousSibling;
		while (!(targbefore.getAttribute('x-stop'))) {
			cmp=(get_sort_key(targbefore.childNodes[column]) <= thiskey);
			if (rev) { // reverse the comparison
				cmp=!cmp;
			}
			if (cmp) {
				break;
			}
			targbefore=targbefore.previousSibling;
		}
		if (targbefore.nextSibling != row) {
			// and then move it
			tbody.insertBefore(row,targbefore.nextSibling);
		}
		row=next;
	}
	// save info
	table.setAttribute('x-last-sorted-on',column);
	table.setAttribute('x-reverse-sort',rev?1:0);
	
	// find the widget if I've already created it
	if (!(widget=table.select('.forum_category_widgets img')[0])) {
		// create if haven't done it yet
		widget=document.createElement('IMG');
	}
	
	// point the widget depending on sort order
	if (rev) {
		widget.src='/editorial/images/sort_reverse.gif';
	} else {
		widget.src='/editorial/images/sort_normal.gif';
	}
	
	// put the widget onto the header for the column we just sorted
	$(tbody.firstChild.childNodes[column]).select('.forum_category_widgets')[0].appendChild(widget);
	
	// 12/4/8 update the sort selector
	if (sel=$('sortsel'+table.id.substr(3))) {
		sel.selectedIndex=column*2+rev;
	}
	//table.style.cursor='';
}

function clicked_column(table,column) {
	tbody=table.firstChild;

	row=tbody.firstChild; // # header row

	// deal with reversals: if last sort was this column, then invert reversal setting
	lastsortedon=table.getAttribute('x-last-sorted-on');
	if (lastsortedon==column) {
		// last time we sorted this table, it was on this column
		rev=(table.getAttribute('x-reverse-sort')==0?1:0); // so invert the sort order
	} else {
		// last time was diff column, so sort 'normally' for that col (incrementally for names, dec for last post date)
		rev=Number(row.childNodes[column].getAttribute('x-default-sort'));
	}
	
	sort_table(table,column,rev);
	save_forum_group_prefs(table);
}

function clicked_sort_select(select) { // 12/4/8
	table=$('tab'+select.getAttribute('x-cat'));
	choice=select.options[select.selectedIndex];
	column=Number(choice.getAttribute('x-col'));
	rev=Number(choice.getAttribute('x-rev'));
	sort_table(table,column,rev);
}

function save_forum_group_prefs(table) {	
	new Ajax.Request('/',
	{
		method: 'get',
		parameters: new Hash( { ajax_function: 'save_forum_group_prefs',
		                        category: table.id.substr(3),
		                        column: table.getAttribute('x-last-sorted-on'),
		                        reverse: table.getAttribute('x-reverse-sort'),
		                        show_inactive: $('cat'+table.id.substr(3)+'pref_show_inactive').checked?1:0
							   } )
		//,onComplete: function(transport) {
		//	alert(transport.responseText);
		//}

	});
}

function clicked_show_inactive_pref(cat) {
	pref=$('cat'+cat+'pref_show_inactive');
	table=$('tab'+cat);
	tbody=table.firstChild;
	row=tbody.firstChild;
	while (row) {
		if (row.className=='inactive_forum_row') {
			if (pref.checked) {
				row.style.visibility='visible';
				row.style.display='';
			} else {
				row.style.visibility='hidden';
				row.style.display='none';
			}
		}
		row=row.nextSibling;
	}
	save_forum_group_prefs(table);
	
	return true;
}
