/** :mode=javascript:indentSize=2:noTabs=true:tabSize=2:
  @brief  simple lightbox widget

  displays a simple lightbox for any a link that has the "lightbox" class
*/

function init_lightbox() {
  $("a.lightbox").click(function() {
    // turn off page scrollbars (this will break the overlay effect)
    $("body").css('overflow-y','none');

    // nifty fade-in overlay
    $("<div id='lightbox_overlay'></div>")
      .css('opacity','0')
      .css('top',$(document).scrollTop())
      .animate({'opacity':'0.5'},'slow')
      .appendTo('body');

    // lightbox container
    $("<div id='lightbox'></div>")
      .hide()
      .appendTo('body');

    // display image
    $("<img />")
      .attr('src',$(this).attr('href'))

      // scale so it will fit in the screen nicely
      // right now this is just a constant 1/6 factor
      .attr('width',$(this).attr('iw')*(1/6))
      .attr('height',$(this).attr('ih')*(1/6))

      // delay image display until it has fully loaded
      .load(function() {
          var top=($(window).height()-$("#lightbox").height())/2;
          var left=($(window).width()-$("#lightbox").width())/2;
          $("#lightbox")
            .css({
              'top':top+$(document).scrollTop(),
              'left':left
            })
            .fadeIn();
      })

      // close lightbox on click
      .click(function() {
          $("#lightbox_overlay, #lightbox")
            .fadeOut('slow',function() {
                $(this).remove();
                $("body").css('overflow-y','auto'); // restore scrolling
            });
      })
      .appendTo("#lightbox");

    // disable normal link navigation
    return false;
  });
}

