$(document).ready(function() {
  // Build lightbox for popup video links.
  $('a.popup-video').replaceWith(function() {
    var content = [];
    content.push('<a class="video-trigger" href="#' + $(this).attr('id') + '">' + $(this).html() + '</a>');
    content.push('<div class="video-container">');
    content.push('<a class="popup-video" id="' + $(this).attr('id') + '" href="' + $(this).attr('href') + '" style="width: ' + $(this).attr('width') + 'px; height: ' + $(this).attr('height') + 'px;">');
    content.push('</a></div>');
    return content.join("");
  });

  // Attach flowplayer to popup videos.
  $('a.popup-video').each(function() {
    $(this).flowplayer('/flowplayer/flowplayer-3.2.7.swf', {
      clip: {
        autoPlay: true,
        autoBuffering: true,
        bufferLength: 10
      }
    });
  });

  // Attach flowplayer to inline videos.
  $('a.video').each(function() {
    $(this).css('width', $(this).attr('width'));
    $(this).css('height', $(this).attr('height'));
    $(this).removeAttr('width');
    $(this).removeAttr('height');
    $(this).flowplayer('/flowplayer/flowplayer-3.2.7.swf', {
      clip: {
        autoPlay: true,
        autoBuffering: true,
        bufferLength: 10
      }
    });
  });

  $('.video-trigger').fancybox( {
    autoDimensions: true,
    scrolling: 'no',
    type: 'inline'
  });
});

