/* ---------------------------------------------
Nested Accordion v.1.1
Script to create 'accordion' functionality on a hierarchically structured content.
http://adipalaz.awardspace.com/experiments/jquery/nested_accordion.html
Requires: jQuery v1.3+
Copyright (c) 2009 Adriana Palazova
Dual licensed under the MIT (http://adipalaz.awardspace.com/docs/mit-license.txt) and GPL (http://adipalaz.awardspace.com/docs/gpl-license.txt) licenses.
------------------------------------------------ */

(function($) {
//$.fn.orphans - http://www.mail-archive.com/jquery-en@googlegroups.com/msg43851.html
$.fn.orphans = function(){
var txt = [];
this.each(function(){$.each(this.childNodes, function() {
  if (this.nodeType == 3 && $.trim(this.nodeValue)) txt.push(this)})}); return $(txt);};
  
$.fn.accordion = function(options) {
    var defaults = {
        obj : 'ol', //the element that contains the accordion 
        objClass : '.accordion', //the class name of the accordion
        objID : '', //the ID of the accordion (optional)
        wrapper :'li', //the common parent of 'a.trigger' and 'o.next' 
        el : 'li', //the parent of 'a.trigger'
        head : '', //the headings that are parents of 'a.trigger' (if any)
        next : 'ol', //the collapsible element
        initShow : '', //the initially expanded section (optional)
        showMethod : 'slideDown', //'slideDown', 'show', 'fadeIn', or custom
        hideMethod : 'slideUp', //'slideUp', 'hide', 'fadeOut', or custom
        showSpeed: 400,
        hideSpeed: 600
    };
    var o = $.extend({}, defaults, options);   

    return this.each(function() {
        var containerID = '#' + this.id,
          Obj = containerID + ' ' + o.obj + o.objID + o.objClass,
          El = Obj + ' ' + o.el;
        
        $(Obj).find(o.head).addClass('h');
                         
        $(El).orphans().wrap('<a class="trigger" style="display:block" href="#" title="open/close" />');
          
        if ($(o.el + '+ div:not(".outer")')) $(o.el + '+ div:not(".outer")').wrap('<div class="outer" />');
        
        $(Obj + ' .h').each(function(){
          var $this = $(this);
          if ((o.wrapper == 'div') && ($this + '+ div.outer')) $this.add( $this.next('div.outer') ).wrapAll('<div class="new"></div>');
        }); 
        
        $(Obj).find(o.wrapper).find('> ' + o.next).hide();
        
        $(Obj).find(o.initShow).show()
          .parent(o.wrapper).find('> a.trigger, > ' + o.el + ' a.trigger').addClass('open');
        
        $(El).find('a.trigger').click(function() {
            var $nextEl = $(this).closest(o.wrapper).find('> ' + o.next),
                $siblings = $(this).closest(o.wrapper).siblings(o.wrapper);
                
            if(($nextEl) && ($nextEl.is(':visible'))) {
              $(this).removeClass('open');
              $nextEl.filter(':visible')[o.hideMethod](o.hideSpeed);
              return false;
            };
            
            if(($nextEl) && ($nextEl.is(':hidden'))) {
              $siblings.find('> a.open, >'+ o.el + ' a.open').removeClass('open').end()
                .find('> ' + o.next + ':visible')[o.hideMethod](o.hideSpeed);
              $(this).addClass('open');
              $nextEl[o.showMethod](o.showSpeed);
              return false;
            };
          });
    });
};
})(jQuery);
///////////////////////////
// The plugin can be invoked, for example, like this:
/* ---
$(function() {
  $('#container1').accordion();
  $('#container2').accordion({el: '.h', head: 'h4, h5', next: 'div', initShow : 'div.outer:eq(1)'});
  $('#container2').accordion({objID: '#acc2', obj: 'div', wrapper: 'div', el: '.h', head: 'h4, h5', next: 'div.outer', initShow : 'div.outer:eq(0)'});
});
--- */