/*
DezinerFolio.com Simple Accordians.

Author  : G.S.Navin Raj Kumar
Website : http://dezinerfolio.com

Modified: Thomas Espenhain

*/

/*
* 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=7;
t=10;

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d, um){			// um = until max h (true/false)
	d = $(d);
	//console.log('collaps id:', d.id, ' sh ', sh(d), ' maxh ', d.maxh);
	if (um!=undefined && um && sh(d)<=d.maxh) {
		//console.log('finish collaps for ', d.id, ' at ', d.maxh);
		sh(d, d.maxh+'px');
		clearInterval(d.t);
		return;
	}
	
	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);
	//console.log('expand id:', d.id, ' sh ', sh(d), ' maxh ', d.maxh);
	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, um) {		// um until maxh (true/false)
	if (um==undefined)
		um = false;
		
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'", '+um+')',t);
	}
}

//Expand Initializer
function ex(d){
	if(dsp(d)=='none'){
		dsp(d,'block');
		d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);
	}
	// expand parent content with submenu
	if (dsp(d) == 'block') {
		clearInterval(d.t);
		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;
		}
	}
}

// return true if class tc is set
function has_tc(n) {
	return n.className.search(n.tc)!=-1?true:false;
}


//Accordian Initializer
function Accordian(d,s,tc,p){
	// d = main dom object, s = speed, tc = class of header highlight (this class will be expanded)
	// get all the elements that have id as content
	//l=$(d).getElementsByTagName('div');
	var l=$(d).childNodes;
	var c=[];
	var h, i, d, tn;
	var ac_ph = p;	// accordian parent header
	for (i in l) {
		h=l[i].id;
		if (typeof(l[i].tagName) == "undefined") {
			continue;
		}
		tn = l[i].tagName;
		if(tn == "DIV" && h.substr(h.indexOf('-')+1,h.length)=='content') {
			c.push(h);
		}
	}
	var sel = null;
	//then search through headers
	for(i=0;i<l.length;i++){
		h = l[i].id;
		tn = l[i].tagName; 
		if(tn == "DIV" && 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.ac_children = {};
			h.ac_ph = ac_ph;
			//console.log('h.c', h.c);
			h.s_ex = function(mh) {		// s_ex = self expand
				var phid = this.id;
				var pc;
				pc = $(phid.substr(0,phid.indexOf('-'))+'-content');
				pc.maxh += mh;
	
				if (typeof(this.ac_ph) != "undefined") {
					//console.log('call expand on parent ', this.ac_ph.id);
					this.ac_ph.s_ex(mh);	
				}

				//console.log('expand ', pc.id, ' to ', pc.maxh);
				ex(pc);
			};
			
			h.s_cl = function(mh) {
				var phid = this.id;
				var pc;
				pc = $(phid.substr(0,phid.indexOf('-'))+'-content');
				pc.maxh -= mh;
	
				if (typeof(this.ac_ph) != "undefined") {
					//console.log('call expand on parent ', this.ac_ph.id);
					this.ac_ph.s_cl(mh);	
				}

				//console.log('collapse ', pc.id, ' to ', pc.maxh);
				cl(pc, true);
			};
			
			h.cl_children = function() {
				var cn, n, htc, ac;
				//console.log('call cl_children on ', this.id);
				for (var i=0; i<this.c.length; i++) {
					cn = this.c[i];
					n=cn.substr(0,cn.indexOf('-'));
					htc=has_tc($(n+'-header'));			// has tc flag (is cn active item)
					if (htc) {	// if true, have current open menu
						// search for new accordian as submenu and create a new accordian instance
						//child = $(cn).children[0];  // == 'accordion_child'
						var children = $(n+'-content').childNodes;
						var child;
						for (var j in children) {
							var c = children[j];
							if (typeof(c.tagName) != "undefined" && 
									c.tagName == 'DIV' && 
									c.className.search(/accordion_child/) != -1) {
										
								child = c;
								break;
							}
						}
						children = child.childNodes;  // children of accordion_child
						for (var j in children) {
							child = children[j];
							if (typeof(child.tagName) == "undefined")
								continue;
							var tn = child.tagName;	
							if (child.id.search(/basic-accordian/) != -1  && tn == "DIV") {
								var cs = child.childNodes; // headers of selected accordian
								for (var a in cs) {
									var ccc = cs[a];
									if (typeof(ccc.tagName) != "undefined" && 
											ccc.tagName == 'DIV' &&
											ccc.id.substr(ccc.id.indexOf('-')+1,ccc.id.length)=='header' &&
											has_tc(ccc)) {
										
										//console.log('found open menu in content', ccc.id);
										ccc.cl_children();
										var nn = ccc.id.substr(0,ccc.id.indexOf('-'));
										sh($(nn+'-content'),0);
										dsp($(nn+'-content'),'none');
										cc(ccc, '');
										//console.log('call s_cl on', ccc.id);
										ccc.ac_ph.s_cl($(nn+'-content').maxh);
									}
								}
							}
						}
					}
				}
			};
			
			h.onclick = function(node) {
				var cn, child, children, n, phid, pc, htc;
				this.cl_children();	// collapse all children before do anything
				for(var i=0;i<this.c.length;i++) {
					cn=this.c[i];
					n=cn.substr(0,cn.indexOf('-'));
					htc=has_tc($(n+'-header'));			// has tc flag (is cn active item)
					
					if((n+'-header')==this.id && !htc) {
						
						// modify heigh of parent content div
						if (typeof(this.ac_ph) != "undefined") {
							//console.log('call expand on', ac_ph.id);
							this.ac_ph.s_ex($(n+'-content').maxh);
//							phid = this.ac_ph.id;
//							pc = $(phid.substr(0,phid.indexOf('-'))+'-content');
//							pc.maxh += $(n+'-content').maxh
//							console.log('expand ', pc.id, ' to ', pc.maxh);
//							ex(pc);
						}						
						
						ex($(n+'-content'));
						n=$(n+'-header');
						cc(n,'__');
						n.className=n.className+' '+n.tc;
						
						// search for new accordian as submenu and create a new accordian instance
						//child = $(cn).children[0];  // == 'accordion_child'
						children = $(cn).childNodes;
						for (var j in children) {
							var c = children[j];
							if (typeof(c.tagName) != "undefined" && 
									c.tagName == 'DIV' && 
									c.className.search(/accordion_child/) != -1) {
										
								child = c;
								break;
							}
						}
						//children = child.children;  // children of accordion_child
						//for (var j=0;j<children.length;j++) {
						children = child.childNodes;  // children of accordion_child
						for (var j in children) {
							child = children[j];
							if (typeof(child.tagName) == "undefined")
								continue;
							var tn = child.tagName;	
							if (child.id.search(/basic-accordian/) != -1 
								&& typeof(this.ac_children[child.id]) == 'undefined' && tn == "DIV") {
								// create a new accordian instance and assign it to current h
								//console.log('create new submenu ', child.id);
								var nac = new Accordian(child.id, 5, 'header_highlight', this);
								this.ac_children[child.id] = new Accordian(child.id, 5, 'header_highlight', this);
							}
						}
						
					} else {
						// do normal collaps 
						cl($(n+'-content'));
						cc($(n+'-header'),'');
						
						// reduce parent conetent height if parent exists
						if (htc && typeof(this.ac_ph) != "undefined") {
							//console.log('call collaps on', ac_ph.id);
							this.ac_ph.s_cl($(n+'-content').maxh);
//							phid = this.ac_ph.id;
//							pc = $(phid.substr(0,phid.indexOf('-'))+'-content');
//							pc.maxh -= $(n+'-content').maxh
//							console.log('collaps ', pc.id, ' to ', pc.maxh);
//							cl(pc, true);
						}
					}					
				}
			}
			if(h.className.match(/selected+/)!=undefined) {
				sel=h;
			}
		}
	}
	if(sel!=undefined) {
		sel.onclick();
	}
}
