/*
 * Programmer's Lightbox v1.0
 * Copyright (c) 2010 Dave's Software Shop, LLC
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

// define the lightbox feature
// in an object and initialize
var plb__lightbox = new Object();
plb__lightbox.visible = false;

plb__lightbox.open = function(contents)
{
  /* jQuery */
  (function($) 
  { 
    if (!plb__lightbox.overlay) return;
    if (!plb__lightbox.window) return;
    
    // when the contents to show have
    // been provided the window shall
    // wrap to fit contents precisely
    if (contents instanceof jQuery)
    {
      plb__lightbox.window.css('width', contents.css('width'));
      plb__lightbox.window.css('height', contents.css('height'));
      plb__lightbox.window.empty().append(contents);
      
      // after adding the contents scan and enable close links
      contents.find('.plb__closer').click(plb__lightbox.close);
    }
    
    // prepare lightbox for display
    // and set css style to show
    plb__lightbox.visible = true;
    plb__lightbox.reposition();
    plb__lightbox.overlay.css('display', 'block');
    plb__lightbox.window.css('display', 'block');
  })(jQuery);
}

plb__lightbox.close = function()
{
  /* jQuery */
  (function($) 
  {     
    if (!plb__lightbox.overlay) return;
    if (!plb__lightbox.window) return;
    
    // simply hide the elements and set flag
    plb__lightbox.window.css('display', 'none');
    plb__lightbox.overlay.css('display', 'none');
    plb__lightbox.visible = false;
  })(jQuery);
}

plb__lightbox.reposition = function()
{
  /* jQuery */
  (function($) 
  { 
    // only perform the repositioning
    // when visible flag is set to save
    // processing when lightbox is hidden
    if (!plb__lightbox.visible) return;
    if (!plb__lightbox.overlay) return;
    if (!plb__lightbox.window) return;
    
    // always ensure the overlay fills the window
    plb__lightbox.overlay.width($(window).width());
    plb__lightbox.overlay.height($(window).height());
    plb__lightbox.overlay.css('top', $(window).scrollTop());
    plb__lightbox.overlay.css('left', $(window).scrollLeft());
    
    // center the content window within the current browswer window by computing proper offsets
    var top = $(window).scrollTop() + ( ($(window).height() - plb__lightbox.window.height()) / 2 );
    var left = $(window).scrollLeft() + ( ($(window).width() - plb__lightbox.window.width()) / 2 );
    plb__lightbox.window.css('top', top);
    plb__lightbox.window.css('left', left);
  })(jQuery);
};

/*----------------*/
/* Initialization */
/*----------------*/

jQuery(document).ready(function($) 
{  
  // elements which compose the mechanics of the lightbox are added in all cases and ready to use
  $('body').append('<div id="plb__lightbox_overlay"></div><div id="plb__lightbox_window"></div>');
  
  // now set global references to the main elements
  plb__lightbox.overlay = $('div#plb__lightbox_overlay');
  plb__lightbox.window = $('div#plb__lightbox_window');
  
  // establish the styling 
  // necessary for the overlay
  // and window container here
  plb__lightbox.overlay.css({
    'position': 'absolute',
    'z-index': '1000',
    'top': '0px',
    'left': '0px',
    'border': '0px',
    'padding': '0px',
    'margin': '0px',    
    'background-color': 'black',
    'display': 'none'
  });
  plb__lightbox.window.css({
    'position': 'absolute',
    'z-index': '1002',
    'top': '0px',
    'left': '0px',
    'border': '0px',
    'padding': '0px',
    'margin': '0px',    
    'display': 'none'
  });
  
  // set opacity and closeability of overlay
  plb__lightbox.overlay.css('opacity', 0.6);
  plb__lightbox.overlay.click(plb__lightbox.close);
  
  // ensure lightbox adjusts as window changes
  $(window).resize(plb__lightbox.reposition);
  $(window).scroll(plb__lightbox.reposition);  
});
