/* Copyright © 2010-2011 Jamie Zawinski <jwz@jwz.org>

   Permission to use, copy, modify, distribute, and sell this software
   and its documentation for any purpose is hereby granted without
   fee, provided that the above copyright notice appear in all copies
   and that both that copyright notice and this permission notice
   appear in supporting documentation.  No representations are made
   about the suitability of this software for any purpose.  It is
   provided "as is" without express or implied warranty.

   Created: 15-Jul-2010.

   This Javascript, when loaded into a NetNewsWire RSS-entry-display page,
   improves readability of certain feeds.

   - If there is a link to a Youtube video, but the video is not embedded
     in the document, an embed is created.  (This is because RSS feeds of
     Youtube users/channels do not include the embedded video.)

   - If there is an embedded Flickr photo, and it's a thumbnail, the
     image is replaced with the large (1024-ish) image instead.
     (This is because RSS feeds of Flickr users and groups only include
     the thumbnail image.)

   See also "jwz.nnwstyle" on http://www.jwz.org/hacks/
 */

function add_youtube_embeds() {
  var result = new Object();

  // Iterate over every link and find the youtube IDs.

  var tags = document.getElementsByTagName('a');
  var re = new RegExp('^https?://(?:www\.)?youtube(?:-nocookie)?\.com/' +
                      '(?:watch[?#]v[=/]|v/)([^<>&,?]+)');
  for (var i = 0; i < tags.length; i++) {
    var m = tags[i].href.match(re);
    if (m && m[1]) { 
      var html = ("<p>" +
                  "<iframe width=853 height=505 src='" +
                  "http://www.youtube.com/embed/" + m[1] + "'>" +
                  "</iframe>");
      result[m[1]] = html;
    }
  }

  // Iterate over every object tag and omit those IDs.

  var tags = document.getElementsByTagName('object');
  for (var i = 0; i < tags.length; i++) {
    var kids = tags[i].childNodes;
    for (var j = 0; j < kids.length; j++) {
      if (kids[j].tagName == 'PARAM' && kids[j].name == 'movie') {
        var m = kids[j].value.match(re);
        if (m && m[1]) { 
          delete (result[m[1]]);
        }
      }
    }
  }

  // Likewise for embed and iframe tags.

  tags = document.getElementsByTagName('embed');
  for (var i = 0; i < tags.length; i++) {
    var m = tags[i].src.match(re);
    if (m && m[1]) {
      delete (result[m[1]]);
    }
  }

  tags = document.getElementsByTagName('iframe');
  for (var i = 0; i < tags.length; i++) {
    var m = tags[i].src.match(re);
    if (m && m[1]) {
      delete (result[m[1]]);
    }
  }


  // Now emit any missing videos, but no more than 8 (since webkit loses
  // its mind if you have dozens of youtube embeds on one page.)

  var count = 0;
  var max = 8;
  for (var id in result) {
   if (count++ < max) {
     document.write(result[id]);
   }
  }
}


// Iterate over every image and hack the SRCs.
//
function enlarge_flickr_images() {
  for (var i = 0; i < document.images.length; i++) {
    var img = document.images[i];
    var m = img.src.match(new RegExp('^(https?://([a-z0-9.]*\.)?flickr\.com/' +
                                     '.*)_[stm](.jpg)$'));
    if (m) {
      // Have to create a new img node, because there's no way to unset
      // the width= and height= parameters.
      var img2 = document.createElement('img');
      img2.src = m[1] + "_b" + m[3];
      img.parentNode.replaceChild(img2, img);
    }
  }
}

add_youtube_embeds();
enlarge_flickr_images();

