////////////////////////////
// http://adipalaz.awardspace.com/experiments/jquery/expand.html
///////////////////////////
(function($) {
$.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.addTrigger = function(options) {
    var defaults = {
         expand : '[ + ]',
         collapse : '[ - ]',
         a : '<p id="switch"><a href="#expand-all/collapse-all"><span title="Expand All">',
         b : '</span><span class="hidden" title="Collapse All">',
         c : '</span></a></p>',
         ref : 'div:first',
         el : '#' + this.attr("id") + ' '
    };
    var options = $.extend(defaults, options);   
    return this.each(function() {
        $(options.a + options.expand + options.b + options.collapse + options.c).insertBefore(options.el + options.ref);
});};
//http://www.learningjquery.com/2008/02/simple-effects-plugins:
})(jQuery);
////////////////////////////
$(function() {
	$('#outer').addTrigger().find('div.hide').hide().end()
    .find('.expand').css('cursor','pointer').orphans().wrap('<a style="display:block" href="#expand/collapse" title="expand/collapse"></a>');
        /* ---  * If you want to have your DIVs initially expanded:
            remove: $('.collapse').hide();
            --- * The first DIV initially expanded:
            $('#outer').addTrigger().find('div.collapse:gt(0)').hide().end()
            .find('h4.expand:eq(0)').addClass('open').end() ...  --- */

    /// Expand All/Collapse All ///        
    $('#switch').click(function () {
        $(this).find('span').toggleClass('hidden');
        var $cllps = $(this).closest('div').find('div.collapse'),
            $exp = $(this).closest('div').find('.expand'),
            $sp = $(this).closest('div').find('span:first');
        ($sp.hasClass('hidden')) ? $exp.addClass('open') : $exp.removeClass('open');
        ($sp.hasClass('hidden')) ? $cllps.show() : $cllps.hide();
    });
    //////////////////////////////
    // div.demo:
    $('#outer .expand').click(function() {
        $(this).toggleClass('open')
        .next('div.collapse').slideToggle().end()
    });
});

