function CAnimatedCollapse(ObjectName, Intervalms, SlideAnimationLength, SliderType,EndHeight) 
{
	this.Intervalms 	 				= Intervalms; //times to run the script
	this.SlideAnimationLength = SlideAnimationLength; //time to slide up or down
	this.ObjectName	   				= ObjectName;
  this.SliderType  					= SliderType; // smooth or static 
  this.EndHeight						= EndHeight;

  
	this.TimerID 		= null;
	this.StartTime 	= 0;
	this.DivObject	= null;
	this.bMoving 		= false;
	this.Direction 	= "none";
}

CAnimatedCollapse.prototype.toggle = function()
{
  if(this.bMoving) {
    return;
  }
 if(document.getElementById(this.ObjectName).style.display != "block") {
    this.Direction = "down";
	} else {
    this.Direction = "up"; 
  }
  this.bMoving = true;
  this.startslide();
}

CAnimatedCollapse.prototype.slidedown = function()
{
  if(this.bMoving) {
    return; 
  }
  if(document.getElementById(this.ObjectName).style.display != "none") {
    return; // cannot slide down something that is already visible
	}
  this.bMoving = true;
  this.Direction = "down";
  this.startslide();
}
 
CAnimatedCollapse.prototype.slideup = function()
{
  if(this.bMoving) {
    return;
  }
  if(document.getElementById(this.ObjectName).style.display == "none") {
    return; // cannot slide up something that is already hidden
  }
  this.bMoving = true;
  this.Direction = "up";
  this.startslide();
}

CAnimatedCollapse.prototype.startslide = function()
{
  this.DivObject = document.getElementById(this.ObjectName);
  this.StartTime = (new Date()).getTime();
 
  if(this.Direction == "down"){
    this.DivObject.style.height = "1px";
  }
  this.DivObject.style.display = "block";
  var ThisObject=this;
  this.TimerID = setInterval(function(){ThisObject.slidetick()},this.Intervalms);
}

CAnimatedCollapse.prototype.slidetick = function()
{
  var ElapsedTime = (new Date()).getTime() - this.StartTime;
  if (ElapsedTime >= this.SlideAnimationLength) {
    this.endSlide()
  } else {
    var NextHeight = 0;
    if (this.SliderType == 'smooth') {
      var DistancePercent = 0;  
      if (this.Direction=="down") {
        DistancePercent = this.curveincrement(ElapsedTime/this.SlideAnimationLength); 
      } else {
        DistancePercent = (1-this.curveincrement(ElapsedTime/this.SlideAnimationLength));
      }
      NextHeight = Math.round(DistancePercent * this.EndHeight);
    } else {
      var d =Math.round(ElapsedTime / this.SlideAnimationLength * this.EndHeight);
      if(this.Direction == "up") {
        d = this.EndHeight - d;
      }
      NextHeight = d;
    }  
    if(NextHeight < 1) { //make sure heigth doesn't go below 1 px (causes flicker)
      NextHeight = 1;
    }
	  this.DivObject.style.height = NextHeight +"px";
  }
  return;
}

CAnimatedCollapse.prototype.endSlide = function()
{
  clearInterval(this.TimerID);
  if(this.Direction == "up") {
    this.DivObject.style.display = "none";
    this.DivObject.style.height =  "0px";
  } else {
  	this.DivObject.style.display = "block";
  	this.DivObject.style.height = this.EndHeight + "px";
	}
  delete(this.bMoving);
  delete(this.TimerID);
  delete(this.StartTime);
  delete(this.obj);
  delete(this.Direction);
  return;
}

CAnimatedCollapse.prototype.curveincrement=function(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}
