// ==UserScript==
// @name          Geocaching Thumbnails
// @namespace     http://benchmarks.org.uk/geothumbs/
// @description   (v1.0) Changes the default picture icons on cache pages to image thumbnails
// @include       http://www.geocaching.com/seek/*
// ==/UserScript==

// Copyright (C) 2007 Gary Player <dev@benchmarks.org.uk>
// Released under the GPL http://www.gnu.org/copyleft/gpl.html
// This is a Greasemonkey user script, see http://greasemonkey.mozdev.org/.


//////////////////////////////////////////////////////////////////
// Change this value to the required number of images per row   //
//////////////////////////////////////////////////////////////////
var imagesPerRow=2;

//////////////////////////////////////////////////////////////////
// Change this value to true to show images tagged as spoilers  //
//////////////////////////////////////////////////////////////////
var showSpoilers=false;


// regexp to use to check for spoilers
var spoilerPattern=/^\s*spoil/i;

// obtain agent as we can run in IE using Turnabout
var msie = window.navigator.userAgent.match("MSIE");

// get log container table
var logContainer = document.getElementById('CacheLogs').getElementsByTagName('table');

if (!logContainer.length)
	return;

// get tables containing images
var imageTables = logContainer[0].getElementsByTagName('table');

var col;

// process each set of images
for (var i=0;i<imageTables.length;i++)
{
	var it = imageTables[i];

	// force new row
	col=imagesPerRow;

	var anchors = it.getElementsByTagName('a');

	// process each image anchor
	var noAnchors = anchors.length
	for (var j=0;j<noAnchors;j++)
	{
		// create a new row if required
		var newRow;
		if (col==imagesPerRow)
		{
			// add row
			newRow=it.insertRow(-1);
			col=0;
		}

		// save the original anchor
		var origAnchor = anchors[j];

		/////// create image cell /////////
		
		// create a copy of the original anchor
		var anchor = origAnchor.cloneNode(true);
		var anchorText = anchor.lastChild.data
		var anchorHref = anchor.href;

		// clean up the tooltip
		var img = anchor.firstChild;	
		img.title = anchorText;
		img.alt = anchorText;
		
		// change default photo.png image to thumbnail
		// unless we want to hide spoilers!
		
		if (!showSpoilers && !anchorText.match(spoilerPattern))
		{
			// get href to log image and convert to thumbnail href
			var hrefThumb = anchorHref.replace(/log/, "log\/thumb");
			img.src = hrefThumb;
		}
		
		// remove the non image anchor text
		anchor.removeChild(anchor.lastChild);
		
		// create a new cell in the row and add the anchor
		var newCell = newRow.insertCell(-1);
		newCell.appendChild(anchor);
		newCell.align="center";
		newCell.width="100px";
		newCell.height="81px";

		/////// create cell with non lightbox link to image /////////

		// create a copy of the original anchor
		var anchor2 = origAnchor.cloneNode(true);

		// remove the image
		anchor2.removeChild(anchor2.firstChild);
		
		// simplify the link
		anchor2.rel="";
		anchor2.title=anchorText;
		anchor2.href=anchorHref;

		// create a new cell in the row and add the anchor
		newCell = newRow.insertCell(-1);
		newCell.appendChild(anchor2);
		
		// don't set width for IE
		if (!msie) newCell.width="600px";
		
		col++;
	}

	// delete original row
	it.deleteRow(0);
}
