// ==UserScript==
// @name          Amazon-Hennepin County Library Lookup
// @namespace     http://www.mundell.org
// @description	  Search the Hennepin County Library Catalog from Amazon book listings.
// @include       http://*.amazon.*
// ==/UserScript==

// revision history:
// Version 1.2 - Status now displayed when all copies being held. (3/22/07)
// Version 1.2.1 -Added Transit Request status. (3/22/07)
// Version 1.2.2 - Edited var origTitle to match change in Amazon CSS (4/14/08)
// Version 1.2.3 - Edited new index pallti to utl (9/1/2009)
// Thanks to Dale Brayden for his assistance
(

function()
{

var libraryUrlPattern = 'http://catalog.hclib.org/ipac20/ipac.jsp?index=ISBN&term='
var libraryURLPatternForLink = 'http://hzapps.hclib.org/pub/ipac/link2ipac.cfm?LinkyVersion=1.2.3&index=ISBN&term='
var libraryURLPatternForNoMatch = 'http://hzapps.hclib.org/pub/ipac/link2ipac.cfm?LinkyVersion=1.2.3&index=UTL&term='
var libraryName = 'Hennepin County';
var libraryAvailability = /Checked In/;
var libraryOnOrder = /(\d+) Copies On Order/;
var libraryInProcess = /Pending/; 
var libraryTransitRequest = /Transit Request/;
var libraryBeingHeld = /Being held/; 
var libraryHolds = /Current Requests: (\d+)/;
var libraryCopies = /Reservable copies: (\d+)/;
var libraryDueBack = /(\d{2}\/\d{2}\/\d{4})/;
var notFound = /Sorry, could not find anything matching/

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', libraryURLPatternForLink + isbn);
        link.setAttribute('style','color: ' + color + '\;' + 'background-color:#FFFF99\;' + 'font-size:12px');

        var label = document.createTextNode( aLabel );

        link.appendChild(label);

        div.insertBefore(newTitle, origTitle);
        div.insertBefore(br, origTitle);
        div.insertBefore(link, origTitle);
        div.removeChild(origTitle);
        },

    insertNoMatchLink: function(TheTitle, 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', libraryURLPatternForNoMatch + TheTitle);
        link.setAttribute('style','color: ' + color + '\;' + 'background-color:#FFFF99\;' + 'font-size:12px');

        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 ( notFound.test(page) )
                    {
                    var due = page.match(notFound)[1]
                    libraryLookup.insertNoMatchLink (
                        TheTitle,
                        "Check for other editions",
                        "This edition not in " + libraryName + " Library. Click to check for other editions.",
                        "red"
                        );
                    }
				else if ( libraryAvailability.test(page) )
                    {
					var copies = page.match(libraryCopies)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "On the shelf now!",
                        "Available in " + libraryName + " Library!  (Library owns " + copies + " copies)",
                        "green"
                        );
                    }
                else if ( libraryOnOrder.test(page) )
                    {
					var CopiesOnOrder = page.match(libraryOnOrder)[1]
					var holds = page.match(libraryHolds)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "On order!",
                        "Request from " + libraryName + " Library (" + CopiesOnOrder + " copies on order, " + holds + " requests)",
                        "#AA7700"  // dark yellow
                        );
                    }                    
                else if ( libraryInProcess.test(page)  || libraryTransitRequest.test(page) )
                    {
					var copies = page.match(libraryCopies)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "In process!",
                        "Available soon at " + libraryName + " Library! (" + copies + " copies pending)" ,
                        "#AA7700"  // dark yellow
                        );
                    }
                else if ( libraryBeingHeld.test(page) )
                    {
	    var holds = page.match(libraryHolds)[1]
                    var copies = page.match(libraryCopies)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "All copies on hold shelf",
                        "Request from " + libraryName + " Library (currently " + holds + " requests on " + copies + " copies)",
                        "#AA7700"  // dark yellow
                        );
                    }                    
                 else if ( libraryHolds.test(page)  )
                    {
                    var holds = page.match(libraryHolds)[1]
                    var copies = page.match(libraryCopies)[1]
                    var due = page.match(libraryDueBack)[1]
                    libraryLookup.insertLink (
                        isbn,
                        holds + " Holds",
                        "Request from " + libraryName + " Library (currently " + holds + " requests on " + copies + " copies)",
                        "#AA7700"   //dark yellow
                        );
                    }
	else if ( libraryDueBack.test(page) )
                    {
                    var due = page.match(libraryDueBack)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "Due back " + due,
                        "Due back at " + libraryName + " Library on " + due,
                        "#AA7700"  // dark yellow
                        );
                    }
                else
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Error",
                        "Error checking " + libraryName + " Library",
                        "orange"
                        );
                    }
                }
            });
        }


    }

try 
    { var isbn = window.content.location.href.match(/\/(\d{7,9}[\d|X])\//)[1];  }
catch (e)
    { return; }

var origTitle = document.evaluate("//span[@id='btAsinTitle']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
var TheTitle = origTitle.textContent ;

if ( ! origTitle )
  { return; }

libraryLookup.doLookup(isbn);

}
)();