//Standard way of coding these sliding divs
//this is the most stable way only allowing one to grow or shrink at a time

/*
<div id="Div1" style="overflow: hidden; height: 20px; width: 200px; background-color:yellow; text-align:center"
	onclick="if(typeof Div1CLS == 'undefined'){ Div1CLS = new DivScroller('Div1', 20, 200, 1); } if(RollOkay()) Div1CLS.Roll();">
</div>
*/

//Alternate coding unstable in some situations
//removing if(RollOkay()) causes more than one div to be able to move at a time causing some unwanted
//irregularities in certain situations.

/*
<div id="Div1" style="overflow: hidden; height: 20px; width: 200px; background-color:yellow; text-align:center"
	onclick="if(typeof Div1CLS == 'undefined'){ Div1CLS = new DivScroller('Div1', 20, 200, 1); } Div1CLS.Roll();">
</div>
*/

//KEEP TRACK OF ALL DIVSCROLLERS SO WE CAN SHRINK IF NEED BE
var DivScrollers = new Array();
var divScrolllerCount = 0;

//DETERMINES IF ONLY ONE DIV PER GROUP MAY BE EXPANDED OR NOT
//TRUE = ONLY ONE FALSE = ANY NUMBER
var ExpandOnlyOne = true;

function DivScroller(divID, minHeight, maxHeight, Group)
{
	//INIT AND SAVE ALL PROPERTIES FOR THIS CLASS
	this.name = divID + 'CLS';
	this.OBJ = document.getElementById(divID);
	this.minHeight = minHeight;
	this.maxHeight = maxHeight;
	this.unRolled = false;
	this.unRolling = false;
	this.Rolling = false;
	this.Group = Group;
	this.Speed = maxHeight / 10;
	this.curHeight = minHeight;
	
	//INCREMENT THE NUMBER OF SCROLLERS WE HAVE LOADED
	DivScrollers[divScrolllerCount] = this;
	divScrolllerCount += 1;
}
function RollOkay()
{
	//LOOP THROUGH ALL OF THE SCROLLERS AND CHECK TO SEE IF ANY ARE SCROLLING
	for(i = 0; i< divScrolllerCount; i++)
	{
		//IF THEIR IS ANYTHING SCROLLING IT IS NOT OKAY FOR A NEW ONE TO SCROLL YET
		if(DivScrollers[i].Rolling == true || DivScrollers[i].unRolling == true)
			return false;
		else
			return true;
	}
}
function Roll()
{
	//USED TO DETERMINE IF WE JUST FINISHED CHANGING ONES ROLL/UNROLLED STATE
	var StateChanged = false;
	
	//IF THERE IS ONLY SUPPOSED TO BE ONE EXPANDED AT A TIME CHECK TO SEE IF THERE ARE ANY OTHERS AND SHRINK THEM
	if(ExpandOnlyOne == true)
	{
		for(i = 0; i< divScrolllerCount; i ++)
		{
			if(DivScrollers[i] != this && this.unRolling == false && this.Rolling == false && DivScrollers[i].Group == this.Group)
			{
				if(DivScrollers[i].unRolled == true && DivScrollers[i].Rolling == false && DivScrollers[i].unRolling == false)
					DivScrollers[i].Roll();
			}
		}
	}
	
	//IF THIS CLASS IS UNROLLING THEN PERFORM THE CALCULATION TO SCROLL THE DIV DOWN
	if(this.unRolling)
	{
		//INCREMENT THE CURHEIGHT OF THE DIV
		this.curHeight += this.Speed * ((this.maxHeight - this.curHeight) / this.maxHeight);
		
		//IF THE CURHEIGHT MEETS THE THRESHOLD OF 2 PIXELS THEN JUST MAKE IT MAXHEIGHT
		//OTHERWISE SET THE TIMEOUT SO WE WILL REPEAT THE EXPANSION AND CREATE AN ANIMATED APPEARANCE
		if(this.curHeight < (this.maxHeight - 2))
			setTimeout(this.name + '.Roll()', 10);
		else
		{
			this.curHeight = this.maxHeight;
			this.unRolling = false;
			this.unRolled = true;
			this.OBJ.style.overflow = 'auto';
			StateChanged = true;
		}
	}
	
	//IF THIS CLASS IS ROLLING UP THEN PERFORM THE CALCULATION TO SCROLL THE DIV UP
	if(this.Rolling)
	{
		//INCREMENT THE CURHEIGHT OF THE DIV
		this.curHeight -= this.Speed * ((this.curHeight - this.minHeight) / this.maxHeight);
		
		//IF THE CURHEIGHT MEETS THE THRESHOLD OF 2 PIXELS THEN JUST MAKE IT MINHEIGHT
		//OTHERWISE THE THE TIMEOUT SO WE WILL REPEAT THE SHRINKING AND CREATE AN ANIMATED APPEARANCE
		if(this.curHeight > (this.minHeight + 2))
			setTimeout(this.name + '.Roll()', 10);
		else
		{
			this.curHeight = this.minHeight;
			this.Rolling = false;
			this.unRolled = false;
			StateChanged = true;
		}
	}
	
	//STARTS THE PROCESS OF EXPANSION CONTRACTION IF THE CLASS IS NOT ALREADY IN THE STATE OF DOING SO
	if(this.Rolling == false && this.unRolling == false && StateChanged == false)
	{
		//IF THIS CLASS IS UNROLLED THEN IT NEEDS TO ROLLUP
		//OTHERWISE IT NEEDS TO UNROLL
		if(this.unRolled == false)
		{
			this.unRolling = true;
			this.Roll();
		}
		else
		{
			this.OBJ.scrollTop = 0;
			this.OBJ.style.overflow = 'hidden';
			this.Rolling = true;
			this.Roll();
		}
	}
	
	//ACTUALLY MAKE THE OBJECT MEET THE NEW SIZE REQUIREMENTS
	this.OBJ.style.height = this.curHeight + 'px';
}

//PROTOTYPE THE ROLL FUNCTION SO IT CAN BE USED
DivScroller.prototype.Roll = Roll;
