$(document).ready(function() {

  // Handle external links (new window)
  $('a[href^="http://"][class!=nopopup]').each(function() {
    $(this).attr('title', $(this).attr('title') + ' (opens in a new window)');
  }).click(function(evt) {
    evt.preventDefault();
    window.open($(this).attr('href'));
  });

  // Handle popups
  $('a.popup').click(function(evt) {
    evt.preventDefault();
    window.open($(this).attr('href'), 'popup', 'width=500, height=400, scrollbars=1');
  });

  // Enable tooltips on policy tables
  $('body.policy td.tooltip').tooltip({
    bodyHandler: function() {
      return '<h3>' + $(this).html() + '</h3>'
           + '<div class="body">' + this.tooltipText + '</div>';
    }
  });

  // Stripe tables
  $('table.stripe tbody tr:odd').addClass('alt');

  //lightbox overlay
  var lightBoxConfig = {
          'overlayShow'     : true,
          'overlayOpacity'  : 0.4,
          'zoomSpeedIn'     : 500,
          'zoomSpeedOut'    : 50,
          'easingIn'        : 'easeOutBack',
          'easingOut'       : 'easeInBack'
      };

  var overlayError = function(error, domElement) {
      if (jQuery.browser['mozilla']) {
          console.warn(error, domElement);
      }
  };

  $('a.mb').each(function(i){

      $(this).attr('href', $(this).attr('href') + '?format=html');
      var sizeSource = $(this).attr('rel');

      if ('' === sizeSource) {
          overlayError('Overlay link has no size data', this);
      } else {
          var heightMatches = /height:(\d+)/.exec(sizeSource);
          var widthMatches = /width:(\d+)/.exec(sizeSource);

          if ((null === heightMatches) || (null === widthMatches)) {
              overlayError('Overlay link invalid height and/or width data', this);
          } else {
              $(this).fancybox({
                  'prototype'           : lightBoxConfig,
                  'frameWidth'          : new Number(widthMatches[1]),
                  'frameHeight'         : new Number(heightMatches[1]),
                  'hideOnContentClick'	: false 
              });
          }
      }
  });

});
