// vi:set ts=4 sw=4:
var explanation_state = Array();
var explanations_visible = 0;
var explanation_cnt = 0;

var all_explanations_obj;


function initialise_explanation()
{
	all_explanations_obj = document.getElementById('all_explanations');
	toggle_all_explanations( false );
}

function toggle_explanation( explanation_id )
{
	var explanation = document.getElementById( 'explanation_' + explanation_id );
	
	if( !explanation )
	{	// invalid id given
		alert( 'oops' );
		return( false );
	}


	if( explanation_state[explanation_id] )
	{
		explanation.style.display = 'none';
		explanation_state[explanation_id] = false;
//		document.getElementById('explanation_' + explanation_id + '_label' ).innerHTML = '(Show)';
		if( --explanations_visible <= 0 )
		{// none visible, so update the #all_explanations link text
			explanations_visible = 0;
			set_all_explanations_label( false );
		}
	}
	else
	{
		explanation.style.display = 'block';
		explanation_state[explanation_id] = true;
//		document.getElementById('explanation_' + explanation_id + '_label' ).innerHTML = '(Hide)';
		if( ++explanations_visible >= explanation_cnt )
		{// All visible, so update the #all_explanations link text
			explanations_visible = explanation_cnt;
			set_all_explanations_label( true );
		}
	}


	return( false );
}

function toggle_all_explanations( state )
{
	var explanation;

	for( var i=0; i<100; i++ )
	{
		explanation = document.getElementById( 'explanation_' + i );
		if( !explanation )
		{
			explanation_cnt = i;
			break;
		}

		if( !state )
		{
			explanation.style.display = 'none';
			explanation_state[i] = false;
//			document.getElementById('explanation_' + i + '_label' ).innerHTML = '(Show)';
			explanations_visible = 0;
		}
		else
		{
			explanation.style.display = 'block';
			explanation_state[i] = true;
//			document.getElementById('explanation_' + i + '_label' ).innerHTML = '(Hide)';
			explanations_visible = explanation_cnt;
		}
	}

	set_all_explanations_label( state );
}

function set_all_explanations_label( state )
{
	if( !all_explanations_obj )
		return;	// no #all_explanations element on the page, so don't try to set it
	if( !state )
		all_explanations_obj.innerHTML = '<a href="#" onclick="toggle_all_explanations( true ); return false;">Show All</a>';
	else
		all_explanations_obj.innerHTML = '<a href="#" onclick="toggle_all_explanations( false ); return false;">Hide All</a>';
}
