// QRFlickr script
// version 0.5
// 2007-06-18
// Copyright (c) 2007, Dash Labs
// Author Josh Russell - http://twitter.com/joshr
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "QRFlickr", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          QRFlickr
// @namespace     http://joshrussell.com/
// @description   Creates a QR Code for the image on a Flickr photo page
// @include       http://www.flickr.com/photos/*
// @include       http://flickr.com/photos/*
// ==/UserScript==

// This is a hack, i'm not a coder :)

// I played around with creating more behaviours in the browser to make
// the QR image less intrusive but got distracted by other work!

// like getElementByTag but for classes
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// gets the photo URL from the IMG tag for the photo
var qrPhotoTag = getElementsByClass(document,'reflect','img');
var qrPhotoURL = qrPhotoTag[0].src;

// generates the QR code and puts it under the photostream navigation on a photo page
var qrCode = getElementsByClass(document,'ContextTop','div');
qrCode[0].style.background = "transparent url(http://qrcode.kaywa.com/img.php?s=5&d=" + qrPhotoURL + ") no-repeat left bottom";
qrCode[0].style.padding = "0px 0px 220px 0px";