/* Copyright 2006 Christopher Lansdown
 * This code is released under the Gnu General Public License version 2.0.
 */

/**
 * stSizes is a hash of the tabs and the sizes (in pixels) that they
 * should be. You'll need to customize this to your own site. The ids
 * are very necessary; the sizes themselves are only used as a backup
 * in case they can't be auto-detected.
 */
window.stSizes = {about:80, professional:130, resume:30, interests:290, contact:40};
window.transmogrifySteps = 5;
window.stepDuration = 60;

/* There shouldn't be anything that you need to customize below this line */


function selectST(st) {
	var target = document.getElementById(st);
	var expandName;
	var shrinkName;


	/* Step 0: detect re-clicks */
	if(target == window.selectedST)
		return false;

	/* Step 1: figure out who does what */
	if(window.selectedST != null) {
		window.expandST = target;
		expandName = st;
		window.shrinkST = window.selectedST;
		shrinkName = window.selectedSTName;
		window.shrinkAllBut = null;
	} else {
		window.expandST = null;
		window.expandSTName = null;
		window.shrinkST = null;
		window.shrinkSTName = null;
		window.shrinkAllBut = target;
	}
	window.selectedST = target;
	window.selectedSTName = st;

	/* Step 2: compute the sizes */
	if(window.expandST != null) {
		window.expandSTSize = 0;
		window.expandSTFinalSize = window.stSizes[expandName];
		window.expandSTStepSize = Math.ceil(window.stSizes[expandName]/window.transmogrifySteps);
		window.shrinkSTSize = window.stSizes[shrinkName];
		window.shrinkSTStepSize = Math.ceil(window.stSizes[shrinkName]/window.transmogrifySteps);
	} else {
		window.shrinkSTs = new Array();
		window.shrinkSTSizes = new Array();
		window.shrinkStepSizes = new Array();
		for(t in stSizes) {
			var el = document.getElementById(t);
			if(el != null && el != target) {
				window.shrinkSTs.push(el);
				window.shrinkSTSizes.push(window.stSizes[t]);
				window.shrinkStepSizes.push(Math.ceil(window.stSizes[t]/window.transmogrifySteps));
			}
		}
	}

	/* Step 3: do the initial resize */
	resizeSTs();

	/* Return false since this may be used as the return value to an onClick() */
	return false;
}

function resizeSTs() {
	initializeHeights();

	if(window.expandST) {
		window.expandSTSize += window.expandSTStepSize;
		window.shrinkSTSize -= window.shrinkSTStepSize;
		window.shrinkSTSize = Math.max(window.shrinkSTSize, 0);
		window.expandST.style.height = expandSTSize + "px";
		window.expandST.style.display = 'block';
		window.shrinkST.style.height = window.shrinkSTSize + "px";
		if(window.shrinkSTSize > 0)
			setTimeout("resizeSTs();", window.stepDuration);
		else {
			window.expandST.style.height = window.expandSTFinalSize;
			window.shrinkST.style.display = 'none';
			window.blur();
		}

	} else {
		for(var i = 0; i < window.shrinkSTs.length; i++) {
			window.shrinkSTSizes[i] -= window.shrinkStepSizes[i];
			window.shrinkSTSizes[i] = Math.max(window.shrinkSTSizes[i], 0);
			window.shrinkSTs[i].style.height = window.shrinkSTSizes[i] + "px";
		}
		if(window.shrinkSTSizes[0] && window.shrinkSTSizes[0] > 0)
			setTimeout("resizeSTs();", window.stepDuration);
		else
			for(var i = 0; i < window.shrinkSTs.length; i++) {
				window.shrinkSTs[i].style.display = 'none';
			}
	}

}

function initializeHeights() {
	if(window.heightsInitialized)
		return true;
	window.heightsInitialized = 1;

	for(var st in stSizes) {
		var el = document.getElementById(st);
		var h = getElementHeight(el);
		if(h > 0)
			stSizes[st] = h;
	}
}

function getElementHeight(el)  {
    var height = 0;
    if (el.offsetHeight) {
        height = el.offsetHeight;
    } else if (el.clip && el.clip.height) {
        height = el.clip.height;
    } else if (el.style && el.style.pixelHeight) {
        height = el.style.pixelHeight;
    }
    return parseInt(height);
}
