//Animated Collapsible DIV- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Updated June 27th, 07'. Added ability for a DIV to be initially expanded.
var openMenu = 0;
var childMenuLinkHeight = 23;
var animatetime = 200; 
function toggleMenu(menuId) {
	eval(menuId).slideit();
}

function menuCollapse(divId){
	this.divId=divId;
	this.divNumber = divId.substr(divId.length - 1, 1);
	this.divObj = document.getElementById(divId);
	this.divObj.style.overflow="hidden";
	this.timelength=animatetime;
	
	if (divId == "submenu_"+openMenu) {
		this.isExpanded = "yes";
		openMenu = 0;
	} else { 
		this.isExpanded =  "no";
	}
	
	// get the height of this div (unless set explicitly)
	this.contentheight = parseInt(this.divObj.style.height);
	if(isNaN(this.contentheight)) {
		this.contentheight=parseInt(this.divObj.getElementsByTagName("a").length * childMenuLinkHeight);
	}
	
	if(this.isExpanded != "yes") {
		this.divObj.style.height = 0;
	} else {
		this.divObj.style.height = this.contentheight+"px";
	}
}

menuCollapse.prototype._slideengine=function(direction){
	var elapsed=new Date().getTime()-this.startTime //get time animation has run
	var thisobj=this
	
	if (elapsed<this.timelength){ //if time run is less than specified length
		var distancepercent=(direction=="down")? menuCollapse.curveincrement(elapsed/this.timelength) : 1-menuCollapse.curveincrement(elapsed/this.timelength)
	this.divObj.style.height=distancepercent * this.contentheight +"px"
	this.runtimer=setTimeout(function(){thisobj._slideengine(direction)}, 10)
	} else { //if animation finished
		this.divObj.style.height=(direction=="down")? this.contentheight+"px" : 0
		this.isExpanded=(direction=="down")? "yes" : "no" //remember whether content is expanded or not
		this.runtimer=null;
	}
}

menuCollapse.prototype.slidedown=function(){
	if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running
		if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
			alert("Please wait until document has fully loaded then click again")
		else if (parseInt(this.divObj.style.height)==0) { //if content is collapsed
			this.startTime=new Date().getTime() //Set animation start time
			this._slideengine("down");
		}
	}
}

menuCollapse.prototype.slideup=function(){
	if (typeof this.runtimer=="undefined" || this.runtimer==null){ //if animation isn't already running or has stopped running
		if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
			alert("Please wait until document has fully loaded then click again")
		else if (parseInt(this.divObj.style.height)==this.contentheight){ //if content is expanded		
			this.startTime=new Date().getTime()
			this._slideengine("up");
		}
	}
}

menuCollapse.prototype.slideit=function(){
	if (isNaN(this.contentheight)) {//if content height not available yet (until window.onload)
		alert("Please wait until document has fully loaded then click again")
	}
	else if (parseInt(this.divObj.style.height) == 0) {
		this.slidedown();
	}
	else if (parseInt(this.divObj.style.height) == this.contentheight) {
		this.slideup();
	} else { // wtf?
		this.divObj.style.height = this.contentheight;
		this.slideup();
	}
}

// -------------------------------------------------------------------
// A few utility functions below:
// -------------------------------------------------------------------
menuCollapse.curveincrement=function(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}

menuCollapse.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.addEventListener) {
		target.addEventListener(tasktype, functionref, false)
	}
	else if (target.attachEvent) {
		target.attachEvent(tasktype, functionref)
	}
}
// ------------------------
