(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 = {
         a : '',
         b : '',
         c : '',
         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).css('cursor','pointer');
});};

//
$.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};


})(jQuery);
////////////////////////////

$(function() {

    $('table#outer').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>');

    // 1. div.demo:eq(0):
    $('table#outer td.demo:eq(0) h4.expand').click(function() {        
        $(this).toggleClass('open')
        .next('div.normal').fadeToggle('slow','linear').end();
    });
    // 2. div.demo:eq(1):
    $('table#outer td.demo:eq(1) h4.expand').click(function() {
        $(this).toggleClass('open')
        .next('div.normal').fadeToggle('slow','linear').end();
    });
    // 3. div.demo:eq(2):
    $('table#outer td.demo:eq(2) h4.expand').click(function() {
        $(this).toggleClass('open')
        .next('div.normal').fadeToggle('slow','linear').end();
    });


});
