var beginHeight = 53.3;
var ContentHeight = 160;					// hoogte van uitgeklapte div's
var TimeToSlide = 250.0;					// aantal miliseconde om te sliden
var accordionIds = new Array(1,2,3);		// ID-numbers all participating Divs
var sliding = false;						// Geeft aan of een slide bezig is
var openAccordion = '';						// contains id-name van eventuele open div
var waiting = '';							// nr. van div in de wacht
var restoreStartPos = false;				// als true -> vakken terugzetten naar beginHeight


function runAccordion(index) {

	restoreStartPos = false;

	if(openAccordion != index)	// Checks if div is already opened
	{
		if (sliding != true)		// Check if function is locked during slide
		{								
			sliding = true;				// lock function while sliding
			openAccordion = index;	
			setTimeout("animate("+ index + ")", 150);		//begint na 33ms met animate();
		}
		else {
			if (restoreStartPos == false)	// als startpositie niet ingenomen mag worden
			{
				waiting = index;
			}
		}
	}
}


function checkWaiting() {

	if (waiting != ''){
		index = waiting;
		waiting = 0;
		if (restoreStartPos == false) // als startpositie ingenomen mag worden
		{
			runAccordion(index);
		}
	}

}


/*
* control animation
*/
function animate (index)
{ 
	if (restoreStartPos == false)
	{
		openingId = "Accordion" + index + "Content";

		if (waiting)
		{
			openingId = "Accordion" + waiting + "Content";
		}

		for (i=0;i<accordionIds.length;i++)
		{
			if ("Accordion" + accordionIds[i] + "Content" == openingId)
			{
				animateOpen(new Date().getTime() , TimeToSlide , '' , openingId);
			}
			else
			{
				animateClose(new Date().getTime() , TimeToSlide , 'Accordion' + accordionIds[i] + 'Content' , '');	
			}
		}
	}
}

/*
* Open a div
*/
function animateOpen(lastTick, timeLeft, closingId, openingId)
{ 

	var curTick = new Date().getTime();		
	var elapsedTicks = curTick - lastTick;	

	var opening = (openingId == '') ?
	  null : document.getElementById(openingId);

	if(timeLeft <= elapsedTicks)
	{
		if(opening != null) 
		{
			opening.style.height = ContentHeight + 'px';
		}

		sliding = false;
		checkWaiting(); 
		return;
	}

	timeLeft -= elapsedTicks;
	var newClosedHeight =
	  Math.round((timeLeft/TimeToSlide) * ContentHeight);

	if(opening != null)
	{
		if(opening.style.display != 'block') 
		{
			opening.style.display = 'block';
		}
		height = parseFloat(opening.style.height);
		newheight = (ContentHeight - newClosedHeight);
		if (height <= newheight)
		{
			opening.style.height = newheight + 'px';
		}
	}
	if (restoreStartPos == false)
	{
		  setTimeout("animateOpen(" + curTick + "," + timeLeft + ",'" + closingId + "','" + openingId + "')", 1);
	}
}

/*
* Close a div
*/
function animateClose(lastTick, timeLeft, closingId, openingId)
{ 
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var closing = (closingId == '') ?
		null : document.getElementById(closingId);

	if(timeLeft <= elapsedTicks)
	{
		if(closing != null)
		{
		  closing.style.display = 'none';
		  closing.style.height = '0px';
		}
		return;
	}

	timeLeft -= elapsedTicks;
	var newClosedHeight =
	  Math.round((timeLeft/TimeToSlide) * ContentHeight);

	if(closing != null) 
	{
		height = parseFloat(closing.style.height);
		if (height >= newClosedHeight)
		{
			closing.style.height = newClosedHeight + 'px';
		}	
	}
	if (restoreStartPos == false)
	{
		setTimeout("animateClose(" + curTick + "," + timeLeft + ",'" + closingId + "','" + openingId + "')", 1);
	}
}


/*
* Triggers the restore of the divs
*/

function triggerRestore() {

		restoreStartPos = true;	
		setTimeout("restoreAccordion()", 33);
}


/*
* Restores the startposition of the divs
*/
function restoreAccordion () {

	if (restoreStartPos == true) // als startpositie ingenomen mag worden
	{
		waiting = 0;
		openAccordion = '';
		var div = '';
		for (i=0;i<accordionIds.length;i++)
		{
			div = document.getElementById("Accordion" + accordionIds[i] + "Content");
			div.style.display = 'block';
			div.style.height = beginHeight + 'px';
		}
		sliding = false;
	}
}
