// ==UserScript==
// @name           Flickr: Preview Set
// @namespace      http://loucypher.wordpress.com/
// @include        http://www.flickr.com/photos/*/sets/*
// @include        http://flickr.com/photos/*/sets/*
// @exclude        http://www.flickr.com/photos/*/sets/
// @exclude        http://flickr.com/photos/*/sets/
// ==/UserScript==

var viewSet = document.getElementById("ViewSet");
if (!viewSet) return;

var thumbs = document.evaluate(".//div[@id='setThumbs']//img",
                               viewSet, null, 6, null);
if (!thumbs.snapshotLength) return;

var prmImg = document.getElementById("primary_photo_img");
var prmImgTitle = prmImg.parentNode
                       .parentNode
                       .appendChild(document.createElement("div"));
prmImgTitle.style.fontWeight = "bold";
prmImgTitle.style.marginTop = ".5em";
prmImgTitle.textContent = prmImg.alt;

prmImg.parentNode.parentNode.style.backgroundPosition = "bottom center";
prmImg.parentNode.parentNode.style.backgroundRepeat = "no-repeat";

prmImg.addEventListener("load", function(e) {
  this.parentNode.parentNode.style.backgroundImage = "none";
  this.parentNode.nextSibling.textContent = this.alt;
}, false);


for (var i = 0; i < thumbs.snapshotLength; i++) {
  thumbs.snapshotItem(i).addEventListener("click", function(e) {

    var prmImg = document.getElementById("primary_photo_img");
    var thsLnk = this.parentNode;
    var prmLnk = prmImg.parentNode;
    prmImg.src = this.src.replace(/_s.jpg$/, "_m.jpg");
    var throbber = "http://l.yimg.com/www.flickr.com/images/pulser2.gif";
    if (prmLnk.href != thsLnk.href) {
      e.preventDefault();
      prmImg.parentNode.parentNode.style.backgroundImage = "url(" + throbber + ")";
    }

    prmLnk.href = thsLnk.href;
    prmImg.setAttribute("alt", this.alt);
    prmLnk.setAttribute("title", this.alt);

  }, false);
}

