/*
A set of function which add the ability to collapse fieldsets down to just he legend.

It automatically registers event handlers on any fieldset which contains a 'collapseable' or 'collapsed'
class and automatically hides the collapsed fieldset.

It also swaps the class names on change so that the stylesheet can format the the fieldset appropriately.
*/

	//Handler which collapses field sets when you click on the legend
	function collapseHandler(e){
		if (!e) var e = window.event;
		// e gives access to the event in all browsers

		//IE uses srcElement instead of target - copy to the same name
		if(!e.target) e.target = e.srcElement;
		
		//We only do something if the legend has been clicked
		if(e.target.nodeName != 'LEGEND') return;

		//Loop up the tree to find the surrounding fieldset
		var obj = e.target;
		while (obj.nodeName != 'FIELDSET')
		{
			obj = obj.parentNode;
		}
		switch(getCollapseStatus(obj)){
			case "collapseable":
				collapseFieldSet(obj);
			break;
			case "collapsed":
				unCollapseFieldSet(obj);
			break;
			default:
			 // Do nothing
			break;
		}
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
	
	function collapseFieldSet(obj){
		for(var i=0; i < obj.childNodes.length; i++){
			//alert(obj.childNodes[i].nodeName+':'+obj.childNodes[i].nodeType);
			if(obj.childNodes[i].nodeType == 1 && obj.childNodes[i].nodeName != 'LEGEND'){
				obj.childNodes[i].style.display = 'none';
			}
		}
		//Set the class to be collapsed
		addClass(obj,'collapsed');
		removeClass(obj,'collapseable');
	}
	
	function unCollapseFieldSet(obj){
		for(var i=0; i < obj.childNodes.length; i++){
			if(obj.childNodes[i].nodeType == 1 && obj.childNodes[i].nodeName != 'LEGEND'){
				obj.childNodes[i].style.display = 'block';
			}
		}
		//Set the class to be collapseable
		addClass(obj,'collapseable');
		removeClass(obj,'collapsed');
	}


	//Get the status - one of collapsed, collapseable or '' (empty string)
	function getCollapseStatus(obj){
		var classArray = obj.className.split(' ');
		var j=0;
		//search for the class
		for(j=0;j <= classArray.length-1; j++){
			if(classArray[j] == 'collapsed') return 'collapsed';
		}
		for(j=0;j <= classArray.length-1; j++){
			if(classArray[j] == 'collapseable') return 'collapseable';
		}
		return '';
	}
	
	//Attach the collapse handler to the field which have the collapse class
	function initCollapseFieldSet(){
		var aFieldsets = document.getElementsByTagName("fieldset");
		for(var i=0; i < aFieldsets.length; i++){
			switch(getCollapseStatus(aFieldsets[i])){
				case "collapseable":
					aFieldsets[i].onclick =  collapseHandler;
				break;
				case "collapsed":
					aFieldsets[i].onclick =  collapseHandler;
					collapseFieldSet(aFieldsets[i]);
				break;
				default:
				 // Do nothing
				break;
			}
		}
	}
	//TODO: Make this use onload handler - in a nice way - without clobbering other onloads.
	initCollapseFieldSet();

