/************************************************************************************************************
Automatic collapsible list generation script written by Mark Wilton-Jones - 2002 - script@freegames.uk.eu.org
*************************************************************************************************************

Feel free to use this code yourself on non-profit websites.
Please leave this header section and the comments in there.
If you change anything, please leave a note saying what you did and why.
_________________________________________________________________________

You can put as lists on the page as you like, each list may have a different format

To use:
_________________________________________________________________________

Inbetween the <head> tags, put:

	<script src="PATH TO SCRIPT/collapsibleList.js" type="text/javascript" language="javascript1.2"></script>
_________________________________________________________________________

For best results, all images should be the same height, slightly taller than
the text around them. I would recommend about 21px.

Now, wherever you want the list (not in a layer or positioned element due
to Netscape 4 style bug), put:

	<script type="text/javascript" language="javascript1.2"><!--

//The following is an example of a list which I have decided to call 'myList'

//first create a tree object and define all images to use
var myList = new collapsibleList(
	[
		17,      //width of tree and branch pictures
		21,      //height of tree and branch pictures
		't.gif', //branch junction image, shaped like this |-
		'l.gif', //'last branch' image, shaped like this `-
		'i.gif', //tree trunk image, shaped like this |
		'e.gif'  //blank spacer image, eg. a transparent gif
	],
	[
		25,      //width of expand / collapse images
		21,      //height of expand / collapse images
		'f.gif', //image used in place of expand or collapse images if the browser cannot expand / collapse
		'b.gif', //basic horizontal line image like this - for branches that do not expand
		'p.gif', //image used for expand link, looks like this [+]
		'm.gif'  //image used for collapse link, looks like this [-]
	]
);

//then create branches
myList.sub[0] = new sub('List item 1',true);
myList.sub[0].sub[0] = new sub('List item 1.1');
myList.sub[0].sub[0].sub[0] = new sub('List item 1.1.1');
myList.sub[0].sub[1] = new sub('List item 1.2',true);
myList.sub[0].sub[1].sub[0] = new sub('List item 1.2.1');
myList.sub[0].sub[1].sub[1] = new sub('List item 1.2.2',true);
myList.sub[0].sub[1].sub[1].sub[0] = new sub('List item 1.2.2.1');
myList.sub[0].sub[2] = new sub('List item 1.3');
myList.sub[0].sub[2].sub[0] = new sub('List item 1.3.1');
myList.sub[0].sub[3] = new sub('List item 1.4');
myList.sub[0].sub[4] = new sub('List item 1.5');
myList.sub[1] = new sub('List item 2');
myList.sub[2] = new sub('List item 3');
myList.sub[2].sub[0] = new sub('List item 3.1');
myList.sub[2].sub[0].sub[0] = new sub('List item 3.1.1');

//then tell the browser to create the collapsible list
createList(myList);

	//--></script>

*******************************************************************************************************
                               And here's the actual code
******************************************************************************************************/

window.collapsibleListRef = []; window.imageCache = [];

function collapsibleList() {
	//hold tree information about the list and the images used
	this.treeImg = arguments[0];
	this.expdImg = arguments[1];
	this.sub = [];
}

function sub() {
	//hold information about the list item
	this.text = arguments[0];
	this.expanded = arguments[1];
	this.sub = [];
}

//done because WebTV does not understand inline object syntax
function ColLexPdOb(plI,miI) { this.plI = plI; this.miI = miI; this.subs = []; }
function ColLexPdIT(name,expanded) { this.name = name; this.expanded = expanded; }

