// JavaScript Document

function CollapsableDivObject(div)
{
	var me = this;
	
	this.element = div;
	this.collapseBar = null;
	this.collapseBox = null;
	
	var divs = div.getElementsByTagName('div');
	for(var i = 0; i < divs.length; ++i)
	{
		if(divs.item(i).className.containsWord('collapse_bar'))
		{
			this.collapseBar = divs.item(i);
		}
		if(divs.item(i).className.containsWord('collapse_box'))
		{
			this.collapseBox = divs.item(i);
		}
	}
	this.boxHeight = this.collapseBox.offsetHeight;
	
	if(!this.collapseBar || !this.collapseBox)
	{
		throw new Exception('Collapse bar and box not defined');
	}
	
	this.collapsed = false;
	this.changing = false;
	this.collapse = function()
	{
		if(me.changing)
		{
			return;
		}
		me.changing = true;
		shrink(me.collapseBox, 25, 30, me.signalCollapsed);
		me.toggleButton.className = 'expand_button';
	};
	this.expand = function()
	{
		if(me.changing)
		{
			return;
		}
		me.changing = true;
		grow(me.collapseBox, 25, me.boxHeight, 30, me.signalExpanded);
		me.toggleButton.className = 'collapse_button';
	};
	this.signalCollapsed = function()
	{
		me.changing = false;
		me.collapsed = true;
	};
	this.signalExpanded = function()
	{
		me.changing = false;
		me.collapsed = false;
	};
	
	this.toggleButtonWrapper = document.createElement('div');
	this.toggleButtonWrapper.className = 'collapse_button_wrapper';
	this.collapseBar.appendChild(this.toggleButtonWrapper);
	
	this.toggleButton = document.createElement('div');
	this.toggleButton.className = 'collapse_button';
	this.toggleButton.onclick = function()
	{
		if(me.collapsed)
		{
			me.expand();
		}
		else
		{
			me.collapse();
		}
	};
	this.toggleButtonWrapper.appendChild(this.toggleButton);
	
	
	if(this.element.className.containsWord('collapsed'))
	{
		me.collapseBox.style.height = '0px';
		me.toggleButton.className = 'expand_button';
		me.collapsed = true;
	}
}

function shrink(element, interval, step, callback)
{
	if(!step)
	{
		step = 50;
	}
	if(!element.style.height)
	{
		element.style.height = element.offsetHeight+'px';
	}
	var height = parseInt(element.style.height, 10);
	if(height <= 0)
	{
		element.style.display = 'none';
		if(callback)
		{
			setTimeout(function(){callback();}, 0);
		}
		return;
	}
	element.style.height = (step > height ? 0 : height-step)+'px';
	setTimeout(function(){shrink(element, interval, step, callback);}, interval);
}

function grow(element, interval, maxHeight, step, callback)
{
	element.style.display = 'block';
	if(!step)
	{
		step = 50;
	}
	if(!element.style.height)
	{
		element.style.height = element.offsetHeight+'px';
	}
	var height = parseInt(element.style.height, 10);
	if(height >= maxHeight)
	{
		if(callback)
		{
			setTimeout(function(){callback();}, 0);
		}
		return;
	}
	element.style.height = (height+step > maxHeight ? maxHeight : height+step)+'px';
	setTimeout(function(){grow(element, interval, maxHeight, step, callback);}, interval);
}
