//by Valerio Proietti (http://mad4milk.net). MIT-style license.
//exfolders.js - depends on prototype.js or prototype.lite.js + moo.fx.js
//version 2.0
//edited

function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      if (oldonload) { 
        oldonload(); 
        func();
      }
    } 
  } 
} 

function UpFolder(folder_num, folders_cnt, idparam)
{
  for(i=0; i < folders_cnt; i++)
  {
    if (i != folder_num)
    {
      document.getElementById("folder_open_" + idparam + "_" + i).style.display = 'none';
      document.getElementById("folder_close_" + idparam + "_" + i).style.display = 'inline';
    }
  }

  if (document.getElementById("folder_open_" + idparam + "_"  + folder_num).style.display == 'inline')
  {
    document.getElementById("folder_open_" + idparam + "_"  + folder_num).style.display = 'none';
    document.getElementById("folder_close_" + idparam + "_" + folder_num).style.display = 'inline';
//    accordion.hideThis(folder_num);
//    document.getElementById("body_text_" + idparam + "_"  + folder_num).style.display = 'none';
  }
  else
  {
    document.getElementById("folder_open_" + idparam + "_"  + folder_num).style.display = 'inline';
    document.getElementById("folder_close_" + idparam + "_" + folder_num).style.display = 'none';
  }

}


//Example: expandable_folders('id_container', 'id_suffix','Header 1','img_url 1','body text 1','Header 2','img_url 2','body text 2','Header 3','img_url 3','body text 3');
function expandable_folders()
{
  var arguments_on_folder = 3;
  var folders_cnt = Math.floor((expandable_folders.arguments.length - 2) / arguments_on_folder); 
  if (folders_cnt == 0)
    return;
  idparam = expandable_folders.arguments[1];

  var template_folder = 
    "<div class=\"ex_folders\">" + 
    "<div><h3 class=\"toggler\" onclick=\"javascript:UpFolder(%folder_num%, %folders_cnt%, "+idparam+");\" id=\"toggler_"+idparam+"_%folder_num%\">" + 
    "<div>" +
    "<span id=\"folder_open_"+idparam+"_%folder_num%\" style=\"display:none\"><img src=\"i/exfolders_minus.gif\" style=\"padding-top: 15px\" width=\"9\" height=\"10\" />&nbsp;&nbsp;</span>" +
    "<span id=\"folder_close_"+idparam+"_%folder_num%\" style=\"display:none\"><img src=\"i/exfolders_plus.gif\" style=\"padding-top: 15px\" width=\"9\" height=\"10\" />&nbsp;&nbsp;</span>" +
    "<a href=\"#\" >%header_text%<!--<img src=\"%image_url%\" width=\"1\" height=\"1\" style=\"display:none\" align=\"right\" border=\"0\" />--></a>" +
    "</div>" +
    "</h3></div>" +
    "<div class=\"body_text\" id=\"body_text_"+idparam+"_%folder_num%\">" +
    "<div class=\"body_text_padding\">" +
    "%body_text%" +
    "</div>" +
    "</div>" +
    "</div>";

  s = '';  
  for (var i = 0; i < folders_cnt; i++)
  {
    folder_html = template_folder.replace(/%folder_num%/g, i);
    folder_html = folder_html.replace(/%folders_cnt%/g, folders_cnt);
    folder_html = folder_html.replace(/%header_text%/, expandable_folders.arguments[2 + i * arguments_on_folder]);
    folder_html = folder_html.replace(/%image_url%/, expandable_folders.arguments[3 + i * arguments_on_folder]);
    folder_html = folder_html.replace(/%body_text%/, expandable_folders.arguments[4 + i * arguments_on_folder]);
    s += folder_html;
  }
  document.getElementById(expandable_folders.arguments[0]).innerHTML = s;


  var h3s = [];
  for(i=0; i < folders_cnt; i++)
    h3s[i] = document.getElementById("toggler_" + idparam + "_" + i);
    
  var divs = [];
  for(i=0; i < folders_cnt; i++)
    divs[i] = document.getElementById("body_text_" + idparam + "_" + i);

  var accordion = new Fx.Accordion(h3s, divs, {opacity: true, height: true, duration: 400});
  UpFolder('0', folders_cnt, idparam);
}



