/*********** ACCORDIAN ***************/
function eventTarget(e) {
  var el = e.target ? e.target : e.srcElement;  // e.target for W3C, e.srcElement for IE
  if (el.nodeType == 3) el = el.parentNode;     // Safari bug
  return el;
}
var accordian = {
  DELTA: .1,  
  DELAY: 25,
  target: null,
  init: function(id, tog){
    var dl = document.getElementById(id);
    var dt = dl.getElementsByTagName("dt");
    var dd = dl.getElementsByTagName("dd");
    dl.className = "accordian";
    for (i = 0; i < dt.length; i++){
      dt[i].onclick = this.toggle;
      dt[i].className = "accordianClosed";
      dd[i].style.height = "0px";      
    }
    this.sa = new Array();
    this.sa[0] = dl.insertBefore(this.createShowAll(), dt[0]);
    this.sa[1] = dl.appendChild(this.createShowAll());
    this.dt = dt;
    this.dd = dd;
    if (typeof tog != "undefined") this.toggle(dt[tog]);
  },
  createShowAll: function(){
    var a = document.createElement("a");
    a.innerHTML = "Show All";
    a.className = "accordianShow";
    a.href = "";
    a.onclick = this.toggleAll;
    return a;
  },
  toggle: function(e){
    // this = element clicked
    e = e ? e : window.event;
    if (e.nodeType) var dt = e;
    else var dt = eventTarget(e);
    if (e && dt) {
      var dd = dt.nextSibling;
      while (dd.nodeType != 1){
        dd = dd.nextSibling;
      }
      if (dt.className != "accordianOpen"){
        accordian.anim(dd, dd.scrollHeight, accordian.DELTA);
        dt.className = "accordianOpen";
        var sw = true;
        for (i = 0; i < accordian.dt.length; i++){
          if (accordian.dt[i].className != "accordianOpen") sw = false;
        }
        if (sw) accordian.toggleShowAll("Hide");
      } else {
        accordian.target = dt;
        accordian.anim(dd, dd.scrollHeight, -accordian.DELTA, accordian.toggleClose);
      }
    }
  },
  toggleClose: function(){
    accordian.target.className = "accordianClosed";
    accordian.toggleShowAll("Show");
  },
  toggleAll: function(e){
    e = e ? e : window.event;
    var a = eventTarget(e);
    if (a.className == "accordianShow"){
      accordian.toggleShowAll("Hide");
      for (i = 0; i < accordian.dt.length; i++){
        if (accordian.dt[i].className != "accordianOpen"){
          accordian.dt[i].className = "accordianOpen";
          accordian.anim(accordian.dd[i], accordian.dd[i].scrollHeight, .1);
        }
      }
    } else {
      accordian.toggleShowAll("Show");
      for (i = 0; i < accordian.dt.length; i++){
        accordian.dt[i].className = "accordianClosed";
        accordian.anim(accordian.dd[i], accordian.dd[i].scrollHeight, -.1);
      }
    }
    return false;
  },
  toggleShowAll: function(showhide){
      for (i = 0; i < accordian.sa.length; i++){
        this.sa[i].className = "accordian" + showhide;
        this.sa[i].innerHTML = showhide + " All";
      }
  },
  anim: function(el, scroll, delta, callback){
    if ((parseInt(el.style.height) < scroll && delta > 0) || (parseInt(el.style.height) > 0 && delta < 0)){
      var height = parseInt(el.style.height) + scroll * delta;
      if (height > scroll) height = scroll;
      if (height < 0)  height = 0;
      el.style.height = height + "px"; 
      if (height < scroll && height > 0) setTimeout(function(){ accordian.anim(el, scroll, delta, callback); }, this.DELAY);
      else if(typeof callback == "function") callback();
    }
  }
};
window.onload = function(){
  accordian.init('accordian', 0);
}