
var cm_options = new collapse_options();

function collapse_options()
{
	this.container_id = 0;
	this.content_id = 0;
	this.clickable_id = 0;
	this.class_open = null;
	this.class_close = null;
}

function collapse_init(container, clickable, content, class_open, class_close) 
{
	if(class_open != 'undefined')
		cm_options.class_open = class_open;
	if(class_close != 'undefined')
		cm_options.class_close = class_close;
	
	cm_options.container_id = container;
	cm_options.content_id = content;
	cm_options.clickable_id = clickable;
	
	var clickables = getElementsByIdFilter(cm_options.clickable_id,cm_options.container_id);
	var contents = getElementsByIdFilter(cm_options.content_id,cm_options.container_id);
	
	if((clickables.length != contents.length)||(clickables.length == 0))
		return;
	
	for(var i = 0; i < clickables.length; i++)
	{
		clickables[i].onclick = showHideCollapse;
		clickables[i].name = i;
		contents[i].style.display = 'none';
		
		if(cm_options.class_close != null)
			clickables[i].className = cm_options.class_close;
	}
	
}

function showHideCollapse(e)
{
	var content_name = getEventNode(e).name
	
	if(String(content_name).replace(/ /gi, '') == '')
		return;
	else
		content_name = Number(content_name);
	
	var contents = getElementsByIdFilter(cm_options.content_id,cm_options.container_id);
	var clickables = getElementsByIdFilter(cm_options.clickable_id,cm_options.container_id);
		
	for(var i = 0; i < contents.length; i ++)
	{
		if((i == content_name)&&(contents[i].style.display == 'none'))
		{
			if(cm_options.class_open != null)
				clickables[i].className = cm_options.class_open;
			
			contents[i].style.display = '';
		}
		else
		{
			if(cm_options.class_close != null)
				clickables[i].className = cm_options.class_close;
			contents[i].style.display = 'none';
		}
	}
		
}
	
function childNodesByTag(container, tagName) 
{
	   var childDivs = container.childNodes;
	   var theChildern = new Array();
		   
	   for(var i = 0; i < childDivs.length; i ++)
	   {
		   if ( childDivs[i] && childDivs[i].tagName && childDivs[i].tagName == tagName)
			   theChildern.push(childDivs[i]);
	   }
		  
	   return theChildern;
		  
}
	
	
function getEventNode(e)
{
	var targ
	if (!e)
		var e = window.event
	
	if (e.target) 
		targ = e.target
	else if (e.srcElement)
		targ = e.srcElement
		
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode
	
	return targ;
}
	
function getElementsByIdFilter(id,filter)
{
	var children = document.getElementById(filter).getElementsByTagName("DIV");
		
	myChildren = new Array();
		
	for(var i = 0; i < children.length; i ++)
	{
		if(children[i] && children[i].id && children[i].id == id)
			myChildren.push(children[i]);
	}
		
	return myChildren;
}

