test.js 2.23 KB
var collapsingDiv = "";
var activeHeight = 0;
var divHeight = "";
var collapseState = 0;
var deltaTime = 10;
var deltaHeight = 4;
var titleOffset = 40;
var docDivs = new Array();
var collapsingSpeed = "";

function initialize() {
	docDivs = document.getElementsByTagName("div");
	for (i=0;i<docDivs.length;i++) {
		if (docDivs[i].className == "hidden") {
			collapsingDiv = docDivs[i].id;
		}
	}
	activeHeight = titleOffset;
	divHeight = document.getElementById(collapsingDiv).offsetHeight;
	collapsingSpeed = collapsingDiv.substring(collapsingDiv.lastIndexOf('-')+1);
	document.getElementById(collapsingDiv).style.height = activeHeight+'px';
	document.getElementById('toggle').style.display = 'inline';
	
	if (collapsingSpeed == 1) { deltaTime = 100; deltaHeight = 1; }
	if (collapsingSpeed == 2) { deltaTime = 70; deltaHeight = 1; }
	if (collapsingSpeed == 3) { deltaTime = 40; deltaHeight = 1; }
	if (collapsingSpeed == 4) { deltaTime = 20; deltaHeight = 1; }
	if (collapsingSpeed == 5) { deltaTime = 10; deltaHeight = 1; }
	if (collapsingSpeed == 6) { deltaTime = 10; deltaHeight = 2; }
	if (collapsingSpeed == 7) { deltaTime = 10; deltaHeight = 4; }
	if (collapsingSpeed == 8) { deltaTime = 10; deltaHeight = 7; }
	if (collapsingSpeed == 9) { deltaTime = 10; deltaHeight = 10; }
}

function toggle() {
	if (collapseState === 0) {
		// Going Down
		document.getElementById(collapsingDiv).style.height = activeHeight+'px';
		if ((((divHeight)-activeHeight) < deltaHeight) && (activeHeight !== divHeight)) {
			activeHeight = divHeight;
		} else {
			activeHeight = activeHeight+deltaHeight;
		}
		if (activeHeight <= divHeight) {
			setTimeout('toggle()',deltaTime);//Recursively calling this function again.
		}
		if (activeHeight > divHeight) {
			activeHeight = divHeight;
			collapseState = 1;
		}
	} else {
		// Going Up
		document.getElementById(collapsingDiv).style.height = activeHeight+'px';
		activeHeight = activeHeight-deltaHeight;
		if (((divHeight)-activeHeight) <= divHeight-titleOffset) {
			setTimeout('toggle()',deltaTime);//Recursively calling this function again.
		}
		if (((divHeight)-activeHeight) > divHeight-titleOffset) {
			activeHeight = titleOffset;
			document.getElementById(collapsingDiv).style.height = activeHeight+'px';
			collapseState = 0;
		}
	}
}