// BookMooch Enhancher
// version 1.1
// 20/09/2007
// Copyright (c) 2007, Rob Sharp
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// 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 "BookMooch Enhancer", and click Uninstall.
//
// This script has been tested in Firefox V2.0.0.4 and Greasmonkey 0.7.20070607.0
//
// Version History
//
// 1.0	Initial release
//
// 1.1  Add auto redirect from http://bookmooch.com to 
//      http://www.bookmooch.com. Should avoid having to login 
//      on a wishlist email link.
//
// Features
// --------
//
// Adds a direct Charity link to a users Bio page
//
// --------------------------------------------------------------------
// ==UserScript==
// @name            BookMooch Enhancer
// @description     Add extra feeatures to the BookMooch website
// @namespace       http://www.boringbutgood.com/blog/archive/bookmooch/
// @include         http://*bookmooch.com/*
// ==/UserScript==

var ident = document.createElement("div");
ident.innerHTML = ' <font face="arial, sans-serif"><p align=center><img src="http://img.bookmooch.com/bookmooch/images/greydot.gif" width="600" height="1" alt=""></p><p class="tablehead" align=center>' + 
                  ' You are running <a href=' +
                  '"http://www.boringbutgood.com/blog/archive/bookmooch/">' +
                  'BookMooch Enhancer</a> :- Version 1.1</p>' +
                  '<p class="tabletext"' +
                  ' align=center>Like it..? <a href="mailto:robmonster@gmail.com">' +
                  'Mail me</a> with any comments or queries, or even ' + 
                  '<a href="http://www.bookmooch.com/m/charity_amount?name=robmonster">' + 
                  'Charity me a point</a></p>';
document.body.appendChild(ident, document.body.firstChild);


var href, pageAddr, user, start, end, tabletext, buttontitle, buttonhref, buttontitleupper, newtable, buttonbars, thisbar;

if (window.location.href.match(/^http:\/\/bookmooch/)){
     window.location.href = window.location.href.replace(/^http:\/\/bookmooch/, 'http:\/\/www.bookmooch');
                                          }
pageAddr = window.location.href;
if (pageAddr.match(/\/bio\//)){
// GM_log('On a BIO Page');

   start = pageAddr.search(/\/bio\//) + 5;
   user  = pageAddr.substr(start);
//   GM_log('Username is ' + user);

   newtable = document.createElement("td");
   newtable.bgcolor = "#6eb0b1"
   newtable.innerHTML = createbutton("charity to this user", "http://www.bookmooch.com/m/charity_amount?name=" +user);
//   GM_log("HTML= " + newtable.innerHTML);

buttonbars = document.evaluate(
    '/html/body/font/table[2]/tbody/tr[2]/td[2]/table[2]/tbody/tr/td[3]/table[1]/tbody/tr/td[3]',
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
for (var i = 0; i < buttonbars.snapshotLength; i++) {
       thisbar = buttonbars.snapshotItem(i)
       thisbar.parentNode.insertBefore(newtable, thisbar.nextSibling);
       }

                          }  // End of Bio page match



function createbutton(buttontitle, buttonhref) 

{
buttontitleupper = buttontitle.toUpperCase();

tabletext = '<table id="button" height="18" border="0" ' +
            'cellpadding="0" cellspacing="0"> <tr> ' + 
            '<td bgcolor="#6EB0B1"> <a href="' + buttonhref + '"  ' + 
            'title="' + buttontitle + '"><img border=0 bgcolor="#6EB0B1"' + 
            ' src="http://img.bookmooch.com/bookmooch/images/button_templa' + 
            'te_left.gif" width="5" height="18" alt=""></a></td> <td valign' +
            '="middle" height="18" bgcolor="#6EB0B1"><a  STYLE="text-decora' + 
            'tion:none" href="' + buttonhref + '" title="'+ buttontitle +
            '"><font face="Verdana, Arial, utopia, sans-serif" size=2 color' + 
            '="#FFFFFF">' + buttontitleupper + '></font></a></td> <td bgcol' + 
            'or="#6EB0B1"> <a  href="' + buttonhref + '" title="' + buttontitle + 
            '"><img border=0 bgcolor="#6EB0B1" src="http://img.bookmooch.co' + 
            'm/bookmooch/images/button_template_right.gif" width="6" height' + 
            '="18" alt=""></a></td> </tr> </table>';
return tabletext;
//GM_log(tabletext);
}

