//Animated Trasition Table 
var calwrapper,tableorig,tablecopy;
var animating = false;

function prepAnimation() {
	if(!document.getElementById) return;
	calwrapper = document.getElementById('calwrapper');
	tableorig = document.getElementById('calendar');
	calwrapper.style.height = calwrapper.offsetHeight+"px";
	
	document.getElementById('fyi').onclick = function(){ animateTransition("FYI"); return false };
	document.getElementById('y2').onclick = function(){ animateTransition("Y2"); return false };
	document.getElementById('glue').onclick = function(){ animateTransition("GLUE"); return false };
	document.getElementById('precollege-cal').onclick = function(){ animateTransition("Pre-College"); return false };
	document.getElementById('general').onclick = function(){ animateTransition("General"); return false };
	document.getElementById('all').onclick = function(){ animateTransition("all"); return false };

	var address = document.location.href.split("#");
	if(address.length==2) updateTable(document.getElementById('calendar'),address[1]); //only update the screen if the tab is specified in the address
}


function updateTable(caltable,category) {
	var catlist = document.getElementById('listtabs')	//list of categories

	/* update display of table */
	for(var i=0; i < caltable.rows.length; i++) {
		var rowcat = caltable.rows[i].cells[0].innerHTML; //category of row i
		if(rowcat == "Category" || rowcat == "all" || category == "all") //speical cases
			caltable.rows[i].className = "" //display the row
		else {	//the row is a "normal" category, check for match
			rowcat = rowcat.split(" ")
			caltable.rows[i].className = "hidden"		//hide the row as default, then change to display if matched
			for(var j=0; j < rowcat.length; j++) {
				if(rowcat[j] == category) {
					caltable.rows[i].className = ""		//display the row
					break
				}
			}
		} //else
	}
	
	/* update display of tabs */
	var x = ['fyi','y2','glue','precollege-cal','general','all'];
	for (var i in x) {
		var listlabel = document.getElementById(x[i]);
		listlabel.className = ((listlabel.innerHTML==category) || (category=="all" && listlabel.innerHTML=="All Events"))? "current" : "";
	}
}


function animateTransition(category) {
	if(animating) return;
	animating = true;
	tableorig = document.getElementById('calendar');
	tablecopy = tableorig.cloneNode(true);
	with(tablecopy) {
		id = "calcopy";
		with(style) {
			position = "absolute";
			zIndex = "1";
			left = "0";
			top = "0";
			opacity = "1";
			filter = "alpha(opacity=100)";
		}
	}
	document.getElementById('calwrapper').appendChild(tablecopy);
	tableorig.style.visibility = "hidden";
	updateTable(tableorig,category);
	animateEngine({
		category:category,
		startH:parseInt(calwrapper.style.height),
		dist:tableorig.offsetHeight-parseInt(calwrapper.style.height),
		timer:null,
		timelength:(Math.abs(this.dist)>600)? 1000 : 450,
		startTime:(new Date()).getTime() });
}


function animateEngine(ani) {
	var elapsed = (new Date()).getTime() - ani.startTime;
	if(elapsed < ani.timelength) {
		var percent = coscurve(elapsed/ani.timelength);
		calwrapper.style.height = Math.round(percent * ani.dist + ani.startH) + "px";
		tablecopy.style.opacity = 1-percent;
		tablecopy.style.filter = "alpha(opacity=" + 100*(1-percent) + ")";
		if(percent > .4) tableorig.style.visibility = "visible";
		ani.timer = setTimeout(function(){ animateEngine(ani) }, 5);
	} else { //animation has finished
		calwrapper.style.height = Math.round(ani.startH + ani.dist) + "px";
		ani.timer = null;
		tablecopy.style.opacity = "0";
		tablecopy.style.filter = "alpha(opacity=0)";
		with(tablecopy) {
			with(style) {
				position = "";
				zIndex = "";
				left = "";
				top = "";
			}
		}		
		
		calwrapper.removeChild(tablecopy);
		tablecopy = null;
		animating = false;
	}
}


addLoadEvent(prepAnimation);