// JavaScript Document
//Animated Collapsible DIV- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated Aug 1st, 07'. Fixed bug with "block" parameter not working when persist is enabled
//Updated June 27th, 07'. Added ability for a DIV to be initially expanded.

var uniquepageid=window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, "") //get current page path and name, used to uniquely identify this page for persistence feature
var heightNum = 0

function animatedcollapse(divId, divParentId, divHeight, imageName, animatetime, persistexpand, initstate){
	this.divId=divId
	this.divObj=document.getElementById(divId)
	this.divObj.style.overflow="hidden"
	this.divObj.style.height = divHeight + 'px'

	if(divParentId != null){
		this.divParentId = divParentId
		this.divParentObj = document.getElementById(divParentId)
		this.parentDivNumHeight=parseInt(divHeight)
		this.parentPreviousHeight=parseInt(divHeight)
	}
	else
	{
		this.divParentId = null
		this.divParentObj = null
		this.parentDivNumHeight=parseInt(divHeight)
		this.parentPreviousHeight=parseInt(divHeight)
	}
		
	this.timelength=animatetime
	this.initstate=(typeof initstate!="undefined" && initstate=="block")? "block" : "contract"
	this.contentheight=parseInt(divHeight)
	
	if (this.initstate == 'block'){
		img=document.getElementById(imageName);
		img.src=img.src.replace(/expand/,"collapse")	
	}
	
	var thisobj=this
	
	if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes" && this.isExpanded!="")//Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
		this.divObj.style.height=0 //just collapse content if CSS "height" attribute available
}

animatedcollapse.prototype._getheight=function(persistexpand){
	this.contentheight=this.divObj.offsetHeight
	
	if (!persistexpand && this.initstate=="contract" || persistexpand && this.isExpanded!="yes"){ //Hide DIV (unless div should be expanded by default, OR persistence is enabled and this DIV should be expanded)
		this.divObj.style.height=0 //collapse content
		this.divObj.style.visibility="visible"
	}
	else //else if persistence is enabled AND this content should be expanded, define its CSS height value so slideup() has something to work with
		this.divObj.style.height=this.contentheight+"px"
}

animatedcollapse.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")? animatedcollapse.curveincrement(elapsed/this.timelength) : 1-animatedcollapse.curveincrement(elapsed/this.timelength)
		
		if(this.divParentObj == null)
		{
			heightNum=distancepercent * this.parentDivNumHeight;
			this.divObj.style.height = heightNum +"px"
		}
		else
			this.divObj.style.height=distancepercent * this.contentheight +"px"	
		
		if(this.divParentObj != null)
		{
			if (direction == "down"){
				this.parentDivNewHeight = this.parentDivNumHeight + (distancepercent * this.contentheight);
			}
			if (direction == "up"){
				this.parentDivNewHeight = this.parentPreviousHeight + (distancepercent * this.contentheight);
			}
			this.divParentObj.style.height = this.parentDivNewHeight +"px"
		}
		
		this.runtimer=setTimeout(function(){thisobj._slideengine(direction)}, 10)
	}
	else{ //if animation finished
		this.divObj.style.height=(direction=="down")? this.divObj.offsetHeight+"px" : 0
		this.isExpanded=(direction=="down")? "yes" : "no" //remember whether content is expanded or not
		this.runtimer=null
	}
}


animatedcollapse.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")
		}
	}
}

animatedcollapse.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)>0){ //if content is expanded
			this.startTime=new Date().getTime()
			this._slideengine("up")
		}
	}
}

animatedcollapse.prototype.slideit=function(image){
	imageName = image.src
	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(this.divParentId != null){
			this.parentDivNumHeight = parseInt(this.divParentObj.style.height);
			this.parentPreviousHeight = parseInt(this.divParentObj.style.height) - this.contentheight
		}
		else
		{
			if (parseInt(this.divObj.style.height) !=0)
			{
				this.parentDivNumHeight = parseInt(this.divObj.style.height);
				this.parentPreviousHeight = parseInt(this.divObj.style.height) - this.contentheight
			}
		}
		this.slidedown()
		image.src=imageName.replace(/expand/,"collapse")
	}
	else if (parseInt(this.divObj.style.height)>0){
		if(this.divParentId != null){
			this.parentDivNumHeight = parseInt(this.divParentObj.style.height);
			this.parentPreviousHeight = parseInt(this.divParentObj.style.height) - this.contentheight
		}
		else
		{
			this.parentDivNumHeight = parseInt(this.divObj.style.height);
			this.parentPreviousHeight = parseInt(this.divObj.style.height) - this.contentheight
		}
		this.slideup()
		image.src=imageName.replace(/collapse/,"expand")
	}
}

// -------------------------------------------------------------------
// A few utility functions below:
// -------------------------------------------------------------------

animatedcollapse.curveincrement=function(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}