// ==UserScript==
// @name          Amazon to Libraries Australia Link
// @namespace     http://www.nla.gov.au 
// @description	  Search the LA holdings from Amazon book listings.
// @include       http://*.amazon.*
// ==/UserScript==

// fixed for Firefox 1.5 and GM 0.6.4
// dervived from , derived from SPL Linky, http://www.mundell.org, KKF May06
(

function()
{
var NBDhost = "ll01.nla.gov.au"
var libraryUrlPattern = 'http://' + NBDhost + '/search.jsp?searchTerm=&xml=y&topic=isbn:'
var libraryUrlLookup = 'http://' +  NBDhost + '/show.jsp?rid='
var libraryTitleSearchUrl = 'http://' +  NBDhost + '/search.jsp?searchTerm='
var libraryName = 'Libraries Australia'
var libraryAvailability = /Topics ordered by occurrence/;
var foundResults = /found/

var libraryLookup = 
    {
    insertLink: function(rid, hrefTitle, aLabel, color, stitle )
        {
        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 );
	if (stitle != null) link.setAttribute('href', libraryTitleSearchUrl + title.replace(/\(.*\)/, "")) ;
        else link.setAttribute('href', libraryUrlLookup + rid);
        link.setAttribute('style','color: ' + color + ";font-weight:bold;font-size:130%;background:#ccffcc");


        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 (foundResults.test(page)) {
					var j = page.indexOf("item id='") ;
					var k = page.indexOf("'", j+9) ;
					var rid = page.substring(j+9, k) ;
                    libraryLookup.insertLink (
                        rid,
                        "$4.95 delivered to your door!",
                        "Borrow this book from Libraries Australia for 3 weeks.  Just $4.95, delivered to your door in 48 hours",
                        "green"
                        );
                    }

                else
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Not found",
                        "ISBN not found on " + libraryName +
				" - click to try a title search",
                        "orange",
			origTitle

                        );
                    }

            }
            });
        }


    }


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, origTitle);

}
)();
