$(document).ready(function() {

/** Java ready? **/
$('body').addClass('java');


/** Portfolio junk **/
if($('body.portfolio').length) {

$('div.view').append('<div class="thmbly"></div>');

$('div.view').each(function() {
  // 0. Define stuff
  var thisGal = $(this);
  var thisGalID = $(this).parent().attr('id');
  var imgCount = $(thisGal).children('img').length;
  var secH = 400;
  var secW = 600;
  var partitionW = Math.floor(secW/imgCount);
  
  // 1. Hide the images
  $(thisGal).children('img').hide();
  $(thisGal).css('background', "url(\'" + $(thisGal).children('img').slice(0,1).attr('src') + "\')");
  
  // 2. Build partition links
  $(thisGal).children('img').each(function(i) {
    var thisSrc = $(this).attr('src');
    // 2.a. dA-style partitions
    $('<a/>')
      .html(' ')
      .addClass('partition')
      .width(partitionW + 'px')
      .height(secH + 'px')
      .css('left', partitionW*i + 'px')
      .attr('href','#')
      .bind('click mouseover', function() {
        $(thisGal).css('background', "url(\'" + thisSrc + "\')");
        $('#' + thisGalID + ' .thmbly a').removeClass('selected').fadeTo(1, 0.3);
        $('#' + thisGalID + ' .thmbly a:eq(' + i + ')').addClass('selected').fadeTo(1, 0.8);
        return false;
      })
      .appendTo($(thisGal));
      
    // 2.b. thmb blocks
    $('<a/>')
      .text(i)
      .addClass('thmb')
      .attr('id',thisGalID+i)
      .attr('href',thisSrc)
      .fadeTo(1, 0.3)
      .bind('click mouseover', function() {
        $(thisGal).css('background', "url(\'" + thisSrc + "\')");
        $(this).siblings().removeClass('selected').fadeTo(1, 0.3);
        $(this).addClass('selected').fadeTo(1, 0.8);
        return false;
      })
      .appendTo($(thisGal).find('.thmbly'));
    
    $(thisGal).find('.thmbly a:first').click();
  });
  
  // 3. Select first digs

});

}
/** End Portfolio junk **/

});