// ==UserScript==
// @name          Saitama Prefectural Library Lookup from Amazon book listings.
// @namespace     http://www.amazon.co.jp
// @description   Saitama Prefectural Library Lookup from Amazon book listings.
// @include       http://*.amazon.*
// ==/UserScript==

// Version 20100307
libsearch(
  'https://www2.lib.city.saitama.jp/licsxp-opac/WOpacTifSchCmpdExecAction.do?hash=&returnid=&gamenid=tiles.WTifSchCmpd&chkflg=check&loccodschkflg=check&langcodschkflg=nocheck&condition1=0&condition1Text=&range1=0&mixing1=0&condition2=1&condition2Text=&range2=0&mixing2=0&condition3=6&condition3Text=',
  '&#x3055;&#x3044;&#x305F;&#x307e;&#x5e02;&#x56F3;&#x66F8;&#x9928;',
  'https://www2.lib.city.saitama.jp/licsxp-opac/WOpacMsgNewListToTifTilDetailAction.do?tilcod='
);

var isbn10 = '';
var isbn13 = '';

function libsearch( api, title, anchor ) {
  // get ISB10
  document.body.parentNode.innerHTML.match( /\s(4(\d{8}|-[\d-]{9}-)[\dX])/ );
  if ( RegExp.$1 != '' ) {
    isbn10 = RegExp.$1;
  }
  // get ISB13
  document.body.parentNode.innerHTML.match( /\s(978\-4(\d{8}|-[\d-]{9}-)[\dX])/ );
  if ( RegExp.$1 != '' ) {
    isbn13 = RegExp.$1;
  }

  // check library
  var header = document.getElementById( "priceBlock" );
  if ( header ) {
    checkLibrary( api, title, isbn10, header, anchor );
  }
}

//HttpRequest作成
function CreateHttpRequest() {
  //for Win+IE
  if (window.ActiveXObject) {
    try {
      //for MSXML2
      return new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
      try {
        //for Old MSXML
        return new ActiveXObject('Microsoft.XMLHTTP');
      } catch(e2) {
        return null;
      }
    }
  } else if (window.XMLHttpRequest) {
    //for Other browser
    return new XMLHttpRequest();
  }

  return null;
}

// isbn10, isbn13の2回しか繰り返さない
var count = 2;
//コールバック
function on_loaded( response, api, title, isbn, header, anchor ) {
  var result = RegExp.$1;
  response.responseText.match(/tilcod=(\d{13})/);
  if ( count > 0 ) {
    count = count - 1;
    if ( RegExp.$1 != '' && RegExp.$1 != result ) {
      makelink( api, title, isbn, header, anchor, RegExp.$1 );
    } else {
      if ( isbn13!='' ) {
        checkLibrary( api, title, isbn13, header, anchor );
      }
    }
  } else {
    makelink( api, title, header, anchor, "" );
    return 0;
  }

  return 1;
}

function checkLibrary( api, title, isbn, header, anchor ) {
  url = String(api + isbn);
  var http_ins = new CreateHttpRequest();
  if ( !http_ins ) {
    return 0;
  }

  var result = 0;
  http_ins.onreadystatechange = function() {
    if ( http_ins.readyState == 4 ) {
      result = on_loaded( http_ins, api, title, isbn, header, anchor );
    }
  }

  try {
    http_ins.open("GET", url, false);
    http_ins.send(null);
  } catch(e) {
  }

  return result;
}

function makelink( api, title, isbn, header, anchor, foungflg ) {
  var msg = ( foungflg ) ? '<b>&#x8535;&#x66f8;&#x3042;&#x308A;&#x3002;</b>' : '<b>&#x8535;&#x66F8;&#x4E0D;&#x660E;&#x3002;</b>';
  var spl_link = document.createElement( 'div' );
  spl_link.setAttribute( 'class', 'buying' );
  spl_link.innerHTML = '<span class=\"availGreen\" style=\"color:#ff0000;\">' + msg + '</span>&nbsp;';
  if ( foungflg ) {
    spl_link.innerHTML += '<a href=\"' + anchor + foungflg + '\" target=\"_blank\" title=\"' + title + '\">' + title + '</a>&#x3067;&#x8868;&#x793A;&nbsp;'
                        + '<a href=\"http://www.dokushogaku.com/\" target=\"_blank\" title=\"&#x3055;&#x3044;&#x305F;&#x307E;&#x5E02;&#x56F3;&#x66F8;&#x9928;&#x5F85;&#x3061;&#x4EBA;&#x6570\">'
                        + '<img src=\"http://www.dokushogaku.com/php/saitama/user.php?url=' + anchor + foungflg + '\" border=\"0\">'
                        + '</a></div>';
  } else {
    spl_link.innerHTML += '<a href=\"' + api + isbn10 + '\" target=\"_blank\" title=\"' + title + '\">' + title + '</a>&#x3067;&#x691C;&#x7D22;&#x3002;'
                        + '&#x7121;&#x3044;&#x306A;&#x3089;&#x3001;<a href=\"' + api + isbn13 + '\" target=\"_blank\" title=\"&#x3053;&#x3063;&#x3061;\">&#x3053;&#x3063;&#x3061;</a>&#x3002;</div>';
  }
  header.parentNode.insertBefore( spl_link, header.nextSibling );
}
