/*
 * Programmer's Lightbox v1.21
 * 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)
    {
      // when the special parameter has been embedded in the 
      // contents of the lightbox ensure element data is saved
      if (plb__lightbox.window.children().data('plb__preserve'))
      { plb__lightbox.window.children().detach(); plb__lightbox.window.append(contents); }
      else plb__lightbox.window.empty().append(contents);
        
      // retrieve the current size of the contents and adjust the window to the proper size to surround content
      plb__lightbox.window.css('width', contents.hasClass('plb__auto_width') ? 'auto' : contents.css('width'));
      plb__lightbox.window.css('height', contents.hasClass('plb__auto_height') ? 'auto' : contents.css('height'));
      
      // after adding the contents scan and enable
      // close links where they are not enabled
      var closers = contents.find('.plb__closer');
      if (closers) closers.each(function(i, el) {
        var el = $(el);
        if (el.data('plb__enabled')) return;
        el.click(plb__lightbox.close);
        el.data('plb__enabled', true);
      });
    }
    
    // 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');
    plb__lightbox.reposition();
  })(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.7);
  plb__lightbox.overlay.click(plb__lightbox.close);
  
  // ensure lightbox adjusts as window changes
  $(window).resize(plb__lightbox.reposition);
  $(window).scroll(plb__lightbox.reposition);  
});
