function feature(e) {
  if (e != "") {
    featured = e.attr('id');
    window.location.hash = '#'+featured.replace('work_', '');


    $('#featured').fadeOut(function(){ 
        $('#placeholder').show();

        var img = new Image();
        $(img).load(function() {
          e.data("loaded", true);
          $('#featured').html($('<div></div>').append(e.html()));
          $('#featured').prepend(this);
          $('#featured .info').show();
          $('#featured .thumb').remove();
          $('#featured a').remove();
          $('<a onclick="next(); return false;" class="next" href="#">Next &raquo;</a><br class="clr"/>').appendTo($('#featured'));
          $('#placeholder').hide();
          $('#featured').fadeIn();
        }).attr('src', e.find('a').attr('rel'));
        $("html, body").animate({ scrollTop: 0 }, "fast");
    });
   
  }
}

function next() {
  current = $('#' + featured);
  if (current.is('.works li:last-child')) {
    feature($('.works li:first-child'));
  } else {
    feature(current.next());
  }
}

function checkHash() {
  if ( window.location.hash != current_hash ) {
    current_hash = window.location.hash;
    feature($('#work_'+current_hash.replace('#','')));
 }
}

$(document).ready(function() {
  featured = '';
  $('.works li a').click(function() {
    if (featured == $(this).parent().attr('id')) {
      $('#featured').fadeOut(function(){ $(this).text('') }); 
      window.location.hash='';
    } else {
      /* feature($(this).parent()); */
      window.location.hash=$(this).parent().attr('id').replace('work_', '');
    }
  });
  current_hash = ""
  hashCheck = setInterval( "checkHash()", 50 );
});

