////////////////////////////
// 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 : '[Expand All]',
         collapse : '[Collapse All]',
         a : '<p id="switch"><a href="#expand-all/collapse-all"><span>',
         b : '</span><span class="hidden">',
         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:
$.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() {
	$(".expand").addTrigger().find('div.collapse').hide().end()
    .find('h4.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('h4.expand'),
            $sp = $(this).closest('div').find('span:first');
        ($sp.hasClass('hidden')) ? $exp.addClass('open') : $exp.removeClass('open');
        ($sp.hasClass('hidden')) ? $cllps.show() : $cllps.hide();
    });
    //////////////////////////////
    $('.expand').click(function() {
        $(this).toggleClass('open')
        .next('div.normal').fadeToggle('slow','linear').end()
        .next('div.slow').slideFadeToggle('slow','linear');
    });
});
