/*
 * Functions for expanding/collapsing chunks of markup
 *
 * The markup should be structured into 'sections' (divs),
 * each containing an 'expander' (div with class="expander").
 *
 * When the expander is clicked, the section is either
 * expanded or collapsed (based on its current state).
 *
 * In this case, 'collapsing' a section merely alters its class
 * name to contain the word 'collapsed' - the presentation is
 * handled in CSS.
 *
 * @author scampbell
 * @date 26-09-2005
 */

/** Easy-access array of all sections */
var allSections = Array();


/**
 * Initialises event handlers for expanders.
 */
function init_expanders() {

	var divs = document.getElementsByTagName('div');

	//find the expander toggle for each section
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className.indexOf('expander') == -1) {
			continue;
		}

		var expander = divs[i];

		//perform implementation-specific initialisation
		init_expander(expander);

		var section = expander.parentNode;

		//collapse each section
		//toggle_section(section);

		//keep a reference to each section for later
		allSections.push(section);
	}
}


/**
 * Stub for expander initialisation function
 */
function init_expander(expander) {
	//overwrite as needed
}


/**
 * Expands/collapses the specified section.
 */
function toggle_section(section) {

	var expander;
	var divs = section.getElementsByTagName('div');

	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className.indexOf('expander') != -1) {
			expander = divs[i];
		}
	}

	if (is_collapsed(section)) {
		//expand
		section.className = section.className.replace(' collapsed', '');
		expander.getElementsByTagName('img')[0].src = collapseIcon.src;
	} else {
		//collapse
		section.className += ' collapsed';
		expander.getElementsByTagName('img')[0].src = expandIcon.src;
	}
}


/**
 * Collapses all sections.
 */
function collapse_all() {

	for (var j = 0; j < allSections.length; j++) {
		var section = allSections[j];

		if (!is_collapsed(section)) {
			toggle_section(section);
		}
	}
}


/**
 * Returns true if the specified section is collapsed.
 */
function is_collapsed(section) {
	return (section.className.indexOf(' collapsed') != -1);
}