$(document).ready(function() {
/* 		$('a[href^="index_source"]').each(function() {
		$(this).addClass("lightbox")}); //find all links with href linking to index_source - then add the class "lightbox"	 */	

  $('a.lightbox').click(function(e) {
    $('body').css('overflow-y', 'hidden'); // hide scrollbars!
    
    $('<div id="lightbox_overlay"></div>')
      .css('top', $(document).scrollTop())
      .css('opacity', '0')
      .animate({'opacity': '0.5'}, 'slow')
      .appendTo('body');
      
    $('<div id="lightbox_container"></div>')
      .hide()
      .appendTo('body');
      
    $('<img />')
      .attr('src', $(this).attr('href'))

      .load(function() {
        positionLightboxImage();
      })
      .click(function() {
        removeLightbox();
      })
            .appendTo('#lightbox_container');
    
    e.preventDefault();
  });
});


function positionLightboxImage() {
  var top = ($(window).height() - $('#lightbox_container').height()) / 2;
  var left = ($(window).width() - $('#lightbox_container').width()) / 2;
  $('#lightbox_container')
    .css({
      'top': top + $(document).scrollTop(),
      'left': left
    })
    .fadeIn();
}

function removeLightbox() {
  $('#lightbox_overlay, #lightbox_container')
    .fadeOut('slow', function() {
      $(this).remove();
      $('body').css('overflow-y', 'auto'); // show scrollbars!
    });
    
}

