// ==UserScript==
// @name          Boulder Public LibraryLookup
// @namespace     http://ucsub.colorado.edu/~thompsma
// @description   Check availability in Boulder Public Library on Amazon. Based on LibraryLookup by John Udell.
// @include       http://*.amazon.*
// ==/UserScript==

// This is based on Jon Udall's amazing script that I got
// from the GreaseMonkey wiki.

(

function()
{

var libraryUrlPattern = 'http://nell.boulder.lib.co.us/search/i='
var libraryName = 'Boulder';
var libraryAvailability = /SHELF/;
var libraryDueBack = /DUE (\d{2}\-\d{2}\-\d{2})/;
var libraryOrdered = /copy ordered/;
var notFound = /No matches found/

var libraryLookup = 
    {
    insertLink: function(isbn, hrefTitle, aLabel, color )
        {
        var div = origTitle.parentNode;
        var title = origTitle.firstChild.nodeValue;

        var newTitle = document.createElement('b');
        newTitle.setAttribute('class','sans');

        var titleText = document.createTextNode(title);
        newTitle.appendChild(titleText);
        
        var br = document.createElement('br');

        var link = document.createElement('a');
        link.setAttribute ( 'title', hrefTitle );
        link.setAttribute('href', libraryUrlPattern + isbn);
        link.setAttribute('style','color: ' + color);

        var label = document.createTextNode( aLabel );

        link.appendChild(label);

        div.insertBefore(newTitle, origTitle);
        div.insertBefore(br, origTitle);
        div.insertBefore(link, origTitle);
        div.removeChild(origTitle);
        },

    doLookup: function ( isbn )
        {
        GM_xmlhttpRequest
            ({
            method:'GET',
            url: libraryUrlPattern + isbn,
            onload:function(results)
                {
                page = results.responseText;
                if ( libraryAvailability.test(page) )
                    {
                    libraryLookup.insertLink (
                      isbn,
                      "On the shelf now!",
                      "Available in the " + libraryName + " Library!",
                      "green"
                       );
                    }
                else if ( libraryDueBack.test(page) )
                    {
                    var due = page.match(libraryDueBack)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "Due back " + due,
                        "Due back at the " + libraryName + " Library on " + due,
                        "#AA7700" // dark yellow
                        );
                    }
                else if ( libraryOrdered.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Copy on Order!",
                        "Copy is on order at the " + libraryName + " Library!",
                        "#B03060" // maroon-y
                        );
                    }
                }
            });
        }


    }

try 
    { var isbn = window.content.location.href.match(/\/(\d{7,9}[\d|X])\//)[1];  }
catch (e)
    { return; }

var origTitle = document.evaluate("//b[@class='sans']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

if ( ! origTitle )
  { return; }

libraryLookup.doLookup(isbn);

}
)();
