/*
 * Summary - Expandable and shrinkable bodies of text.
 *
 * Copyright (c) 2008 Brandon Diamond <brandon.t.diamond@gmail.com>, http://www.megathink.com
 * $Version: 7/25/2008 +r1 beta
 * 
 */
(function($) {
  $.fn.summary = function(options){
    options = $.extend({
      maxlength: 100,
      allowToggle: true,
      expandClass: "expand-link",
      shrinkClass: "shrink-link",
      expandText: "...",
      shrinkText: "(less)",
      button: "<a href='#' />"
    }, options);

    this.each(function() {
      var $this = $(this);

      if($this.text().length > options.maxlength) {
        var base = $this.text().substr(0, options.maxlength);
        var extra = $this.text().substr(options.maxlength);
        
        $this.empty();
        $("<span />").text(base + (options.allowToggle ? "" : options.expandText))
                     .appendTo(this);

        if(!options.allowToggle) {
          return true;
        }

        $(options.button).addClass(options.expandClass)
                         .text(options.expandText)
                         .appendTo(this)
                         .click(function() {
                           $(this).hide();
                           $(this).next("."+options.shrinkClass).show();
                           $this.children("span:first").text(base + extra);
                           return false;
                         });
        
        $(options.button).addClass(options.shrinkClass)
                         .text(options.shrinkText)
                         .hide()
                         .appendTo(this)
                         .click(function() {
                           $(this).hide();
                           $(this).prev("."+options.expandClass).show();
                           $this.children("span:first").text(base);
                           return false;
                         });
    }});

    return this;
  };
})(jQuery);

