/*
DezinerFolio.com Simple accordions.

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 accordion
* Variable 'T' defines the refresh rate of the accordion
*/
s=5;
t=5;

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
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);
		d.style.opacity = 1;
		clearInterval(d.t);
	}
}

// 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'){
		dsp(d,'block');
		d.style.height='0px';
		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){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}
}


function accordion_pager(h2list) {
  for (i = 0; i < h2list.length; i++) {
    if (dsp(h2list[i].ele) == 'block') {
      // This one's the open one.  Close it, and open the next one.
      // If the next one is out of bounds, go back to the first one.
      if (h2list[i].clocktimer) {
        clearInterval(h2list[i].clocktimer);
        h2list[i].clocktimer = null;
      }
      cl(h2list[i].ele);
      cc(h2list[i++], 'accordion_highlight');
      break;
    }
  }

  // Back to the beginning
  if (i >= h2list.length) {
    i = 0;
  }
  ex(h2list[i].ele);

  // Add the clock graphic and set up timer
  h2list[i].className = h2list[i].className + ' accordion_highlight'; // TODO move
  h2list[i].style.backgroundPosition = "right 0px";
  h2list[i].clock_y = 0;
  moveclock = setInterval("accordion_moveclock(h2list[i])", h2list[i].auto_delay * 1000 / 8);
  h2list[i].clocktimer = moveclock;
}


function accordion_moveclock(h2) {
  clock_y = h2.clock_y - 22;
  h2.style.backgroundPosition = "right " + clock_y + 'px';
  h2.clock_y = clock_y;
}


function removeclocks(h2list) {
  for (i = 0; i < h2list.length; i++) {
    if (h2list[i].clocktimer) {
      clearInterval(h2list[i].clocktimer);
      h2list[i].clocktimer = null;
      cc(h2list[i], 'accordion_highlight');
    }
  }
}


//accordion Initializer
function accordion(d,s,tc,auto_delay){
  idnum = 1;
  ele_list = [];

  // Find all H2 elements in the accordion
  h2list = $(d).getElementsByTagName('h2');

  // For each H2...
  for (i = 0; i < h2list.length; i++) {
    h2 = h2list[i];
    h2.className = h2.className + ' accordion_headings'; // TODO move this to a var or const
    if (h2.id == '') {
      h2.id = 'accord_' + idnum++;
    }

    // Find the next element (div, ul, p...) after this H2
    ele = h2;
    do {
      ele = ele.nextSibling;
    } while (ele.nodeType != 1);

    // Associate that element with the H2
    if (ele.id == '') {
      ele.id = 'accord_' + idnum++;
    }
    h2.ele_id = ele.id;
    h2.ele = ele;
    h2.clocktimer = null;
    h2.auto_delay = auto_delay;
    ele_list.push(ele);
  }

  for (i = 0; i < h2list.length; i++) {
    h2 = h2list[i];
    ele = $(h2.ele_id);
    ele.style.display='none';
    ele.style.overflow='hidden';
    ele.maxh = sh(ele);
    ele.s=(s==undefined)? 7 : s;
    h2.tc = tc;
    h2.ele_list = ele_list;
    h2.h2list = h2list;
    h2.onclick = function() {
      clearInterval(autopager);
      removeclocks(this.h2list);
      // Look through the content blocks. Close all others, open ours.
      for (i = 0; i < this.ele_list.length; i++) {
        ele = this.ele_list[i];
        if (ele.id == this.ele_id) {
          ex(ele);
        } else {
          cl(ele);
        }
      }
    }
  }

  if (auto_delay > 0) {
    // Set up a timer to page through the accordion
    accordion_pager(h2list);
    autopager = setInterval("accordion_pager(h2list)", auto_delay * 1000);
  }
}



