(function (w, d) {
  "use strict";

  // Copy img src to rollover text box mask image on load, this is an easy way
  // to lazyload the mask image and keep it in sync with the preview
  // image. Because the load event will fire each time the image src changes,
  // this ensures that they are both using the same src at all times.
  d.addEventListener('lazybeforeunveil', function (e) {
    var img = e.target;
    var id = img.getAttribute('data-tb-mask-image-id');
    !!id && img.addEventListener('load', function (e) {
      var img = e.target; // avoid closing over img
      var maskImage = d.getElementById(id);
      if (maskImage) {
        maskImage.setAttribute('href', img.currentSrc);
        maskImage.setAttribute('xlink:href', img.currentSrc);
      }
    });
  });

}(window, document));
