// add multiple events after page loads (by Simon Willison)
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// borrowed from dustan diaz http://www.dustindiaz.com/top-ten-javascript
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// -- start ddhide group ---
// (inspired by http://www.kevinleitch.co.uk/wp/?p=286 )
// toggle the element's class attribute between 'hidden' and 'hide' (CSS handles the rest)
function toggle(id) {
	var el = document.getElementById(id);
	el.className = (el.className == 'ddhidden' ? 'ddhide' : 'ddhidden');
}
// set the initial class values
function init() {
	// change all the 'hide' class attributes to 'hidden'
	var ddlist = getElementsByClass('ddhide');
	for (var j=0; j<ddlist.length; j++) {
		if (ddlist[j].className == 'ddhide') {
			ddlist[j].className = 'ddhidden';
		}
	}
	// append a link to the toggler element that will toggle the 'hide/hidden' element
	var ddp = getElementsByClass('dd');
	for (var k=0; k<ddp.length; k++) {
		ddp[k].appendChild(document.createTextNode(' '));
		var ddpid = ddp[k].id.slice(0,-1);
		var ddanchor = document.createElement('a');
		ddanchor.href = '#';
		ddanchor.id = ddpid + 0;
		ddanchor.className = 'dda';
		var ddatextnode = document.createTextNode('[view more \u2193]');
		ddanchor.appendChild(ddatextnode);
		ddp[k].appendChild(ddanchor);
	}
}
// add an event handler to the toggler element
function dropdown() {
	var dd = getElementsByClass('dda');
	for (var i=0; i<dd.length; i++) {
		dd[i].onclick = function() {
			var id = this.getAttribute('id');
			var idroot = id.slice(0,-1);
			var toggleid = idroot + 2;
			toggle(toggleid);
			return false;
		}
	}
}
// --- end ddhide group ---

addLoadEvent (function() {
	init();
	dropdown();
});
