// scrolly kill/show javascript
/* WDC:ML 23 Mar 2007
use: see http://www.csulb.edu/depts/enrollment/registration/misc_fees.html
@todo: detect scrolly regions by classname and auto-insert control bar at the top
*/

if(window.wdc){
	window.wdc.scrolly = new Object(); 
	window.wdc.scrolly.magicClass = 'scrolly'; 
	window.wdc.scrolly.height = '30em';
	window.wdc.scrolly.count = 0
	window.wdc.scrolly.expandText = 'expand';
	window.wdc.scrolly.collapseText = 'collapse';
	window.wdc.scrolly.controlBarClass = 'scrollyAdmin';
	window.wdc.scrolly.controlBarContainerClass = 'scrollyAdminContainer'; 
	addEvent(window,'load',addScrollyControls);
}

function addScrollyControls(){
	if(window.wdc.scrolly) {
		var regions = getElementsByClassName(document,'*',wdc.scrolly.magicClass);
		window.wdc.scrolly.count = regions.length
		for(var i=0;i<regions.length;i++){
			// add an ID if one doesn't already exist
			if(!regions[i].id){ regions[i].id = 'wdc_scrollyAutoID_'+i; }
			insertControlBar(regions[i],i)
		}
	}
}

function insertControlBar(node,index){
	// create the control bar
	var d = document.createElement('div');
	d.className = wdc.scrolly.controlBarClass;
	var a = document.createElement('a');
	a.setAttribute('href','#');
	a.id = 'scrollyAdminLink_'+index
	a.appendChild(document.createTextNode(wdc.scrolly.expandText)); 
	addEventJK(a,'click',toggleScrolly2,false,false); 
	addEventJK(a,'keypress',toggleScrolly2,false,false);
	// add it to the page
	d.appendChild(a)
	p = node.parentNode
	p.insertBefore(d,node);
}

function toggleScrolly2(){
	// where are we?
	var control = document.getElementById(this.id);
	if(control){ 
		// the clicked link's parent's next sibling; this is how the control bar was constructed
		var target = control.parentNode.nextSibling; 
	}
	if(target){
		// collapse if we're expanded and expand if we're collapsed
		if( hasClass(target,'expanded') ){
			removeClass(target,'expanded');
			control.innerHTML = wdc.scrolly.expandText;
		} else { 
			appendClass(target,'expanded'); 
			control.innerHTML = wdc.scrolly.collapseText;
		}
	// target a selected part of the list, if any
	if(window.location.hash.length>0){ document.location.hash = document.location.hash }
	}
	return false; 
}
