// ==UserScript==
// @name          Amazon UK Hud Linky
// @namespace     http:/www.daveyp.com/blog/
// @description	  Search the University of Huddersfield Catalogue from Amazon UK book listings, based on script from http://www.mundell.org
// @include       http://*.amazon.co.uk*
// ==/UserScript==

// fixed for Firefox 1.5 and GM 0.6.4
// fixed for changes to Amazon UK layout (10/Aug/2006) - cheers to Lorcan Dempsey for spotting there was a problem!

(

function()
{

    var libraryUrlPattern = 'http://161.112.232.203:4128/isbn/'
    var libraryName = 'the University of Huddersfield';
    var libraryAvailability = /Available\<\/a\>/;
    var libraryElectronic = /Internet access\:/;
    var libraryOtherEditions = /other editions available/;
    var libraryOnOrder = /On Order/;
    var libraryInProcess = /In Cataloguing/;
    var libraryDueBack = /(\d{2}\/\d{2}\/\d{4})/;
    var notFound = /no matches found for ISBN/;

    var libraryLookup = 
    {
        insertLink: function(isbn, hrefTitle, aLabel, color )
        {
            var div = origTitle.parentNode;
//            var title = origTitle.firstChild.nodeValue;
    	    var title = '';

            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','font-size:small; font-weight:bold; color:' + color);

            var label = document.createTextNode( aLabel );

            link.appendChild(label);

            div.insertBefore(newTitle, origTitle);
            div.insertBefore(link, origTitle);
            div.insertBefore(br, origTitle);
//            div.removeChild(origTitle);
        },

        doLookup: function ( isbn )
        {
            GM_xmlhttpRequest
            ({
                method:'GET',
                url: 'http://161.112.232.203:4128/checkisbn/' + isbn,
                onload:function(results)
                {
                    page = results.responseText;
                    if ( notFound.test(page) )
                    {
                        var due = page.match(notFound)[1]
                        libraryLookup.insertLink (
                            origTitle.firstChild.nodeValue,
                            "ISBN not found",
                            "ISBN not found at " + libraryName + " - click here to start title search",
                            "red"
                        );
                    }
                    else if ( libraryAvailability.test(page) )
                    {
                        libraryLookup.insertLink (
                            isbn,
                            "available now",
                            "Available at " + libraryName,
                            "green"
                        );
                    }
                    else if ( libraryElectronic.test(page) )
                    {
                        libraryLookup.insertLink (
                            isbn,
                            "available electronically",
                            "Available in electronic format from " + libraryName,
                            "green"
                        );
                    }
                    else if ( libraryOtherEditions.test(page) )
                    {
                        libraryLookup.insertLink (
                            isbn,
                            "other editions available",
                            "Other editions of this title are available at " + libraryName,
                            "green"
                        );
                    }
                    else if ( libraryOnOrder.test(page) )
                    {
                        libraryLookup.insertLink (
                            isbn,
                            "on order",
                            "On order at " + libraryName,
                            "#AA7700"  // dark yellow
                        );
                    }                    
                    else if ( libraryInProcess.test(page) )
                    {
                        libraryLookup.insertLink (
                            isbn,
                            "In process!",
                            "In process (available soon) at " + libraryName,
                            "#AA7700"  // dark yellow
                        );
                    }                    
                    else if ( libraryDueBack.test(page) )
                    {
                        var due = page.match(libraryDueBack)[1]
                        libraryLookup.insertLink (
                            isbn,
                            "due back " + due,
                            "Due back at " + libraryName + " on " + due,
                            "#AA7700"  // dark yellow
                        );
                    }
                    else
                    {
                        libraryLookup.insertLink (
                            isbn,
                            "Error",
                            "Error checking " + libraryName,
                            "orange"
                        );
                    }
                }
            });
        }


    }

    try 
    { var isbn = window.content.location.href.match(/\/(\d{7,9}[\d|X|x])\//)[1]; }
     

    catch (e)
    { return; }

    var origTitle = document.evaluate("//b[@class='sans']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

    if ( ! origTitle )
    { return; }

//     alert( isbn );
//     alert( origTitle.firstChild.nodeValue );

    libraryLookup.doLookup(isbn);

    }
)();