/*
DezinerFolio.com Simple Accordians.

Author  : G.S.Navin Raj Kumar
Website : http://dezinerfolio.com

*/

/*
* The Variable names have been compressed to achive a higher level of compression.
*/

// 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;
	}
}
/*
* Variable 'S' defines the speed of the accordian
* Variable 'T' defines the refresh rate of the accordian
*/
s=10;
t=1;
e = 0;
clicked = false;
current = '';

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d){
	d = $(d);
	if (current != d.id)
	{
	
	if(sh(d)>1){
		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)+');';
		//alert('ct '+d.style.filter);
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
	//alert("ct"+d.id);
	}
}

//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
	d = $(d);
	//alert('e='+e + ", d.height=" + d.style.height);
	//d.style.filter= 'alpha(opacity='+(e*100/d.maxh)+');';
	//d.style.height = e+'px';
	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)+');';
		//alert('et '+d.style.filter);
	}else{
		sh(d,d.maxh);
		clearInterval(d.t);
	}
	/*if(e<d.maxh){
		v = Math.round((d.maxh-e)/d.s);
		v = (v<1) ? 1 :v ;
		e = (e+v);
		sh(d,e+'px');
		d.style.opacity = (e/d.maxh);
		d.style.filter= 'alpha(opacity='+(e*100/d.maxh)+');';
		//alert('et '+d.style.filter);
	}else{
		sh(d,d.maxh);
		clearInterval(d.t);
		current = d.id;
	}*/
	
	
}

// Collapse Initializer
function cl(d){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'")',t);
	}
}

//Expand Initializer
function ex(d){
	if(dsp(d)=='none'){
		clearInterval(d.t);
		d.style.height='0px';
		e = 0;
		dsp(d,'block');
		d.t=setInterval('et("'+d.id+'")',t);
		
	}
}

// Removes Classname from the given div.
function cc(n,v){
	/*s=n.className.split(/\s+/);
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}*/
}
//Accordian Initializer
function Accordian(d,s,tc){
	// get all the elements that have id as content
	l=$(d).getElementsByTagName('div');
	c=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){c.push(h);}
	}
	sel=null;
	//then search through headers
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			d=$(h.substr(0,h.indexOf('-'))+'-content');
			d.style.display='none';
			d.style.overflow='hidden';
			d.maxh =sh(d);
			d.s=(s==undefined)? 7 : s;
			h=$(h);
			h.tc=tc;
			h.c=c;
			// set the onclick function for each header.
			h.onclick = function(){	
				if (clicked == false)
				{
					clicked == true;
					for(i=0;i<this.c.length;i++){
						cn=this.c[i];
						n=cn.substr(0,cn.indexOf('-'));

						if((n+'-header')==this.id){
							div = $(cn);

							if (dsp(div) == 'none') //added to ensure at least one menu expanded 
							{
								sh(div,'0px');
								ex($(n+'-content'));
								current = n+'-content';
							}

							if (n.substr(0,3) == 'mcc' || n.substr(0,3) == 'mff' ||  n.substr(0,3) == 'max' || n.substr(0,3) == 'mcb' )
							{
								lmenuclick2(document.getElementById(n+'-header-init').value, document.getElementById(n+'-header-init-id').value);
							}else{
								lmenuclick(document.getElementById(n+'-header-init').value);
							}
							if (dsp(div) == 'none') //added to ensure at least one menu expanded 
							{
								n=$(n+'-header');
								cc(n,'__');
							}

						}else{
								cl($(n+'-content'));
								cc($(n+'-header'),'');
						}


					}
					clicked == false;
				}
				
			}
			if(h.className.match(/selected+/)!=undefined){ sel=h;}
		}
	}
	if(sel!=undefined){sel.onclick();}
}
