//Based on Animated Collapsible DIV- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Modified by Micromania

function animatedcollapse(divId, divIdanchor, animatetime, persistexpand,addtotop,addtoleft){
	this.divId=divId
	this.divIdanchor = divIdanchor
	this.divObj=document.getElementById(divId)
	this.divObjanchor=document.getElementById(divIdanchor)
	
	this.divObjfixed = document.getElementById(divId + 'fixed');
	this.divObj.style.overflow="hidden";
	this.timelength=animatetime
	
	this.contentheight=parseInt(this.divObj.style.height)
	this.contentwidth=parseInt(this.divObj.style.width)
	var thisobj=this
	
	if (isNaN(this.contentheight)){ //if no CSS "height" attribute explicitly defined, get DIV's height on window.load
		animatedcollapse.dotask(window, function(){thisobj._getheight(persistexpand)}, "load")
	}
	
	this.divObjfixed.style.left = animatedcollapse.calculate_offsetLeft(this.divObjanchor)+addtoleft+"px";
	this.divObjfixed.style.top = animatedcollapse.calculate_offsetTop(this.divObjanchor)+addtotop+"px";
}

animatedcollapse.calculate_offsetLeft = function(elem)
{
	var p = elem.offsetParent;
	return elem.offsetLeft;
	/*
	if(!p)
	{
		return elem.offsetLeft;
	}
	else
	{
		return elem.offsetLeft+animatedcollapse.calculate_offsetLeft(p);
	}
	*/
}
animatedcollapse.calculate_offsetTop = function(elem)
{
	var p = elem.offsetParent;
	return elem.offsetTop;
	/*
	if(!p)
	{
		return elem.offsetTop;
	}
	else
	{
		return elem.offsetTop+animatedcollapse.calculate_offsetTop(p);
	}
	*/
}


animatedcollapse.prototype._getheight=function(persistexpand){

	this.contentheight=this.divObjfixed.offsetHeight
	this.contentwidth=this.divObjfixed.offsetWidth
	
	this.divObj.style.width=0
	this.divObj.style.height=0 //collapse content
	//this.divObj.style.visibility="visible";
	this.divObjfixed.style.visibility = "visible";
}

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 iOpacity=(direction=="down")? animatedcollapse.opacityincrement(elapsed/this.timelength) : 1-animatedcollapse.opacityincrement(elapsed/this.timelength);
		var distancepercent=(direction=="down")? animatedcollapse.curveincrement(elapsed/this.timelength) : 1-animatedcollapse.curveincrement(elapsed/this.timelength);
		this.divObj.style.height=distancepercent * this.contentheight +"px";
		this.divObj.style.width=distancepercent * this.contentwidth +"px";
		
		this.divObj.style.opacity = iOpacity;
		this.divObj.style.MozOpacity = iOpacity;
		this.divObj.style.KhtmlOpacity = iOpacity;
		this.divObj.style.filter = "alpha(opacity=" + iOpacity * 100 + ")";
		this.runtimer=setTimeout(function(){thisobj._slideengine(direction)}, 10)
	}
	else{ //if animation finished
		this.divObj.style.height=(direction=="down")? this.contentheight+"px" : 0
		this.divObj.style.width=(direction=="down")? this.contentwidth+"px" : 0
		this.divObj.style.opacity = (direction=="down")? 1 : 0;
    	this.divObj.style.MozOpacity = (direction=="down")? 1 : 0;
    	this.divObj.style.KhtmlOpacity = (direction=="down")? 1 : 0;
		this.divObj.style.filter=(direction=="down")? "alpha(opacity=100)" : "alpha(opacity=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)==this.contentheight){ //if content is expanded
			this.startTime=new Date().getTime()
			this._slideengine("up")
		}
	}
}

animatedcollapse.prototype.slideit=function(){
	if (isNaN(this.contentheight)) //if content height not available yet (until window.onload)
		alert("Merci d'attendre le chargement complet de la page et essayez à nouveau")
	else if (parseInt(this.divObj.style.height)==0)
		this.slidedown()
	else if (parseInt(this.divObj.style.height)==this.contentheight)
		this.slideup()
}

// -------------------------------------------------------------------
// 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
}

animatedcollapse.opacityincrement=function(percent){
	return Math.round(percent*100)/100;
}

animatedcollapse.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)
}
