/*
  YUIDomCollapse by Christian Heilmann
  Version 1.0 / May 2007
  License: http://creativecommons.org/licenses/by/3.0/
  Homepage: http://onlinetools.org/tools/yuidomcollapse/
*/
// YUI Namespace
YAHOO.namespace('otorg');
YAHOO.otorg.DomCollapse = function() {
  // shortcut for CSS properties
  var css = YAHOO.otorg.DomCollapse.css;
  if(typeof(css) == 'undefined'){
    return;
  }
  var bookmark = this.bookmark();
  // get all elements with the correct class
  var elms = this.dom.getElementsByClassName(css.triggerClass);

  for(var i=0,j=elms.length;i<j;i++){
    if(this.isLink(elms[i])) {
      this.initLink(elms[i], bookmark, css);
    } else {
      this.initNonLink(elms[i], bookmark, css);
    }
  }
};

YAHOO.otorg.DomCollapse.prototype = {
  dom: YAHOO.util.Dom,
  isLink: function(elem) {
    return elem.nodeName.toLowerCase() == "a";
  },
  bookmark: function() {
    return window.location.hash.replace('#','');
  },
  initNonLink: function(el, bookmark, css) {
    // get the next element
    var t = this.dom.getNextSibling(el);
    if(t){
      // get the element's ID or create a new one
      var newID = t.id || this.dom.generateId();
      t.setAttribute('id',newID);
      // create a new target and replace the element's
      // content with this new element
      var a = document.createElement('a');
      a.setAttribute('href','#'+newID);
      var c = el.innerHTML;
      a.innerHTML = el.innerHTML;
      el.innerHTML = '';
      el.appendChild(a);
      // if the ID is not the bookmark add the parent class 
      // to the trigger element and hide the element by 
      // adding the hide class
      if(newID !== bookmark){
        this.dom.addClass(el,css.parentClass);
        this.dom.addClass(t,css.hideClass);
      // otherwise remove the hide class and add the 
      // open class to the trigger
      } else {
        this.dom.addClass(el,css.openClass);
        this.dom.removeClass(t,css.hideClass);
      }
      // add a click handler to the link pointing to toggle()
      YAHOO.util.Event.on(a, 'click', this.toggle, false, this);
    }
  },
  initLink: function(el, bookmark, css) {
    // get the ID from the href attribute
    var newID = elms[i].href.replace(/.*#/,'');
    // grab the connected element, or the next sibling in case
    // it doesn't exist
    var t = document.getElementById(newID) || this.dom.getNextSibling(elms[i]);
    if(t !== null){
      // re-set the href attribute to this element
      if(t.id !== newID){
        newID = t.id;
        elms[i].setAttribute('href','#'+newID);
      };
      // if the ID is not the bookmark, hide the element
      // and add the parent class
      if(newID !== bookmark){
        this.dom.addClass(elms[i],css.parentClass);
        this.dom.addClass(t,css.hideClass);
      // otherwise add the open class to the trigger
      } else {
        this.dom.addClass(elms[i],css.openClass);
      };
      // add a click handler to the link pointing to toggle
      YAHOO.util.Event.on(elms[i], 'click', this.toggle, false, this);
    }
  },
  toggle: function(evt){
    var el = YAHOO.util.Event.getTarget(evt);
    // shortcut for CSS object
    var css = YAHOO.otorg.DomCollapse.css;
    // if the element has the trigger class it is a link, otherwise 
    // it is a generated link by init()
    var parent = this.dom.hasClass(el,css.triggerClass) ? el : el.parentNode;
    // grab the ID the link points to from the href attribute and get the element
    var id = el.href.replace(/.*#/,'');
    var t = document.getElementById(id);
    if(t !== undefined){
      // if the element is hidden (has the hide class) remove the hide 
      // class and swap parent for open and vice versa
      if(this.dom.hasClass(t,css.hideClass)){
        this.dom.removeClass(t,css.hideClass);
        this.dom.replaceClass(parent,css.parentClass,css.openClass);
      } else {
        this.dom.addClass(t,css.hideClass);
        this.dom.replaceClass(parent,css.openClass,css.parentClass);
      }
      // don't follow the link ever
      YAHOO.util.Event.preventDefault(evt);
    }
  }
};
// If the DOM is ready, go for it.
YAHOO.util.Event.onDOMReady(function() {
  new YAHOO.otorg.DomCollapse();
});
