(function($) {
  $.fn.figure = function() {
    $(this).each(function(i) {
      var $self = $(this),
          $img  = $self.find('img');
          $html = $self.find('p.figure_html'),
          width = $img.width(),
          text  = $html.length ? $html.remove().html() : $img.attr('alt');

      $self.width(width).append('<p class="caption"><em class="title">Figure '+(++i)+'</em>'+text+'</p>');
    });
  };

  geshi = function() {
    var links = ['hi', 'raw'];
    for(var i=0; ln=links[i]; i++) {
      $('.geshi-'+ln).each(function(){
        var geshi = $(this).parents('.geshi'),
        hide = geshi.find(i==1 ? 'ol' : 'pre'),
        show = geshi.find(i==1 ? 'pre' : 'ol');
        $(this).click(function(){
          hide.hide();
          show.fadeIn();
          return false;
        });
      });
    }
  };

  $(document).ready(function() {
    geshi();
    $('div.figure').figure();
  });
})(jQuery);
