// Page wrap thing (I will never get CSS)
// 
// Image links with title attribute on the fly, for Lightbox.
// Which (amen, hallelujah, peanut butter) works in IE, too, with the prop style border line.

$(document).ready(function(){
  
  // Make the main wrapper about as high as the window, so you don't see things "under" the page moving about
  //   when the window is resized. Since layoutUpdate runs continuously during resizing, most of the DOM stuff was "pre-computed".
  var $wrapper        = $("#wrapper");
  var $window         = $(window);
  var topMarginHeight = parseInt($("body").css("marginTop"), 10);
  function layoutUpdate() {
    $wrapper.css("height", ($window.height() - topMarginHeight - 10) + "px");
  }
  layoutUpdate();
  $(window).resize(layoutUpdate);
  
  // -- Lightbox functionality for images
  // Since the body text is input using TinyMCE and having to add a link around each image is icky, just have jQuery do it.
  // Filter out images with border style specifically set to '0' however, this way the user can "disable" lightbox for certain images
  //   (note: the not() method is buggy up until and including 1.6.3 it seems, works in jquery-edge)
  // Then, call the lightbox plugin for every class="lightbox" link in #main (<-- page body text)
  $('img:not([style*="border: 0"])', '#main').each(function() {
    var _this = $(this);
    if (_this.prop('style')['border'] != '') return;
    var img_name = encodeURI(_this.attr('src').split('/').pop()); // Halleluja, it works in IE!!1!
    // Todo: check the target image actually exists?
    // Use the TinyMCE image's alt property for the link's title attribute, Lightbox uses that.
    _this.wrap('<a href="/images/'+ img_name +'" title="'+ _this.attr('alt').htmlents() +'" class="lightbox" />');
  });
  $('#main a.lightbox').lightBox({
    // why can't the script figure out its own location and go from there?
    imageLoading:  '/lib/jquery-lightbox-0.5/images/lightbox-ico-loading.gif',
    imageBtnClose: '/lib/jquery-lightbox-0.5/images/lightbox-btn-close.gif',
    imageBtnPrev:  '/lib/jquery-lightbox-0.5/images/lightbox-btn-prev.gif',
    imageBtnNext:  '/lib/jquery-lightbox-0.5/images/lightbox-btn-next.gif',
    imageBlank:    '/lib/jquery-lightbox-0.5/images/lightbox-blank.gif'
  }); // Apply Magic.
  
});
