Collapsers = function() {
	this.sections = new Array;
	this.hash = document.location.hash.substring(1);
	var elements = getElementsByClassName("collapse-section", document.getElementsByTagName("body")[0]);
	for (var i = 0; i < elements.length; i++) {
		var singleFlag = elements[i].className.match("single");
		this.sections[this.sections.length] = new CollapseSection(elements[i], singleFlag,this);
	}
	addClass(document.getElementsByTagName("body")[0], "collapser-ok");
	tickle();
}
Collapsers.prototype.destroy = function() {
	for (var i = 0; i < this.sections.length; i++) {
		for (var j = 0; j < this.sections[i].items.length; i++) {
			this.sections[i].items[j].ref = null;
			this.sections[i].items[j].header.onclick = this.sections[i].items[j].header.ref = null;
		}
	}
}
CollapseSection = function(obj, singleFlag, ref) {
	this.animated = obj.className.match("animated");
	this.items = new Array;
	this.ref = ref;
	this.singleFlag = singleFlag;
	var elements = getElementsByClassName("collapse-item", obj);
	for(var i = 0; i < elements.length; i++) {
		this.items[this.items.length] = new CollapseItem(elements[i], this);
	}
}
CollapseSection.prototype.closeAll = function(obj) {
	for (var i = 0; i < this.items.length; i++) {
		if (this.items[i] != obj)
			this.items[i].close(); 
		else
			this.items[i].open();
	}
}
CollapseItem = function(obj, ref) {
	this.collapsed = obj.className.match("collapsed");
	this.container = obj;
	this.ref = ref;
	var header = getElementsByClassName("collapse-header", this.container)[0];
	this.header = document.createElement("a");
	this.header.href = "#" + this.container.id;
	this.header.ref = this;
	this.header.onclick = function() {
		this.ref.toggle();
		return false;
	}
	this.header.innerHTML = header.innerHTML;
	header.innerHTML = "";
	header.appendChild(this.header);
	this.content = getElementsByClassName("collapse-content", this.container)[0];
	if (this.ref.ref.hash && this.container.id == this.ref.ref.hash) {
		addClass(this.container, "collapsed");
		this.collapsed=true;
	}
}
CollapseItem.prototype.toggle = function() {
	if (this.ref.singleFlag) this.ref.closeAll(this);
	else if (!this.collapsed)
		this.open();
	else
		this.close();
}
CollapseItem.prototype.open = function() {
	if (this.collapsed) return;
	if (this.ref.animated) {
		if (this.animation) this.animation.stop();
		this.container.style.height = this.container.offsetHeight + "px";
		this.container.style.overflow = "hidden";
		addClass(this.container, "collapsed");
		this.animation = new Animator(this.container.offsetHeight, this.header.offsetHeight + this.content.offsetHeight, this.method(this.setHeight), this.method(this.done));
		this.animation.start();
	} else addClass(this.container, "collapsed");
	this.collapsed = true;
	tickle();
}
CollapseItem.prototype.close = function() {
	if (!this.collapsed) return;
	if (this.ref.animated) {
		if (this.animation) this.animation.stop();
		this.container.style.height = this.container.offsetHeight + "px";
		this.container.style.overflow = "hidden";
		this.animation = new Animator(this.container.offsetHeight, this.header.offsetHeight, this.method(this.setHeight), this.method(this.closed));
		this.animation.start();
	} else {
		removeClass(this.container, "collapsed");
		this.collapsed = false;
	}
	tickle();
}
CollapseItem.prototype.setHeight = function(x) {
	this.container.style.height = x + "px";
}
CollapseItem.prototype.closed = function() {
	removeClass(this.container, "collapsed");
	this.collapsed = false;
	this.done();
}
CollapseItem.prototype.done = function() {
	this.container.style.height = "auto";
	this.container.style.overflow = "visible";
	tickle();	
}
