/*
DezinerFolio.com Simple Accordions.

Author  : G.S.Navin Raj Kumar
Website : http://dezinerfolio.com
Heavily modified by: PL

*/

// Prototype Method to get the element based on ID
function $get(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;
	}
}

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(number){
	d = acc_elements[number];
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		if(acc_options.opacity) {
			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(number){
	d = acc_elements[number];
	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');
		if(acc_options.opacity) {
			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){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.number+'")', acc_options.refresh);
	}
}

//Expand Initializer
function ex(d){
	if(dsp(d)=='none'){
		dsp(d,'block');
		d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.number+'")', acc_options.refresh);
	}
}

// Removes Classname from the given div.
function cc(n, v){
	s=n.className.split(' ');
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			//break;
		}
	}
	n.className=s.join(' ');
}

//Accordion Initializer (accordioncontainerid, speed, activeClass, togglerClass, elementClass)
var acc_headers = new Array();
var acc_elements = new Array();
var acc_options;
function Accordion(options) {
	acc_options = options;
	var s = options.speed;
	var refresh = options.refresh;
	var tc = options.activeClass;
	var tClass = options.togglerClass;
	var eClass = options.elementsClass;
	var show = options.show;

	// get all the elements
	if(tClass != undefined && tClass != '' && eClass != undefined && eClass != '') {
		var l=document.getElementsByTagName('*');
		for(i=0;i<l.length;i++) {
			h=l[i].className;
			if(h) {
				if(h.indexOf(tClass) > -1){
					acc_headers.push(l[i]);
				}
				if(h.indexOf(eClass) > -1){
					acc_elements.push(l[i]);
				}
			}
		}
		//then search through headers
		for(i=0;i<acc_elements.length;i++){
			var d=acc_elements[i];
			d.style.display='none';
			d.style.overflow='hidden';
			d.maxh =sh(d);
			d.s = (s == undefined ) ? 7 : s;
			h=acc_headers[i];
			h.tc=tc;
			h.number = i;
			d.number = i;
			// set the onclick function for each header.
			h.onclick = function(){
				for(i=0;i<acc_headers.length;i++){
					cn=acc_headers[i];
					eln=acc_elements[i];
					if(i == this.number) {
						ex(eln);
						n=cn;
						cc(n,'__');
						if(n.className.indexOf(n.tc) == -1) {
							n.className=n.className+' '+n.tc;
						}
					}
					else {
						cl(eln);
						cc(cn,'');
					}
				}
			}
		}
		if(show!=undefined) {
			if(acc_headers[show]) {
				acc_headers[show].onclick();
			}
		}
	}
	else {
		alert('Accordion toggler and elements classnames must not be empty!');
	}
}