Fx.Accordion = Class.create();
Fx.Accordion.prototype = Object.extend(new Fx.Base(), {
  
  extendOptions: function(options){
    Object.extend(this.options, Object.extend({
      start: 'open-first',
      fixedHeight: false,
      fixedWidth: false,
      alwaysHide: false,
      wait: false,
      onActive: function(){},
      onBackground: function(){},
      height: true,
      opacity: true,
      width: false
    }, options || {}));
  },

  initialize: function(togglers, elements, options){
    this.now = {};
    this.elements = $A(elements);
    this.togglers = $A(togglers);
    this.setOptions(options);
    this.extendOptions(options);
    this.previousClick = 'nan';
    this.togglers.each(function(tog, i){
      if (tog.onclick) tog.prevClick = tog.onclick;
      else tog.prevClick = function(){};
      $(tog).onclick = function(){
        tog.prevClick();
        this.showThisHideOpen(i);
      }.bind(this);
    }.bind(this));
    this.h = {}; this.w = {}; this.o = {};
    this.elements.each(function(el, i){
      this.now[i+1] = {};
      el.style.height = '0';
      el.style.overflow = 'hidden';
    }.bind(this));
    switch(this.options.start){
      case 'first-open': this.elements[0].style.height = this.elements[0].scrollHeight+'px'; break;
      case 'open-first': this.showThisHideOpen(0); break;
    }
  },
  
  setNow: function(){
    for (var i in this.from){
      var iFrom = this.from[i];
      var iTo = this.to[i];
      var iNow = this.now[i] = {};
      for (var p in iFrom) iNow[p] = this.compute(iFrom[p], iTo[p]);
    }
  },

  custom: function(objObjs){
    if (this.timer && this.options.wait) return;
    var from = {};
    var to = {};
    for (var i in objObjs){
      var iProps = objObjs[i];
      var iFrom = from[i] = {};
      var iTo = to[i] = {};
      for (var prop in iProps){
        iFrom[prop] = iProps[prop][0];
        iTo[prop] = iProps[prop][1];
      }
    }
    return this._start(from, to);
  },

  hideThis: function(i){
    if (this.options.height) this.h = {'height': [this.elements[i].offsetHeight, 0]};
    if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, 0]};
    if (this.options.opacity) this.o = {'opacity': [this.now[i+1]['opacity'] || 1, 0]};
  },

  showThis: function(i){
    if (this.options.height) this.h = {'height': [this.elements[i].offsetHeight, this.options.fixedHeight || this.elements[i].scrollHeight]};
    if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, this.options.fixedWidth || this.elements[i].scrollWidth]};
    if (this.options.opacity) this.o = {'opacity': [this.now[i+1]['opacity'] || 0, 1]};
  },

  showThisHideOpen: function(iToShow){
    if (iToShow != this.previousClick || this.options.alwaysHide){
      this.previousClick = iToShow;
      var objObjs = {};
      var err = false;
      var madeInactive = false;
      this.elements.each(function(el, i){
        this.now[i] = this.now[i] || {};
        if (i != iToShow){
          this.hideThis(i);
        } else if (this.options.alwaysHide){
          if (el.offsetHeight == el.scrollHeight){
            this.hideThis(i);
            madeInactive = true;
          } else if (el.offsetHeight == 0){
            this.showThis(i);
          } else {
            err = true;
          }
        } else if (this.options.wait && this.timer){
          this.previousClick = 'nan';
          err = true;
        } else {
          this.showThis(i);
        }
        objObjs[i+1] = Object.extend(this.h, Object.extend(this.o, this.w));
      }.bind(this));
      if (err) return;
      if (!madeInactive) this.options.onActive.call(this, this.togglers[iToShow], iToShow);
      this.togglers.each(function(tog, i){
        if (i != iToShow || madeInactive) this.options.onBackground.call(this, tog, i);
      }.bind(this));
      return this.custom(objObjs);
    }
    else
    {
      this.previousClick = iToShow;
      var objObjs = {};
      var err = false;
      var madeInactive = false;
      this.elements.each(function(el, i){
        this.now[i] = this.now[i] || {};
        if (i != iToShow){
          this.hideThis(i);
        } else if (this.options.alwaysHide){
          if (el.offsetHeight == el.scrollHeight){
            this.hideThis(i);
            madeInactive = true;
          } else if (el.offsetHeight == 0){
            this.hideThis(i);
          } else {
            err = true;
          }
        } else if (this.options.wait && this.timer){
          this.previousClick = 'nan';
          err = true;
        } else {
          this.hideThis(i);
        }
        objObjs[i+1] = Object.extend(this.h, Object.extend(this.o, this.w));
      }.bind(this));
      if (err) return;
      if (!madeInactive) this.options.onActive.call(this, this.togglers[iToShow], iToShow);
      this.togglers.each(function(tog, i){
        if (i != iToShow || madeInactive) this.options.onBackground.call(this, tog, i);
      }.bind(this));

      this.previousClick = -1;
      return this.custom(objObjs);
    }
  },
  
  increase: function(){
    for (var i in this.now){
      var iNow = this.now[i];
      for (var p in iNow) this.setStyle(this.elements[parseInt(i)-1], p, iNow[p]);
    }
  }

});


