var sections = new Array();

var collapseInterval = 25;
var collapseDuration = 200;
var collapseDefaultSpeed = 500;
var collapseUseDefaultSpeed = true;

var timer = null;

function addCollapseSection(id) {
	var content = document.getElementById(id);
	if (content) {
		var originalSize = content.offsetHeight ? content.offsetHeight : content.style.pixelHeight;
		content.style.overflow = 'hidden';
		content.style.height = '0px';
		sections[sections.length] = new Array(content, originalSize, 0);
	}
}

function expand(id) {
	if (timer) {
		return;
	}
	var collapseIndex = -1;
	var expandIndex = -1;
	for (var i = 0; i < sections.length; i++) {
		if (sections[i][0].id == id) {
			expandIndex = i;
		}
		else if (sections[i][2]) {
			collapseIndex = i;
		}
	}
	if (collapseIndex != -1) {
		startAnimation(collapseIndex, expandIndex, true);
	}
	else if (expandIndex != -1) {
		startAnimation(expandIndex, -1, false);
	}
}

function startAnimation(current, successor, collapse) {
	var speed = collapseUseDefaultSpeed ? collapseDefaultSpeed  / 1000 : sections[current][1] / collapseDuration;
	var offset = (collapse ? -1 : 1) * Math.round(collapseInterval * speed);
	timer = setInterval('animate(' + current + ', ' + offset + ', ' + successor + ');', collapseInterval);
}

function stopAnimation() {
	clearInterval(timer);
	timer = null;
}

function animate(index, offset, expandIndex) {
	var obj = sections[index][0];
	originalSize = sections[index][1];
	offset = parseInt(offset);
	var currentSize = parseInt(obj.style.height);
	if (currentSize + offset < 0) {
		obj.style.height = '0px';
		sections[index][2] = 0;
		stopAnimation();
		if (expandIndex != -1) {
			startAnimation(expandIndex, -1, false);
		}
	}
	else if (currentSize + offset > originalSize) {
		obj.style.height = originalSize + 'px';
		sections[index][2] = 1;
		stopAnimation();
	}
	else {
		obj.style.height = (currentSize + offset) + 'px';
	}
}

