var currentlyShowing = false;
var minHeight;
var maxHeight;

var isMoving = false;
var currentSpeed;
var currentFrame;
var currentHeight;
var targetHeight;
var vectorModifier;


var heightOfPadding = 10;


var startingSpeed = 10;
var accelerateBy = 8;
var maximumSpeed = 50;
var accelerateEvery = 2;
var animFrequency = 60;

function initPage(){
	var f=document.getElementsByTagName("a");
	for(i=0;i<f.length;i++)
	{
		if(f[i].className=="expandCol")
		{
			f[i].href="javascript:void(0)";
			addEvent(f[i],'click',showHideColumns,false);
		}
	}
	
	// now get the minHeight and maxHeight
	minHeight = document.getElementById("col1").offsetHeight - heightOfPadding;
	
	maxHeight = minHeight;
	
	for(c=1;c<=4;c++)
	{
		h = document.getElementById("col"+c).scrollHeight - heightOfPadding;
		if(h > maxHeight)
			maxHeight = h;
	}
}

function addEvent(elm, evType, fn, useCapture){
	if(elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else{
		elm['on' + evType] = fn;
	}
}

addEvent(window, 'load', initPage, false);

function showHideColumns()
{
	
	if(isMoving)
		return;
		
	isMoving = true;
	
	currentSpeed = startingSpeed;
	currentFrame = 1;
	
	currentHeight = currentlyShowing ? maxHeight : minHeight;
	targetHeight = currentlyShowing ? minHeight : maxHeight;
	vectorModifier = currentlyShowing ? -1 : 1;
	
	if(currentlyShowing)
	{
		var f=document.getElementsByTagName("a");
		for(i=0;i<f.length;i++)
		{
			if(f[i].className=="expandCol")
			{
				f[i].style.backgroundPosition="left top";
			}
		}
	}
	else
	{
		var f=document.getElementsByTagName("a");
		for(i=0;i<f.length;i++)
		{
			if(f[i].className=="expandCol")
			{
				f[i].style.backgroundPosition="left bottom";
			}
		}
	}
	
	
	
	moveColumns();
}

function moveColumns()
{
	d = currentSpeed * vectorModifier;
	
	currentFrame++;
	if(currentFrame % accelerateEvery)
		currentSpeed+=accelerateBy;
	if(currentSpeed > maximumSpeed)
		currentSpeed = maximumSpeed;
		
	currentHeight+=d;
	done = false;
		
	if(vectorModifier < 0)
	{
		if(currentHeight <= targetHeight)
		{
			done = true;
			currentHeight = targetHeight;
		}
	}
	else
	{
		if(currentHeight >= targetHeight)
		{
			done = true;
			currentHeight = targetHeight;
		}
	}
	
	for(c=1;c<=4;c++)
	{
		document.getElementById("col"+c).style.height = currentHeight+"px";
	}
	
	if(!done)
	{
		setTimeout('moveColumns()',animFrequency);
	}
	else
	{
		currentlyShowing = !currentlyShowing;
		isMoving = false;
	}
}
