/*
MODIFIED from the ORIGINAL SCRIPT at DezinerFolio.com Simple Accordians (Author: G.S.Navin Raj Kumar)
*/

// Prototype Method to get the element based on ID
function $(d){
	return document.getElementById(d);
}

// set or get the current display style of the div
function dsp(d,v){
	if(v==undefined){
		return d.style.display;
	}else{
		d.style.display=v;
	}
}

// set or get the height of a div.
function sh(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight; 
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
		return r;
	}else{
		d.style.height=v;//alert('called by: ' + d.id );
	}
}

/*
* Variable 'S' defines the speed of the accordian
* Variable 'T' defines the refresh rate of the accordian
*/
s=7;
t=1;

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially. (1/s th of the height)
function ct(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
}

//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
	d = $(d);
	if(sh(d)<d.maxh){ 
		v = Math.round((d.maxh-sh(d))/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,d.maxh);
		clearInterval(d.t);
	}
}

// Collapse Initializer
function cl(d){
	setHeaderBG(d.id, 0);
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'")',t);
	}
}

//Expand Initializer
function ex(d){
	setHeaderBG(d.id, 1);
	if(dsp(d)=='none'){
		dsp(d,'block');
		d.style.height='0'; // set height = 0 and increase it to maxh
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);
	}
}

function setHeaderBG(id, action) {
	h = $(id.replace(/content/, "header")); 
	//h.onmouseover = function(){h.style.backgroundColor = (action==1)? '#00ccff':;}
	h.style.backgroundColor = (action==1)?"#00ccff":"#779966";
	/*h.className = (action==1)?'accordTitle selected':'accordTitle accordTitleBG';
	myClass = h.className;
	if((/selected/g).test(myClass)) {
		myClass.replace(/selected/g, ""); alert("Y");
	} else {
		(myClass + ' selected'); alert("N");
	}
	h.className = myClass; //alert(myClass);*/
}

//Accordian Initializer
//d: container for accordian, s: accordian speed, e: the id of the element to expand initially
function Accordian(d,e){
	// get all the elements that have "content" as a part of their id
	l=$(d).getElementsByTagName('div'); 
	c=[]; ttl=[];
	for(i=0;i<l.length;i++){
		h=l[i].id; 
		switch(h.substr(h.indexOf('-')+1,h.length)) {
			case "content":
				c.push(h); break; // c[i] contains each content div id
			case "header":
				ttl.push(h); break; // ttl[i] contains each header div id
			default: break;
		}
	}
	
	//sel=null; // use it if sel method is selected
	for(i=0;i<ttl.length;i++){
		ttlID=ttl[i];
		h=$(ttlID); // h = current title div object		
		h.c=c;  // c = array of content div ids		
		d=$(ttlID.substr(0,ttlID.indexOf('-'))+'-content'); // d = current title's content obj which is a div

		d.maxh=sh(d); //alert(d.maxh);
		d.s=(s==undefined)? 7 : s;		
		d.style.overflow='hidden';
		
		//KEEP--> d.style.display=(h.id == e)?'block':'none';  //alert(d.id + '\'s style is: ' + d.style.display); 
		d.style.display='none'; //PLUS the line below about h.id==e then ex(d). 
		if (h.id == e) ex(d); // expand the given id element 
		
		// set the onclick function for each header.
		h.onclick = function(){
			for(i=0;i<this.c.length;i++){
				cn=this.c[i]; //alert(this.className);
				n=cn.substr(0,cn.indexOf('-')); // content id is in the form "test-content", so n = 'test' here
				if((n+'-header')==this.id){ // if there's a match between part cn supplied header to the current one, toggle expand collapse.
					dsp($(n+'-content'))=='none' ? ex($(n+'-content')):cl($(n+'-content'));
				} else {
					cl($(n+'-content'));
				}
			}
		}
	}
	//if(e!=undefined){$(e).onclick();}
}
myFunc = function(){Accordian('basic-accordion', 'officeHrs-header');}
// addOnLoad(nameOfSomeFunctionToRunOnPageLoad); 
// EXAMPLE1: addOnLoad(myFunc); OR  addOnLoad(accordFunc);
// EXAMPLE2: addOnLoad(function() {document.body.style.backgroundColor = '#EFDF95';});

//addOnLoad(myFunc);
