// ==UserScript==
// @name             WorldCatMark
// @namespace    http://www.longhighway.com/monkeyisland
// @description   Add link to bookmark WorldCat page in del.icio.us with non-shared tag "_booktoread"; add link to search for book on MnLINK (www.mnlink.org). 
// @include          http://www.worldcat.org/oclc/*
// @include          http://www.worldcat.org/title/*
// ==/UserScript==

var hook;
var isbn;
var site;
var title;

function tagIt(postUrl) {
  GM_xmlhttpRequest({
      method: 'GET',
      url: postUrl,
      onload: function(response) {
        var parser = new DOMParser();
        var doc = parser.parseFromString(response.responseText, "text/xml");
        elements = doc.getElementsByTagName("result");
        if ((elements.length == 0) || (elements[0].getAttribute("code") != "done")) {
          alert("Post to del.icio.us failed.");
        }
      }
  });
}

function createDelicioiusTagDiv() {
  var deliciousLink = document.createElement("a");
  deliciousLink.innerHTML = "del.icio.us";
  deliciousLink.setAttribute("title", "Tag with \"_booktoread\" at del.icio.us");

  var deliciousPostURL = "https://api.del.icio.us/v1/posts/add?&url=" + encodeURIComponent(window.location.href) + "&description=" + encodeURIComponent(document.title) + "&tags=_booktoread" + "&shared=no";
  deliciousLink.href = deliciousPostURL; 
  deliciousLink.setAttribute("style", "color:red");
  deliciousLink.addEventListener(
    "click", 
    function (event) { 
      tagIt(event.originalTarget.toString()); 
      this.style.color = "grey";
      event.stopPropagation();
      event.preventDefault();
    }, 
    false);

  var newDiv = document.createElement("div");
  //var comment = document.createElement("strong");
  //comment.innerHTML = "Bookmark: ";
  //newDiv.appendChild(comment);
  newDiv.appendChild(deliciousLink);
  return newDiv;
}

function createMnLinkTagDiv(isbn) {
  var mnLink = document.createElement("a"); 
  mnLink.setAttribute("href", "https://www.mnlinkgateway.org/zportal/zengine?VDXaction=ZSearchAdvanced&attr_type1_row1=7&search_term_row1=" + isbn + "&sourceid=Mozilla-search");
  mnLink.setAttribute("title", "Lookup title in MnLINK - may have to edit title in form by hand");
  mnLink.innerHTML = "MnLINK"
  mnLink.setAttribute("style", "color:red");
  var newDiv = document.createElement("div");
  newDiv.appendChild(mnLink);
  return newDiv;
}

//hook = document.evaluate("id('util-links')/ul/li[4]/a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
hook2= document.evaluate("id('borrow')/h2", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
hook = hook2;
oclc = document.evaluate("id('details-oclcno')/td", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var isbnMatch = document.evaluate("id('details-standardno')/td", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
isbn=isbnMatch.textContent

if (isbnMatch) {
    isbn=isbnMatch.textContent.split(' ')[0];
}
if (hook) {
  hook.appendChild(createDelicioiusTagDiv());
}

if (hook2 && isbn) {
  hook2.appendChild(createMnLinkTagDiv(isbn));
}