function createList(listObject,currentExt,imgExt,oLev,oBase) {
	if( !currentExt ) {
		//create the list, start by defining aditional stuff that is required, counting the lists etc.
		window.collapsibleListRef[window.collapsibleListRef.length] = new ColLexPdOb( listObject.expdImg[4], listObject.expdImg[5] );
		currentExt = 'treeList_'+( window.collapsibleListRef.length - 1 ); imgExt = ''; oLev = 0; oBase = listObject;
		document.write( '<p style="line-height:'+(document.layers?listObject.treeImg[1]+'px':'100%')+';">' );
	}
	document.write( '<span style="display:;" id="'+currentExt+'">' );
	for( var x = 0; x < listObject.sub.length; x++ ) {
		var style= 1;
		var tempStr = '';
		var linkStr1 = '';
		var linkStr2 = '';
		if (listObject.sub[x].sub.length) {
			tempStr = '<a href="#" onclick="expandList(\'' + currentExt + '_' + x + '\',\'' + currentExt + '_' + x + '_img\',\'' + oBase.expdImg[4] + '\',\'' + oBase.expdImg[5] + '\');if(this.blur){this.blur();}return false;"><img src="' + oBase.expdImg[2] + '" align="absmiddle" name="' + currentExt + '_' + x + '_img" width="' + oBase.expdImg[0] + '" height="' + oBase.expdImg[1] + '" alt="[+/-]" border="0"></a> ';
			linkStr1 = '<a href="#" class="level' + style + '" onclick="expandList(\'' + currentExt + '_' + x + '\',\'' + currentExt + '_' + x + '_img\',\'' + oBase.expdImg[4] + '\',\'' + oBase.expdImg[5] + '\');if(this.blur){this.blur();}return false;">';
			//linkStr1 = '<a href="#" class="level1" onclick="expandList(\'' + currentExt + '_' + x + '\',\'' + currentExt + '_' + x + '_img\',\'' + oBase.expdImg[4] + '\',\'' + oBase.expdImg[5] + '\');if(this.blur){this.blur();}return false;">';
			linkStr2 = '</a>';
		}
		else {
			linkStr1 = '';
			tempStr = '<img src="' + oBase.expdImg[3] + '" align="absmiddle" width="' + oBase.expdImg[0] + '" height="' + oBase.expdImg[1] + '" alt="-----" border="0"> ';
			linkStr2 = '';
		}
		//for every child object, create their line
		document.write(
			//the line break on all except the very first line
			( ( oLev || x ) ? '\n<br>' : '' ) +
			//what may or may not remain of the trunk
			imgExt +
			//this branch
			( oLev ? '<img src="' + ( ( x < listObject.sub.length - 1 ) ? oBase.treeImg[2] : oBase.treeImg[3] ) + '" align="absmiddle" width="' + oBase.treeImg[0] + '" height="' + oBase.treeImg[1] + '" alt="' + ( ( x < listObject.sub.length - 1 ) ? '|-' : '`-' ) + '" border="0">' : '' ) +
			//the expand / collapse link or not as the case may be
			( listObject.sub[x].sub.length ? '<a href="#" onclick="expandList(\'' + currentExt + '_' + x + '\',\'' + currentExt + '_' + x + '_img\',\'' + oBase.expdImg[4] + '\',\'' + oBase.expdImg[5] + '\');if(this.blur){this.blur();}return false;"><img src="' + oBase.expdImg[2] + '" align="absmiddle" name="' + currentExt + '_' + x + '_img" width="' + oBase.expdImg[0] + '" height="' + oBase.expdImg[1] + '" alt="[+/-]" border="0"></a> ' : '<img src="' + oBase.expdImg[3] + '" align="absmiddle" width="' + oBase.expdImg[0] + '" height="' + oBase.expdImg[1] + '" alt="-----" border="0"> ' ) +
			//the text of the branch
			linkStr1 +
			//the text of the branch
			listObject.sub[x].text +
			linkStr2
		);
		if( listObject.sub[x].sub.length ) {
			//add the span id to a list so we can easily find it and collapse it later
			window.collapsibleListRef[window.collapsibleListRef.length - 1].subs[window.collapsibleListRef[window.collapsibleListRef.length - 1].subs.length] = new ColLexPdIT( currentExt + '_' + x, listObject.sub[x].expanded );
			//create children
			createList( listObject.sub[x], currentExt + '_' + x, oLev ? ( imgExt + ( ( x < listObject.sub.length - 1 ) ? '<img src="' + oBase.treeImg[4] + '" align="absmiddle" width="' + oBase.treeImg[0] + '" height="' + oBase.treeImg[1] + '" alt="|&nbsp;">' : '<img src="' + oBase.treeImg[5] + '" align="absmiddle" width="' + oBase.treeImg[0] + '" height="' + oBase.treeImg[1] + '" alt="&nbsp;&nbsp;">' ) ) : '', oLev + 1, oBase );
		}
	}
	document.write( '</span>' );
	if( !oLev ) {
		//end the list and prepare to collapse as soon as the browser lays it out
		document.write( '</p>\n' );
		if( document.all || document.getElementById ) { window.setTimeout('prepareForUse('+(window.collapsibleListRef.length-1)+')',100); }
	}
}

function expandList(spanName,imgName,plsImg,minImg) {
	var theSpan = document.getElementById ? document.getElementById(spanName) : document.all ? document.all[spanName] : (new Object());
	if( !theSpan ) { return; } if( theSpan.style ) { theSpan = theSpan.style; }
	if( typeof( theSpan.display ) == 'undefined' ) {
		//if we could not access the span element or if its display style cannot be changed, say so
		window.alert( 'Sorry, your browser cannot collapse or expand these lists\nso the lists will remain constantly expanded.' );
	} else {
		//expand / collapse the list and change the image
		theSpan.display = theSpan.display ? '' : 'none';
		document.images[imgName].src = theSpan.display ? plsImg : minImg;
	}
}

function prepareForUse(listNum) {
	if( !window.collapsibleListRef[listNum].subs.length ) { return; } //no branches to collapse
	var lastPart = window.collapsibleListRef[listNum].subs[window.collapsibleListRef[listNum].subs.length - 1].name;
	lastPart = document.getElementById ? document.getElementById(lastPart) : document.all[lastPart];
	//if the page has not loaded enough, try again later
	if( !lastPart ) { window.setTimeout('prepareForUse('+listNum+')',100); } else {
		//if their browser cannot change the display style, don't try
		if( lastPart.style ) { lastPart = lastPart.style; }
		if( typeof( lastPart.display ) == 'undefined' ) { return; }
		//cache images for faster changes
		window.imageCache[listNum] = [(new Image()),(new Image())];
		window.imageCache[listNum][0].src = window.collapsibleListRef[listNum].plI;
		window.imageCache[listNum][1].src = window.collapsibleListRef[listNum].miI;
		for( var x = 0; x < window.collapsibleListRef[listNum].subs.length; x++ ) {
			var lastPart = window.collapsibleListRef[listNum].subs[x];
			if( lastPart.expanded ) {
				//if they want it expanded by default, just change the image
				document.images[lastPart.name + '_img'].src = window.imageCache[listNum][1].src;
			} else {
				//collapse the branch and change the image
				document.images[lastPart.name + '_img'].src = window.imageCache[listNum][0].src;
				lastPart = document.getElementById ? document.getElementById(lastPart.name) : document.all[lastPart.name];
				if( lastPart.style ) { lastPart = lastPart.style; }
				lastPart.display = 'none';
			}
		}
	}
}
