function blockCollapser(blockRoot, menuRoot) {
	this.blockRoot = blockRoot;
	this.menuRoot = menuRoot;

	this.menuItems = menuRoot.getElementsByTagName("li");
	for(var m = 0; m < this.menuItems.length; m++) {
		var link = this.menuItems[m].getElementsByTagName("a")[0];
		var text = link.childNodes[0];
		this.menuItems[m].replaceChild(text, link);
	}

	this.blocks = new Array(this.menuItems.length);

	this.blockItems = YAHOO.util.Dom.getElementsByClassName("collapsing-block", "div", blockRoot);
	for(var i = 0; i < this.menuItems.length; i++) {
		if(this.menuItems[i] != null && this.blockItems[i] != null) {
			this.blocks[i] = new collapsingBlock(this.blockItems[i], this.menuItems[i], this);
			this.blocks[i].hide();
			blockRoot.removeChild(this.blockItems[i]);
		}
	}
	this.expanded;
	this.expand(this.blocks[0]);
	this.blocks[0].expand();
}
blockCollapser.prototype.expand = function(object) {
	if(this.expanded == object) return;
	if(this.expanded != null) {
		YAHOO.util.Dom.removeClass(this.expanded.trigger, "expanded");
		YAHOO.util.Dom.addClass(object.trigger, "expanded");
		this.blockRoot.insertBefore(object.element, this.expanded.element);
		this.expanded.collapse();
	}
	else {
		YAHOO.util.Dom.addClass(object.trigger, "expanded");
		this.blockRoot.insertBefore(object.element, this.blockRoot.firstChild)
	}
	this.expanded = object;
}

function collapsingBlock(element, trigger, collapser) {
	this.element = element;
	this.trigger = trigger;
	this.collapser = collapser || null;

	this.collapsedStyle = { height: { to: 0}};
	this.easing = { expand: YAHOO.util.Easing.easeOut, collapse: YAHOO.util.Easing.easeOut };
	this.expandedStyle = this.readExpandedStyle(element);
	this.duration = { expand: .8, collapse: .8 };
	YAHOO.util.Event.addListener(trigger, "click", this.toggle, this);
}
collapsingBlock.prototype.toggle   = function(event, object) {
	if(object == null) object = this;
	if(object.collapser != null) object.collapser.expand(object);

	//	if(object.isExpanded) object.collapse();
	//	else object.expand();
	object.expand();
}
collapsingBlock.prototype.collapse = function() {
	var myAnim = new YAHOO.util.Anim(this.element, this.collapsedStyle, this.duration.collapse, this.easing.collapse);
	myAnim.animate();
	this.isExpanded = false;
}
collapsingBlock.prototype.hide     = function() {
	for(st in this.collapsedStyle) {
		this.element.style[st] = this.collapsedStyle[st].to;
	}
	this.isExpanded = false;
}
collapsingBlock.prototype.expand   = function() {
	var myAnim = new YAHOO.util.Anim(this.element, this.expandedStyle, this.duration.expand, this.easing.expand);
	myAnim.animate();
	this.isExpanded = true;
}
collapsingBlock.prototype.readExpandedStyle = function(element) {
	var expandedStyle = new Object();
	for(st in this.collapsedStyle) {
		expandedStyle[st] = { to: 0 };
		if(typeof YAHOO.util.Dom.getStyle(element, st) == "string" ||
		typeof YAHOO.util.Dom.getStyle(element, st) == "number") {
			if(YAHOO.util.Dom.getStyle(element,st) == null || 
				YAHOO.util.Dom.getStyle(element,st) == "auto") {
				if(st == "height") { // If the height isn't explicitly defined, derive it.
					expandedStyle[st].to = element.offsetHeight;
				}
				else if(st == "width") { // If the width isn't defined, derive it.
					expandedStyle[st].to = element.offsetWidth;					
				}
				else if(st == "opacity") { // Default the opacity to fully opaque
					expandedStyle[st].to = 1;
				}
			}
			else {
				var reg = /(\d+)/;	// strip out unit identifiers, YAHOO doesn't use them.
				expandedStyle[st].to = YAHOO.util.Dom.getStyle(element,st);
				expandedStyle[st].to = reg.exec(expandedStyle[st].to)[1];
			}
		}
	}
	return expandedStyle;
}

function init() {
	var bc = new blockCollapser(document.getElementById("main-text"), document.getElementById("project-list"));
}
YAHOO.util.Event.addListener(window, "load", init);