// Adds a "Add to Google Reader" buttom to forum threads.
// Version 0.2
// 2008-01-16
// Copyright (c) 2008, Dave Pearson <http://www.davep.org/>
// Released under the GPL
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey: http://www.greasespot.net/
// 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 "RedBubble Add Thread to Google Reader", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          RedBubble Add Thread to Google Reader
// @namespace     http://www.davep.org/
// @description   Adds a "Add to Google Reader" buttom to forum threads.
// @include       http://www.redbubble.com/groups/*/forums/*/topics/*
// @include       http://www.redbubble.com/*/forums/*/topics/*
// ==/UserScript==

// Strip a string from the first instance of a given string.
function stripFromFirst( clean, from )
{
   var cleaned = clean.split( from );

   if ( cleaned.length > 1 )
   {
      clean = cleaned[ 0 ];
   }

   return clean;
}

// Clean an URL of parameters and # links.
function cleanURL( url )
{
   return stripFromFirst( stripFromFirst( url, '#' ), '?' );
}

// Look for the formatting help text.
var formHelp = document.getElementById( 'form-help' );

// Did we get it?
if ( formHelp )
{
   // Yes, insert the Google button into it.
   formHelp.innerHTML += '<p>' +
      '<a target="_blank" title="Watch this thread in Google reader" href="http://fusion.google.com/add?feedurl=' + cleanURL( location.href ) + '.rss">' +
      '<img src="http://gmodules.com/ig/images/plus_google.gif" style="border: none;" />' +
      '</a>' +
      '</p>';
}
