// ==UserScript==
// @name          Google Books SCLS Linky
// @namespace     http://beachy.freeshell.org/greasemonkey/
// @description	  Search the South Central Library System Catalog from Google Books listings.
// @include       http://books.google.*
// ==/UserScript==

// adapted from Ann Arbor District Library Google Books Linky
// by Edward Vielmetti, found at http://www.superpatron.com
(

function()
{

var libraryUrlPattern = 'http://www.linkcat.info/ipac20/ipac.jsp?index=CISBN&term='
var libraryName = 'South Central Library System';
var libraryAvailability = /In Library/;
var libraryOnOrder = /On Order/;
var libraryInProcess = /In Process/;
var libraryDueBack = /(\d{2} \w{3} \d{2})/;
var libraryLost = /Lost/
var libraryMissing = /Missing/
var libraryStorage = /Storage/
var libraryTrace = /Trace/
var libraryNet = /E RESOURCE/
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', 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 ( notFound.test(page) )
                    {
                    var due = page.match(notFound)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "Not carried",
                        "Not in the " + libraryName,
                        "red"
                        );
                    }
                else if ( libraryAvailability.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "On the shelf now!",
                        "Available in the " + libraryName + "!",
                        "green"
                        );
                    }
                else if ( libraryOnOrder.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "On order!",
                        "On order for the " + libraryName + "!",
                        "#AA7700"  // dark yellow
                        );
                    }                    
                else if ( libraryInProcess.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "In process!",
                        "In process (available soon) in the " + libraryName + "!",
                        "#AA7700"  // dark yellow
                        );
                    }                    
                else if ( libraryDueBack.test(page) )
                    {
                    var due = page.match(libraryDueBack)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "Due back " + due,
                        "Due back in the " + libraryName + " on " + due,
                        "#AA7700"  // dark yellow
                        );
                    }
                else if ( libraryStorage.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "In storage",
                        "In storage at the " + libraryName + ".",
                        "#AA7700"  // dark yellow
                        );
                    }
                else if ( libraryTrace.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Under trace",
                        "Under a trace at the " + libraryName + ".",
                        "#AA7700"  // dark yellow
                        );
                    }
                else if ( libraryMissing.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Missing!",
                        "Missing from the " + libraryName + "!",
                        "red"
                        );
                    }
                else if ( libraryLost.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Lost!",
                        "Lost from the " + libraryName + "!",
                        "red"
                        );
                    }
                else if ( libraryNet.test(page) )
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "E-resource",
                        "E-resource available at " + libraryName + "!",
                        "red"
                        );
                    }
                else
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Error",
                        "Error checking the " + libraryName,
                        "orange"
                        );
                    }
                }
            });
        }


    }

try 
    { var isbn = document.body.textContent.match(/\/(\d{7,9}[\d|X])\//)[1];  }
catch (e)
    { return; }

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

if ( ! origTitle )
  { return; }

libraryLookup.doLookup(isbn);

}
)();
