// JavaScript Document

function ShrinkObject(element)
{
	var me = this;
	
	var defaultTime = SECOND / 3;
	this.element = element;
	this.callback = null;
	var changing = false;
	var expanded = true;
	
	this.stepSize =  10;
	this.maxHeight = getHeight(this.element);
	
	var originalOverflow = element.style.overflow;
	
	if(!this.element.style.height)
	{
		this.element.style.height = getHeight(element)+'px';
	}
	
	this.isChanging = function()
	{
		return changing;
	};
	
	this.isExpanded = function()
	{
		return expanded;
	};
	
	this.hide = function()
	{
		this.element.style.height = '0px';
	};
	this.show = function()
	{
		this.element.style.height = maxHeight+'px';
	};
	
	this.collapse = function(time)
	{
		element.style.overflow = 'hidden';
		changing = true;
		time = time ? time : defaultTime;
		var height = parseInt(me.element.style.height);
		height = me.stepSize > height ? 0 : height - me.stepSize;
		me.element.style.height = height+'px';
		if(height == 0)
		{
			//me.element.style.display = 'none';
			changing = false;
			expanded = false;
			if(me.callback)
			{
				setTimeout(function(){me.callback();}, 0);
			}
			return;
		}
		setTimeout(function(){me.collapse(time);}, time / (me.maxHeight / me.stepSize));
	};
	
	this.expand = function(time)
	{
		//me.element.style.display = 'block';
		changing = true;
		time = time ? time : defaultTime;
		var height = parseInt(me.element.style.height);
		height = height + me.stepSize > me.maxHeight ? me.maxHeight : height + me.stepSize;
		me.element.style.height = height+'px';
		if(height == me.maxHeight)
		{
			element.style.overflow = originalOverflow;
			changing = false;
			expanded = true;
			if(me.callback)
			{
				setTimeout(function(){me.callback();}, 0);
			}
			return;
		}
		setTimeout(function(){me.expand(time);}, time / (me.maxHeight / me.stepSize));
	}
}

function CollapsableDivObject(div)
{
	var me = this;
	
	this.element = div;
	this.collapseBar = null;
	this.collapseBox = null;
	
	var animated = div.className.containsWord('animated');
	
	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);
		}
	}
	
	if(!this.collapseBar || !this.collapseBox)
	{
		throw new Exception('Collapse bar and box not defined');
	}
	
	var shrinker = new ShrinkObject(this.collapseBox);
	
	this.boxHeight = getHeight(this.collapseBox);
	
	this.collapsed = false;
	this.collapse = function()
	{
		if(animated)
		{
			if(shrinker.isChanging())
			{
				return;
			}
			shrinker.callback = me.signalCollapsed;
			shrinker.collapse();
		}
		else
		{
			me.collapseBox.style.display = 'none';
			me.signalCollapsed();
		}
	};
	this.expand = function()
	{
		if(animated)
		{
			if(shrinker.isChanging())
			{
				return;
			}
			shrinker.callback = me.signalExpanded;
			shrinker.expand();
		}
		else
		{
			me.collapseBox.style.display = 'block';
			me.signalExpanded();
		}
	};
	
	this.signalCollapsed = function()
	{
		me.collapsed = true;
		me.toggleButton.className = 'expand_button';
	};
	this.signalExpanded = function()
	{
		me.collapsed = false;
		me.toggleButton.className = 'collapse_button';
	};
	
	this.toggleButton = document.createElement('div');
	this.toggleButton.className = 'collapse_button';
	this.toggleButton.onclick = function()
	{
		if(me.collapsed)
		{
			me.expand();
		}
		else
		{
			me.collapse();
		}
	};
	this.collapseBar.appendChild(this.toggleButton);
	
	if(this.element.className.containsWord('collapsed'))
	{
		me.collapse();
	}
}
