/*
  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 = {
  init:function(){
    // shortcut for CSS properties
    var css = YAHOO.otorg.DomCollapse.css;
    if(typeof(css) !== 'undefined'){
      // get possible bookmark
      var bookmark = window.location.hash.replace('#','');
      // get all elements with the correct class
      var elms = YAHOO.util.Dom.getElementsByClassName(css.triggerClass);
      // loop over all the elements
      for(var i=0,j=elms.length;i<j;i++){
      	if(YAHOO.util.Dom.hasClass(elms[i],css.parentClass)) { //dlnsk
      		return;
      	}
        // if the trigger is not a link
        if(elms[i].nodeName.toLowerCase()!=='a'){
          // get the next element
          var t = YAHOO.otorg.DomCollapse.getNext(elms[i]);
          if(t){
            // get the element's ID or create a new one
            var newID = t.id || YAHOO.util.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 = elms[i].innerHTML;
            a.innerHTML = elms[i].innerHTML;
            elms[i].innerHTML = '';
            elms[i].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){
              YAHOO.util.Dom.addClass(elms[i],css.parentClass);
              YAHOO.util.Dom.addClass(t,css.hideClass);
            // otherwise remove the hide class and add the 
            // open class to the trigger
            } else {
              YAHOO.util.Dom.addClass(elms[i],css.openClass);
              YAHOO.util.Dom.removeClass(t,css.hideClass);
            };
            // add a click handler to the link pointing to toggle()
            YAHOO.util.Event.on(a, 'click', YAHOO.otorg.DomCollapse.toggle);
          };
        // if the trigger is a link
        } else {
          // 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) || YAHOO.otorg.DomCollapse.getNext(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){
              YAHOO.util.Dom.addClass(elms[i],css.parentClass);
              YAHOO.util.Dom.addClass(t,css.hideClass);
            // otherwise add the open class to the trigger
            } else {
              YAHOO.util.Dom.addClass(elms[i],css.openClass);
            };
            // add a click handler to the link pointing to toggle
            YAHOO.util.Event.on(elms[i], 'click', YAHOO.otorg.DomCollapse.toggle);
          };
        };
      };
      
    };
  },
  // tool method to get the next sibling that is not a text node
  getNext:function(o){
    var t = o.nextSibling;
    if(t){
      while(t.nodeType !== 1 && t.nextSibling){
        t = t.nextSibling;
      }
    }
    return t;  
  },
  // method to toggle the showing and hiding of the next element
  toggle:function(e){
    // 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 = YAHOO.util.Dom.hasClass(this,css.triggerClass) ? this : this.parentNode;
    // grab the ID the link points to from the href attribute and get the element
    var id = this.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(YAHOO.util.Dom.hasClass(t,css.hideClass)){
        YAHOO.util.Dom.setStyle(t,'opacity',0);
        YAHOO.util.Dom.removeClass(t,css.hideClass);
        // set the appropriate class to the trigger element
        YAHOO.util.Dom.replaceClass(parent,css.parentClass,css.openClass);
        // prepare the animation animate the element
        var a = new YAHOO.util.Anim(t, { opacity: {from:0,to: 1} },.8,YAHOO.util.Easing.easeBoth);
        // animate the element
        a.animate();
      } else {
        YAHOO.util.Dom.replaceClass(parent,css.openClass,css.parentClass);
        var a = new YAHOO.util.Anim(t, { opacity: {from:1,to: 0} },.8,YAHOO.util.Easing.easeBoth);
        // when the animation is done, hide it with the right class, 
        // re-set its styles and replace the open with a parent class
        a.onComplete.subscribe(
          function(){        
            YAHOO.util.Dom.addClass(t,css.hideClass);
            YAHOO.util.Dom.setStyle(t,'opacity',1);
          }
        );
        a.animate();
        // don't follow the link when you hide the element
        YAHOO.util.Event.preventDefault(e);
      };
    };
  }    
};
// If the DOM is ready, go for it.
//YAHOO.util.Event.onDOMReady(YAHOO.otorg.DomCollapse.init); //dlnsk
YAHOO.util.Event.addListener(window, "load", YAHOO.otorg.DomCollapse.init); //for compatibility with yui v0.12.4
