// Adds a "Add to Google Reader" buttom to people's works.
// 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 Work to Google Reader", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          RedBubble Add Work to Google Reader
// @namespace     http://www.davep.org/
// @description   Adds a "Add to Google Reader" buttom to people's works.
// @include       http://www.redbubble.com/people/*/art/*
// @include       http://www.redbubble.com/people/*/clothing/*
// @include       http://www.redbubble.com/people/*/journal/*
// @include       http://www.redbubble.com/people/*/writing/*
// ==/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, '#' ), '?' );
}

// Go looking for the list of actions that can be performed on the work.
var rbActions = document.getElementById( 'actions' );

// If we've got the actions section...
if ( rbActions ) {
   
   // Look for the actual list...
   var rbActionList = rbActions.getElementsByTagName( 'ul' );

   // Got it?
   if ( rbActionList )
   {
      // Create the new elements.
      var li  = document.createElement( 'li' );
      var add = document.createElement( 'a' );
      var img = document.createElement( 'img' );

      // Set up the image.
      img.setAttribute( 'src', 'http://gmodules.com/ig/images/plus_google.gif' );
      img.style.border = 'none';

      // Set up the link.
      add.setAttribute( 'href',   'http://fusion.google.com/add?feedurl=' + cleanURL( location.href ) + '/comments.atom' );
      add.setAttribute( 'title',  'Watch this work in Google reader' );
      add.setAttribute( 'target', '_blank' );

      // String it all together.
      add.appendChild( img );
      li.appendChild( add );
      rbActionList[ 0 ].appendChild( li );
   }
}
