jQuery.fn.truncate = function(max,settings) {
	var d = "";
	var v = "";
	var c = "";
    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());
			if(v.length > 0 && v.length > max) {
	            while (max < v.length && max >= 0) {
	                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;
	                });
	            }
			} else {
				/* modify the DOM structure as if there had been a trunctation done (ewe) */
				jQuery(this).html("<span>" + v + "</span>");
			}
        };
    });
};