var openMenu = "0";
var childMenuLinkHeight = 25;

var animatetime = 200;

function hideMenu(menuId){
	for(i=1; i <= submenuAmount; i++){
		if(i != menuId){
			eval("submenu"+i).slideup();
		}
	}
}

function toggleMenu(menuId) {
	hideMenu(menuId);
	eval("submenu"+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 == "menuChild"+openMenu) {
		this.isExpanded = "yes";

		// switch the arrow icon
		var targetImage = document.getElementById("menuParent"+openMenu).getElementsByTagName('img')[0];
		targetImage.src = targetImage.src.substr(0, targetImage.src.length - 5)+"E.gif";

		// switch the background color
		document.getElementById("menuParent"+openMenu).className = "menuItem menuItem"+openMenu+" menuParent menuParent"+openMenu;

		openMenu = "0";
	} else {
		this.isExpanded =  "no";
	}

	// get the height of this div
	this.contentheight = (this.divObj.style.height) ? this.divObj.style.height.replace("px", "") : 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
			var targetImage = document.getElementById("menuParent"+this.divNumber).getElementsByTagName('img')[0];
			targetImage.src = targetImage.src.substr(0, targetImage.src.length - 5)+"E.gif";
			document.getElementById("menuParent"+this.divNumber).className = "menuItem menuItem"+this.divNumber+" menuParent menuParent"+this.divNumber;

			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
			var targetImage = document.getElementById("menuParent"+this.divNumber).getElementsByTagName('img')[0];
			targetImage.src = targetImage.src.substr(0, targetImage.src.length - 5)+"C.gif";
			document.getElementById("menuParent"+this.divNumber).className = "menuItem menuParent menuParent"+this.divNumber;

			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)
	}
}
