
// http://adipalaz.awardspace.com/experiments/jquery/expand.html

(function($) {
//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.expandAll = function(options) {
    var defaults = {
         a : '<p id="sw"><a href="#expand-all/collapse-all">',
         b : '</a></p>',
         trigger1 : 'Expand All',
         trigger2 : 'Collapse All',
         cont : '#' + this.attr("id") + ' ',
         ref : 'div:first',
         showMethod : 'show',
         hideMethod : 'hide',
         speed : ''
         };
    var options = $.extend(defaults, options);   
    return this.each(function() {
        $(options.a + options.trigger1 + options.b).insertBefore(options.cont + options.ref);
        $(this).find('#sw a').click(function() {
        var $cllps = $(this).closest('div').find('.collapse'),
            $exp = $(this).closest('div').find('h3.ex');
        if ($(this).text() == options.trigger1) {
          $(this).text(options.trigger2);
          $exp.addClass('open');
          $cllps[options.showMethod](options.speed);
        } else {
          $(this).text(options.trigger1);
          $exp.removeClass('open');
          $cllps[options.hideMethod](options.speed);
        }
    });
});}; 

//http://www.learningjquery.com/2008/02/simple-effects-plugins:
$.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};
$.fn.slideFadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};
})(jQuery);
////////////////////////////
$(function() {
    $('#js').expandAll().find('div.collapse').hide().end()
    .find('h3.ex').css('cursor','pointer').orphans().wrap('<a style="display:block" href="#expand/collapse" title="Expand/Collapse"></a>');
    
    // 1. div.demo:eq(0):
    $('#js div#section-js h3.ex').click(function() {
        $(this).toggleClass('open')
        .next('div.normal').toggle().end()
        .next('div.slow').toggle('slow');
        });
 });

