jQuery.fn.truncate = function(max,settings) {
    settings = jQuery.extend({
        chars: /\s/,
        leave: false,
        trail: [false, " ...", ""]
    }, settings);
    return this.each(function() {
        if (!settings.leave || (settings.leave && jQuery(this).children().length == 0)) {
            v = jQuery.trim(jQuery(this).text());
            while (max < v.length) {
                c = v.charAt(max);
                if (c.match(settings.chars)) {
                    d = v.substring(0,max) + settings.trail[1];
                    jQuery(this).html(d);
                    break;
                }
                max--;
            }
            if (settings.trail[0]) {
                jQuery(this).html("<span>" + d + "</span>").append("<span>" + v + "</span>").find("span:eq(1)").append(settings.trail[2]).css("display","none");
                jQuery("a:eq(0)",this).click(function() {
                    jQuery(this).parent().css("display","none").parent().find("span:eq(1)").css("display","inline");
                    return false;
                });
                jQuery("a:eq(1)",this).click(function() {
                    jQuery(this).parent().css("display","none").parent().find("span:eq(0)").css("display","inline");
                    return false;
                });
            }
        }
    });
};
