// ==UserScript==
// @name Google Images - Image Direct Link
// @namespace http://tazzito.wordpress.com
// @author Taz-G (Carmelo Minnella)
// @description Adds a direct link to the image around the attributes informations
// @license GPL
// @include http://images.google.*/*
// @created 2007
// @version 1.2 - 2009/07/25 
// ==/UserScript==
/* ***********************************
@releaseNotes 1.1
 	- Bug : Adaptation to the new Google HTML structure pages (the attributes text has moved in a DIV element) and code simplification
	- Code : The bad-formed structure detection has been removed (it gave no usefull information and no real protection)
	- User : The Direct Link looks like the original style (no more link appearance) and the tooltip contains the image URL
@ releaseNotes 1.2
	- Bug : If the attribute text is not in a single Text type Node it disappears. For example Google could put the text in a <b> and the node type changes
	- Code : Re-assumed minimal bad-formed structure exceptions
**************************************/
(function() {
	var curThumb = 0; // Thumb index
	var detail = null; // Current tDataText node
	var image = null; // Current tDataImage node
	var hrefImage = ""; // Direct link to the image
	var linkImageNode = null; // <a> element for the direct link
	var REGEX_IMAGE = "imgurl=(.*?)&"; // Regexp to extract the image URL
	var curTag = null; // Current image attributes node
	/*
	* Each thumb is referenced  by a tDataText and a tDataImage element.
	* The first index is 0.
	*/
	detail = document.getElementById("tDataText" + curThumb);
	image = document.getElementById("tDataImage" + curThumb);
	while(null!=detail && null!=image) {
		// "Escape" the <font> tag inside the <td> ID
		detail = detail.firstChild;
		image = image.firstChild;
		// Find the image attributes node
		curTag = document.evaluate("div[@class=\"f\"]", detail, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		try {
			if(null==curTag) throw "ERR_TAGNOTFOUND";
			curTag = curTag.singleNodeValue; // convert xPath result to a node
			hrefImage = image.getAttribute("href").match(REGEX_IMAGE)[1]; // Get the image URL only
			// Add the direct link around the image attributes
			linkImageNode = document.createElement("a");
			linkImageNode.setAttribute("title", "Direct Link : "+hrefImage);
			linkImageNode.setAttribute("href", hrefImage);
			linkImageNode.setAttribute("class", "f");
			linkImageNode.setAttribute("style", "text-decoration: none;");
			curTag.parentNode.insertBefore(linkImageNode, curTag); // Insert the <a> in the document
			linkImageNode.appendChild(curTag); // Add the tag inside the <a> (it is implicitily removed from the old location)
			// Next thumb
			curThumb++ ;
			detail = document.getElementById("tDataText" + curThumb);
			image = document.getElementById("tDataImage" + curThumb);
		} catch(error) {
			GM_log("The userscript #Google Images - Image Direct Link# doesn't match with the expected data structure, please disable it. CodeError="+error);
			break;
		}
	}})();