var $j = jQuery.noConflict();
$j(document).ready(function() {
  // add a helpful instruction for users
  $j('#p2c-header p:last').append(' Click on the thumbnails on the left to see more.');
  // create the gallery area
  $j('#archives').before('<div id="side-gallery"></div>');
  // for each entry, get an image and copy to the gallery
  $j('.content-item').each(function(i) {
    var permalink = $j(this).find('h2 a').attr('href');
    $j(this).addClass('portfolio-content');
    var wrap = $j('<div class="gallery-item"></div>');
    var wrap2 = $j('<div class="fullsize-item"><a href="'+permalink+'"></a></div>');
    var target = $j(this);
    $j(this).find('img').eq(0).
      prependTo(wrap2.find('a')).
      removeAttr('align').removeClass('alignright').
      removeAttr('style').
      clone().appendTo(wrap);
    $j(this).prepend(wrap2);
    $j('#side-gallery').append(wrap);
    // hide each one apart from the first
    if(i > 0) {
      $j(this).hide();
    }
    // on click show this item and hide the rest
    wrap.click(function() {
      $j('.content-item').hide();
      target.show();
    });
    // get a blockquote if any and keep it
    var quote = $j(this).find('blockquote').eq(0).clone();
    // remove content after the first paragraph
    $j(this).find('p:first').append(' <a class="readmore" href="'+permalink+'">More&gt;</a>').
    nextAll().remove();
    if(quote.size() > 0) {
      var wrap3 = $j('<div class="client-quote"></div>');
      wrap3.append(quote);
      $j(this).append(wrap3);
    }
  });
});
