
/* Author : Bertrand Mansion <bmansion@mamasam.com> */

(function($) {
    $.fn.colorize = function() {
        this.each(function() {
          var html = $(this).html();
          var lines = html.split('<br>');
          lines.push('');
          var buf = [];
          var num = 0;
          var RegExp = /^\s*([>\s]+)/;
          var quoted = false;
          $.each(lines, function() {
            var line = this.replace(/&gt;/g, '>');
            var quotes = line.match(RegExp);
            if (!quotes) {
              quotes = [''];
              if (quoted) {
                buf.push('</div>');
              }
              quoted = false;
            }
            var match = quotes[0].replace(/ /g, '');
            if (num != match.length) {
              if (num > 0) {
                buf.push('</span>');
              }
              if (!quoted && match.length != 0) {
                buf.push('<div class="quote">');
                quoted = true;
              }
              num = match.length;
              if (num > 0) {
                buf.push('<span class="quoted' + num + '">');
              }
            }
            buf.push(this + '<br>');
          });
          var res = buf.join('');
          $(this).html(res);
        });
        return $(this);
    }
})(jQuery);

function hideQuote(el) {
  var l = $('<a class="readmore" href="#">&gt; show quoted text</a><br />').click(function (e) {
    e.preventDefault();
    $(l).hide();
    el.show();
  });
  el.hide().after(l);
}

(function($) {
    $.fn.readmore = function() {
        this.each(function() {
          var div = $(this);
          if (div.parent().find(":last").closest(".quote").get(0) == div.get(0)) {
            hideQuote(div);
          } else {
            var len = $.trim(div.text()).length;
            if (len < 80*8) {
              return;
            }
            hideQuote(div);
          }
        });
        return $(this);
    }
})(jQuery);

(function($) {
    $.fn.threadize = function() {
        this.each(function() {
          var mess = $(this);
          var meta = $(this).find(".meta a");
          var body = $(this).find(".body");
          var hash = unescape(self.document.location.hash.substring(1));
          if (hash != meta.attr("name")) {
            body.hide();
            mess.css("background-color", "#eeeeee");
          }
          meta.click(function(e) {
            e.preventDefault();
            if (body.is(":visible")) {
              mess.css("background-color", "#eeeeee");
              body.hide();
            } else {
              mess.css("background-color", "#ffffff");
              body.show();
            }
          });
        });
        return $(this);
    }
})(jQuery);
