window.addEvent('domready', function() {
	var index;
	var acc = document.getElementById('accordion');
	// Array of all expandable elements:
	var sections = getElementsByClassName('element', acc);
	
	// Loops through array, reading the href's of all contained a tags
	// if the href matches the current url, we store the index as 'index'
	for (var i=0;i<sections.length;i++)	{
		var links = sections[i].getElementsByTagName('a');
		for (var j=0;j<links.length;j++) {
			if (links[j].href == window.location) {
				index = i;
			};
		}
	}

	// If none matched, we're on the top level
	if (index == undefined)	{
		index = -1;
	};

	// creates accordion and uses 'index' in 'show' option
	// to auto-open that group
	var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
		opacity: false,
		fixedHeight: false,
		fixedWidth: false,
		height:true,
		width:false,
		show: index
	});
});

/* for some reason, all the built in MooTools utilities
/ seem to throw errors in IE6 and 7, so here's some long-hand
/ for collecting elements by class name.
*/

function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	if(re.test(els[i].className))a.push(els[i]);
	return a;
}
