// Expand/Collapse swapper...
ExpandCollapse = Class.create();
ExpandCollapse.prototype = {
	initialize: function(label, content, container) {
		// get the labels and contents
		this.lablesClass = label;
		this.lables = document.getElementsByClassName(label);
		this.contentClass = content;
		this.content = document.getElementsByClassName(content);

		// hide/show the lables and content
		for (var i=0; i<this.lables.length; i++) {
			this.lables[i].target = i;
			if (!Element.hasClassName(this.content[i], 'active')) { this.content[i].style.display = 'none' };
			Event.observe(this.lables[i], 'click', this.showhide.bind(this), false);
		}

		// add the hasjs class to faciliate styles
		Element.addClassName(container, 'hasjs');
	},
	showhide: function(ev) {
		// stop the default action
		Event.stop(ev);

		// find the clicked link
		if(!ev) { ev = window.event; }
		var clicked = (window.event) ? window.event.srcElement : ev.target;
		while (!clicked.className || Element.hasClassName(clicked, this.contentClass)) clicked = clicked.parentNode;

		// swap the clicked link class
		if (Element.hasClassName(clicked, 'closed')) {
			Element.removeClassName(clicked, 'closed');
		} else {
			Element.addClassName(clicked, 'closed');
		}

		// swap the href if it's there
		if (clicked.href) {
			var link = (clicked.href.substring(clicked.href.indexOf('#'), clicked.href.length))
			clicked.href = (link == '#more') ? '#less' : '#more';
		}

		// swap the label text if it has more in it
		if (clicked.innerHTML.indexOf('More')>-1) {
			var newstr = clicked.innerHTML.replace(/More/, 'Fewer');
			clicked.innerHTML = newstr;
		} else if (clicked.innerHTML.indexOf('Fewer')>-1) {
			var newstr = clicked.innerHTML.replace(/Fewer/, 'More');
			clicked.innerHTML = newstr;
		}

		// do the expand/collapse fffect
		Effect.toggle(this.content[clicked.target], 'blind', {duration:.5});
	}
}

Event.observe(window, 'load', function() { new ExpandCollapse('expandlabel', 'expandcontent', 'content'); }, false);
