// ==UserScript==
// @name          Google Reader Quick Links
// @namespace     http://whitebucket.com/greasemonkey
// @description   Adds shortcut keys for links in the body of an item in Google Reader.
// @require       http://arantius.com/misc/greasemonkey/imports/dollarx.js
// @include       http://www.google.com/reader/*
// @include       https://www.google.com/reader/*
// @include       http://google.com/reader/*
// @include       https://google.com/reader/*
// ==/UserScript==

// GoogleReaderQuickLinks
// Copyright (c) 2005, Ben Beckwith (ben at whitebucket dot com)
// Copyright (c) 2007, Ugo Di Profio
// Copyright (c) 2008, Anthony Lieuallen
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html

// This flag indicates that the control key has been pressed
var controlkey=0;
// This variable keep track of the current set of enumerated links.
var linkOffset=0;
// This variable describes the size of the links
var linkNumber=10;

// The following variables are the shorcut (hotkey) key codes used.
// The Control key code is the main button to push to "activate" the features of this script
// The Open key code opens a Google Reader item in a background tab
// The Label key code enumerates the links within the body of a Google Reader Item.
var controlKeyCode=104; // "h"
var openKeyCode   =104; // "h"
var labelKeyCode  =108; // "l"

// Apply the numberical [X] to the links
function LabelLinks() {
	// Get the number of links in the item's body
	var links=$x('//div[@id="current-entry"]//div[@class="item-body"]//a');
	console.log('links:', links);
	// Remove Labels from previous block
	for (var i=(linkOffset-linkNumber); (linkOffset>=linkNumber)&&(i < links.length )&&(i<linkOffset); i++) {
		linkname=links[i].innerHTML;
		links[i].innerHTML=linkname.substring(0,linkname.length-4);
	}
	// Check if last block of links have been labeled already, and reset offset.
	if (linkOffset>links.length) {
		linkOffset=0;
	}
	// Loop through the links, adding the identifier
	for (var i=linkOffset; (i < links.length )&&(i<(linkNumber+linkOffset)); i++) {
		links[i].innerHTML+=" ["+(i-linkOffset)+"]";
	}
	linkOffset+=linkNumber;
}

// Function to handle the keypress events
function LinkKey(event) {
	// Get the keycode for the keypress
	var kcode=(event.keyCode)?event.keyCode:event.which;
	// Get the key pressed in string format
	var k=String.fromCharCode(kcode);
	// If the controlkey flag is set, then the user has 'activated' the script and
	// is trying to open an item, enumerate links, or open a link.
	if (controlkey) {
		// Check to see if it fits our range
		if (k>="0" && k<="9" && controlkey && (linkOffset>0)) {
			// If the number is in range, then get that link.
			var link=$x('//div[@id="current-entry"]//div[@class="item-body"]//a');
			link=link[parseInt(k, 10)+linkOffset-linkNumber];
			if (link) {
				// If the link is valid, open it in a tab.
				GM_openInTab(link.href);
			}
		}
		// If the label key has been pressed, then enumerate the links.
		if (kcode==labelKeyCode) {
			LabelLinks();
		}
		// If the open key has been pressed, then open the Google Reader item in a tab.
		if (kcode==openKeyCode) {
			GM_openInTab(document.getElementById("current-entry").getElementsByTagName("A")[0].href);
			linkOffset=0;
		}
		// Stop the event from being processed.
		event.stopPropagation();
		// Reset the controlkey flag
		controlkey=0;
	} else {
		// set the controlkey flag if the control key has been pressed.
		if (kcode==controlKeyCode) {
			controlkey=1;
			event.stopPropagation();
		}
	}
	// If we move away from a Google Reader item, reset the link offset
	if ( k=="o" || k=="j" || k=="k" || event.which==13 ) {
		if (0==controlkey) {
			linkOffset=0;
		}
	}
}

// Add listener for to handle the keypress events.
document.addEventListener("keypress", LinkKey, true);
