/*
 Bookburro Ajax Panel for GreaseMonkey

 Version 0.13
 (C) 2005 Jesse Andrews, Britt Selvitelle under cc-by-sa
 License: Creative Commons "Attribution-ShareAlike 1.0"
 http://creativecommons.org/licenses/by-sa/1.0/

 Snipits used from RSS Reader for GreaseMonkey 
 http://www.xs4all.nl/~jlpoutre/BoT/Javascript/RSSpanel/
 Version: 1.03 (C) 2005 Johannes la Poutre
 THANKS!

 Changelog:
  *  2005-09-19  *

 0.13 - GreaseMonkey 0.6 compatibility fix to get working onclick handlers
      - Added ajax fetching from Powell's Books, figuring they want customers
      - Added adlibris.se, akademibokhandeln.se, bokus.com and internetbokhandeln.se
      - For kicks, added availability listing at Linköping University library
      / Johan Sundström, oyasumi+bookburro@gmail.com

  *  2005-06-24  *

 0.12 - Added support for alldirect.com

  *  2005-04-25  *

 0.11 - improved skin & add link to message if no results
 0.10 - bug fix - don't show iframes!
 0.09 - bug fix - alert were out of scope, plus a couple visual tweaks
 0.08 - bug - use (function(){ code })(); so you don't kill other javascript
 0.07 - use AWS to grab amazon's prices

  *  2005-04-24  *
 0.06 - wasn't checking for ISBN= in the url as well!
 0.05 - added Amazon marketplace
 0.04 - worked on skin to make it PURTY
 0.03 - use AJAX to add the prices 
 0.02 - Improved interface
 0.01 - initial releasea

*/

// ==UserScript==
// @name          Book Burro - Remixing the bookstore
// @namespace     http://overstimulate.com/userscripts/
// @description   Find the cheapest books
// @include       http://amazon.com*
// @include       http://www.amazon.com*
// @include       http://www.powells.com*
// @include       http://half.ebay.com*
// @include       http://buy.com*
// @include       http://www.buy.com*
// @include       http://search.barnesandnoble.com*
// @include       http://barnesandnoble.com*
// @include       http://www.barnesandnoble.com*
// @include       http://www.alldirect.com*
// @include       http://www.bokus.com/*
// @include       http://www.internetbokhandeln.se/*
// @include       http://www.akademibokhandeln.se/*
// @include       http://akademibokhandeln.se/*
// @include       http://www.adlibris.se/*
// @include       http://*.bibl.liu.se/*
// @exclude       
// ==/UserScript==

(function() {

function checkISBN( isbn ) {
  try {
    isbn=isbn.toLowerCase().replace(/-/g,'').replace(/ /g,'');
    if (isbn.length != 10) return false;
    var checksum = 0;
    for (var i=0; i<9; i++) {
      if (isbn[i] == 'x') {
        checksum += 10 * (i+1);
      } else {
        checksum += isbn[i] * (i+1);
      }
    }
    checksum = checksum % 11;
    if (checksum == 10) checksum = 'x';
    if (isbn[9] == checksum)
      return isbn;
    else
      return false;
  } catch (e) { return false; }
}

function dom_createLink(url, txt, title) {
  var a  = document.createElement("a");
  a.setAttribute("href", url);
  a.setAttribute("style", "color: #00a; text-decoration: none; font-weight: bold");
  if (title) a.setAttribute("title", title);
  a.appendChild(document.createTextNode(txt));
  return a;
}

function add_site(url, title, loc_id ) {
  var a  = dom_createLink( url, title, title + ' Search'  );
  var b  = document.createElement("b");
  b.innerHTML = 'fetching';
  b.setAttribute("id", loc_id);

  var tr = document.createElement("tr");
  var td_left = document.createElement("td");
  var td_right = document.createElement("td");
  td_left.appendChild(a);
  td_right.appendChild(b);
  td_right.setAttribute("align", "right");
  tr.appendChild(td_left);
  tr.appendChild(td_right);
  return tr;
}

function str2xml(strXML) {
        
    //create a DOMParser
    var objDOMParser = new DOMParser();
        
    //create new document from string
    var objDoc = objDOMParser.parseFromString(strXML, "text/xml");

    return objDoc; 
} 

function int2money( cents )  {
  var money = "$"
  if (cents< 100) {
    money = money + '0.';
  } else {
    money = money + Math.floor(cents/100) + '.';
  }
  cents = cents % 100;
  if (cents < 10)
    money = money + '0';
  money = money + cents;
  return money;
}

var amazon_associate_code = 'diaryohayou-20' // PUT YOUR AMAZON ASSOCIATE CODE HERE !!!!
var amazon_dev_key = '0XYJJ825QSB9Q7F2XN02'; // PUT YOUR AMAZON DEV KEY HERE !!!
var bn_associate_code = '41456445'; /// PUT YOUR BN AFFILIATE CODE HERE!!!
var half_associate_code = '1698206-1932276'; /// PUT YOUR HALF AFFILIATE CODE HERE!!!

function run_queries(isbn) {

  var errmsg = 'Either there are no books available,\\nor there is a parsing error because of\\nsome change to their website.';

  ////// AJAX for BN.com /////

  GM_xmlhttpRequest({ method:"POST", url:'http://search.barnesandnoble.com/booksearch/isbninquiry.asp?isbn='+isbn, data:"",
    onload:function(result) {
      try {
        document.getElementById('burro_bn').innerHTML = result.responseText.match('priceRightBNPrice[^>]*>\([^<]*\)</')[1];
      } catch (e) { document.getElementById('burro_bn').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for Buy.com /////

  GM_xmlhttpRequest({ method:"POST", url:'http://www.buy.com/retail/GlobalSearchAction.asp?qu='+isbn, data:"",
    onload:function(result) {
      try {
        document.getElementById('burro_buy').innerHTML = result.responseText.match('productPrice[^>]*>\([^<]*\)</')[1]; 
      } catch (e) { document.getElementById('burro_buy').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for half.com /////
  
  GM_xmlhttpRequest({ method:"POST", url:'http://half.ebay.com/search/search.jsp?product=books:isbn&query='+isbn, data:"",
    onload:function(result) {
      try {
        document.getElementById('burro_half').innerHTML = result.responseText.match('Best[^P]*Price[^\$]*\([^<]*\)<')[1];
      } catch (e) { document.getElementById('burro_half').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for alldirect.com /////
  
  GM_xmlhttpRequest({ method:"POST", url:'http://www.alldirect.com/book.asp?&isbn='+isbn, data:"",
    onload:function(result) {
      try {
        document.getElementById('burro_alldirect').innerHTML = result.responseText.match('All Direct\x27s[^P]*Price[^\$]*\([^<]*\)<')[1];
      } catch (e) { document.getElementById('burro_alldirect').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for both amazon.com /////

  GM_xmlhttpRequest({ method:"POST", url:'http://xml.amazon.com/onca/xml3?t=' + amazon_associate_code + '&dev-t=' + amazon_dev_key + '&type=lite&f=xml&mode=books&AsinSearch='+isbn, data:"",
    onload:function(result) {
      var x = str2xml( result.responseText );
      var ourprices = x.getElementsByTagName('OurPrice');
      if (ourprices.length == 0) {
        document.getElementById('burro_amazon').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>';
      } else {
        document.getElementById('burro_amazon').innerHTML = ourprices[0].childNodes[0].nodeValue;
      }
      var usedprices = x.getElementsByTagName('UsedPrice');
      if (usedprices.length == 0) {
        document.getElementById('burro_amazonmarket').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>';
      } else {
        document.getElementById('burro_amazonmarket').innerHTML = usedprices[0].childNodes[0].nodeValue;
      }
    }
  });

  ////// AJAX for powells.com /////

  GM_xmlhttpRequest({ method:"GET", url:'http://www.powells.com/cgi-bin/biblio?isbn='+isbn,
    onload:function(result) {
      try {
        document.getElementById('burro_powell').innerHTML = result.responseText.match('<div class="price">([^<]*)<')[1];
      } catch (e) { document.getElementById('burro_powell').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for bokus.com /////

  GM_xmlhttpRequest({ method:"GET", url:'http://www.bokus.com/cgi-bin/book_search.cgi?FAST_VALUE=ISBN&FAST='+isbn,
    onload:function(result) {
      try {
        document.getElementById('burro_bokus').innerHTML = result.responseText.match('<span class="price">([^<]*)<')[1].replace(/:-/,'&nbsp;SEK');
      } catch (e) { document.getElementById('burro_bokus').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for internetbokhandeln.se /////

  GM_xmlhttpRequest({ method:"GET", url:'http://www.internetbokhandeln.se/_114K3FPUVX/msearchres.html?s_sortby=b&s_media=ALL&s_type=i&s_search='+isbn,
    onload:function(result) {
      try {
        document.getElementById('burro_ibse').innerHTML = result.responseText.match('<span class=price>([^<]*)<')[1].replace(/:-/,'&nbsp;SEK');
      } catch (e) { document.getElementById('burro_ibse').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for akademibokhandeln.se /////

  GM_xmlhttpRequest({ method:"GET", url:'http://www.akademibokhandeln.se/db/caweb/sok.avanc_artiklar?cisbn='+isbn,
    onload:function(result) {
      try {
        document.getElementById('burro_abse').innerHTML = result.responseText.match('<span class="pris">([^<]*)<')[1].replace(/\s/g,'').replace(/kr/,'&nbsp;SEK');
      } catch (e) { document.getElementById('burro_abse').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for adlibris.se /////

  GM_xmlhttpRequest({ method:"GET", url:'http://www.adlibris.se/shop/product.asp?isbn='+isbn,
    onload:function(result) {
      try {
	var html = result.responseText;
	if( html.match( 'Ingen titel med detta ISBN finns hos AdLibris.' ) )
	  fail();
        document.getElementById('burro_alse').innerHTML = html.match('<span class="price">([^<]*)<')[1].replace(/:-/,'&nbsp;SEK');
      } catch (e) { document.getElementById('burro_alse').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>'; }
    }
  });

  ////// AJAX for bibl.liu.se /////

  GM_xmlhttpRequest({ method:"GET", url:'http://hip.bibl.liu.se/ipac20/ipac.jsp?menu=search&index=ISBN&term='+isbn,
    onload:function(result) {
      var book, inne = 0, ute = 0, html = result.responseText;
      while( book = /<tr height="15">(.*?)<\/tr>/gi.exec( html ) )
	if( !book[1].match( /referens/i ) )
	  if( book[1].match( /tillg|reserverad/i ) )
	    inne++;
	  else
	    ute++;
      if( inne+ute )
      {
	html = inne;
	if( ute )
	  html += '&nbsp;(of&nbsp;'+(inne+ute)+')';
	else
	  html += '&nbsp;in';
	document.getElementById('burro_liubibl').innerHTML = html;
      }
      else
	document.getElementById('burro_liubibl').parentNode.innerHTML = '<a href="javascript: alert(\''+errmsg+'\');">none</a>';
    }
  });

}


function burro( location, isbn ) {
  var box = document.createElement("div");
  box.setAttribute("title","Click box to expand/collapse");
  box.setAttribute("style","position:fixed;z-index:99;top:15px;left:15px;background-color:#ffc;border:1px solid orange;padding:4px;text-align:left;opacity:.85;font:8pt sans-serif;overflow:hidden;width:220px;height:15px;margin-bottom:15px;");
  var carrot = document.createElement("img");
  carrot.setAttribute("style", "top:-10px");
  carrot.setAttribute("src", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYCR020Q08hgAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAiklEQVQY07XPIQoCURSF4e8NAzYxGicNuAa1WlyCO3AlZnfiNgwahQFxikkcBIsGfaZpzgODJ/4c/nMvPyR8g7EsephgH6q6aXnWIelhjkUsi0EL88TqFUfMYlnscMoS5wUccMYS4yxhfuGNPho88oQ5xxQjrHHpKkcMccMqVPU99eATG2zb4n/zAS4OHrV1hIB/AAAAAElFTkSuQmCC");

  
  //carrot.setAttribute("src", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYCRoeq/kCuwAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAmElEQVQY083QMUoDARSE4W92U2xhkxSpAgY0AXMQ29zCM3iSXEXQTrCws44Im85CkO1jnq2KKQWnGxiY+Yd/oXw1tTrr7D86CYdD5Xk3HA8vTi8la7xW1T5Jg8IEN6PvPXnEAkOa5l5Viwuc4yk/d9VyfoLrqnpIMmCGu2z79/wGUsv5FFcYY5Nt/wKjI+BvuEWnbfu///kTargo75QVC5oAAAAASUVORK5CYII=");

  
  
  //iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYBgYTOGNkZgAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAiklEQVQYV83PIQoCURSF4e+JME2MxkkDrkGtFpfgDlyJ2Z24DYNGYUCcYpIZBIsGfQadNq974Jafn8s5/EVCF4xFnmGKQyirpuW9LhkZFljGIh+2sJ+QocYJ81jke5xTn8PvjrhghUlKhhfeGKDBI1Uj+FacYYwNrik5YoQb1qGs7qQHPrHFrhXhA2IhHrUQNp+6AAAAAElFTkSuQmCC");
  carrot.setAttribute("id", "hide_show_carrot");
  box.appendChild( carrot );
  var title_image = document.createElement("img");
  title_image.setAttribute("style", "padding-left:6px");
  title_image.setAttribute("src", "data:image/gif;base64,R0lGODlheAAOAOYAAAAAAOmUUUOGfx1eVj8/M1ijnqBhLrmFVv//zNi6oCUIAO/vwJqEbGk4EL+/mm9vWZmZmVpaWvfAl7ydiC8vJo+PcwAPCaJtSylNS////4tCC9/fs0MeAa+vjefy9LR1QpnMzH9/Zny3s7WIazttbG5MMVyVj09PQJ+fgMvO11UwEwMWGq10R5K6ugg2Nh8fGdiecOaxhdiKTAAACZlqPnWtqu/dxTxlYXNeS1o6IU8rEZqytlp2d4p4bDZ/dn+Eh4pWL2JNOFKLhf/frtusjtGFS750N8ulfo1qVM/Ppm2HhsSQa615UvSlYy5HS9rb1hAEDqdvPjgpJg8PDP/vx4S2s6toMzRERKmlmrS8vkFfXwAhHnI7Ek06K+3x8D92c6/M0DNmZk5nb4xNGr+8s9uNT8x9OysRAtSniJWOlq6GYwAICZGtsP+4dGVCLkaVj4GpqF9fTMuLW8q/ra1rOV1UTXpKJmaZmb+SabGzstWXZEtMTmg9G4RZN4rFwVZeYSH5BAUUAAgALAAAAAB4AA4AAAf/gAiCg4MzM4SIgyo5hGcKj1JTiZOUlZaXmJmagmtrPEprlRN9TA0ICjpIaiMjSEhdm7Gys7SInXd3JiQ3oZN8Bx9jXGpoaEtLFzRumkkEKLTNz4nRtbWGbHBVJttCJBaTDQYaYzASSzAHQCxEmw4AIbTu8Iny1bMzWTtwcH4Fbz4DBHhLZGfMmCJy5NCwY0qWuwdxCHQYVIHAiYmCkkR8sABBvQ4hkgx6GBFjCGknPYYkUCGEgwcnNix4QCCOyEsrVoBpUUOEvwIAw4T5RkjFmA8faPDhU8sdgAcUADhA8ODpCQATN0yhEGKrx3coAMQh5BSqVAQACAhKiyAEgCnv/94CAICAwpQQdjdcmiFmRQuf/nwICDMAA4ZeCLrouBClMR8VJUrMcjd2A4CYlwVNeYEgDgCRFQCgcHdVLVmxCCyfQGuardvVaD8jCFsBQRLUllY40eICRA0BwL8I7ZRhUA8VNAx8sIOGSoISy2LVYz2dAF3rguQ5xT3y3Vq1bFm3PSt+/NTyliyIwbAFhB+BJIinKC5oAh8WBqzAoEKFA5ISONTxRwqZPITAApfdNhYCL0hylV60uUNBVedl99SBmYXnGnnhhfaMapmsgYEY7bWwxRoZZJBCGlAMMgEQdMhghR5DxKAADWokYMMTWeyBiTtTdODZM3Z1EBo8HYjlgL9dC8izwBSmWRjkkAhMMeWG54WnFQUOeFbhJWtoocWJHqj4gyGE6MBCGQHIUIQZetBwAB5qBEGGF3n8KNYLFx541RQPDIICnxSIVE9Y0nwVB5+BzgbXCRSo5VaWUSYR1QuJhniFBRlkAQEAiA1yBgtFtNGEEUbQYUAUeKDBABYZPBGBPbTWWs1cP4BKyRk06NFEAKdawUIUTBBhgxfI+mjrssxiMpclXdhBRxlmmJEUDTQckcAcXnjwRKPNhltrIAA7");
  box.appendChild( title_image );

  var close = document.createElement("img");
  close.setAttribute("src", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAuJAAALiQE3ycutAAAAB3RJTUUH1QQYCDcSg6d+SAAAAPBJREFUKM+Fkr1qAkEURs9dnWBrJT5IHkEw4gtEsBQs/AlpkiKlJGnEJlqIjRbrPoAQYhPio1hGsPAHFoW5KSIxo7J7qvlgDty534j6Rolgt987OQnA7XuEsTuegwIeMYiIkx2hVnsjCL7+su9/0mz2Lox0oNOpUiw+kc2mUVWGww8mkxYiYK09F4xJMho9kMs9IiJMpy8Y83vFWkUTCVcAWCxWLJcrRIT1OiSTOczuCXieK2y3IeXyK4PBPZtNSKn0zGzWJpW6uvyGer1LpVIgn78GYD7/ptHo0e/fHbemvtHIHv4zvonv4ayXuK9xyg8qt0tfe9qKPAAAAABJRU5ErkJggg==");
  close.setAttribute("style", "position:absolute;left:210px;top:3px;margin:2px;width:12px;height:12px;background-color:#ffb;border:none;line-height:8px;text-align:center;");
  close.setAttribute("title","Click To Remove");

  close.addEventListener( 'click', function() { this.parentNode.style.display = "none"; }, true);
  box.appendChild(close);

  var about = document.createElement("a");
  var about_img = document.createElement("img");
  about_img.setAttribute("border", "0");
  about_img.setAttribute("src", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAuJAAALiQE3ycutAAAAB3RJTUUH1QQYCDkprC+64gAAAOdJREFUKM+FkrFOAkEURc9ddrJZCgsqCwtCIiUFJa2df2CwNXRS8AmGisLEaGGs3VhTYDUtxZaED/AriGHjPhuEDC5wq3l5czL3vjeyzBlHtC6KoI4BuPk8Qqx3549rov3+YPBIp3NHq3VLlnkkBf0AWK2+6fevWCze8H7CaPRKUfzgnKsG0jSh272kLEuWyy/a7QvSNKnIsJEk6vWE2SxnPH5nOn3YWopjh9VqIbDBGA5fyPNnGo2znZVIEEWhJTNDgmbzfHvZzA6H/pP3k3/TqQQkIYle7z6oT74wnz8d3KNOfY19/QKFiTrWqbiPtAAAAABJRU5ErkJggg==");
  about.appendChild(about_img);
  about.setAttribute("style", "position:absolute;left:195px;top:3px;margin:2px;width:12px;height:12px;background-color:#ffb;border:none;line-height:12px;text-align:center;text-decoration:none;");
  about.setAttribute("title","OverStimulate");
  about.setAttribute("href","http://overstimulate.com/articles/2005/04/24/greasemonkey-book-burro-find-cheap-books" );
  box.appendChild(about);

  var table = document.createElement("table");
  table.setAttribute("style", "padding:0 5px;width:100%;font:10pt sans-serif;");
  table.appendChild( add_site( 'http://www.adlibris.se/shop/product.asp?isbn=' + isbn, 'Ad Libris', 'burro_alse' ));
  table.appendChild( add_site( 'http://www.akademibokhandeln.se/db/caweb/sok.avanc_artiklar?cisbn=' + isbn, 'Akademibokhandeln', 'burro_abse' ));
  table.appendChild( add_site("http://www.alldirect.com/book.asp?&isbn=" + isbn, "AllDirect.com", "burro_alldirect"));
  table.appendChild( add_site("http://www.amazon.com/exec/obidos/ASIN/" + isbn + "/" + amazon_associate_code, "Amazon", "burro_amazon" ));
  table.appendChild( add_site("http://www.amazon.com/exec/obidos/redirect?tag="+amazon_associate_code+"&path=tg/stores/offering/list/-/"+isbn+"/all/", "Amazon (used)", "burro_amazonmarket" ));
  table.appendChild( add_site( 'http://service.bfast.com/bfast/click?bfmid=2181&sourceid=' + bn_associate_code +'&bfpid=' + isbn + '&bfmtype=book', "Barnes & Noble", "burro_bn"));
  table.appendChild( add_site( 'http://www.bokus.com/b/' + isbn + '.html', "Bokus", "burro_bokus"));
  table.appendChild( add_site("http://www.buy.com/retail/GlobalSearchAction.asp?qu=" + isbn, "Buy.com", "burro_buy"));
  table.appendChild( add_site( 'http://www.tkqlhce.com/click-'+half_associate_code+'?ISBN=' + isbn, 'Half.com', 'burro_half' ));
  table.appendChild( add_site( 'http://www.internetbokhandeln.se/_114K3FPUVX/msearchres.html?s_sortby=b&s_media=ALL&s_type=i&s_search=' + isbn, 'Internetbokhandeln', 'burro_ibse' ));
  table.appendChild( add_site( 'http://hip.bibl.liu.se/ipac20/ipac.jsp?menu=search&index=ISBN&term=' + isbn, 'LiU Library', 'burro_liubibl' ));
  table.appendChild( add_site("http://www.powells.com/cgi-bin/biblio?isbn=" + isbn, "Powell's Books", "burro_powell"));

  box.appendChild(table);

  box.addEventListener( 'click', function() { 
    var carrot = document.getElementById('hide_show_carrot');
    if (this.style.height != "auto") {
      if (this.style.height == "15px") run_queries( isbn ); // height is 15 only first time you expand, thus only 1 query
      this.style.height = "auto";
      //carrot["src"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYBhc2IL6TMQAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAl0lEQVQYV93OMUoDARSE4W92Lbaw0cJKUDAR9CC23sIzeBKvEtBOsLBLvUFYOwtBto95aRQTQi7gdDP8Mwz/XNk0dX3RWX53ElaryuJ93A9Pz24kt/ioqmWSBoVjzA42YckrphjTNM+qWlxhgvnWMtTl+SHuq+olyYhTPKUfvnbgn8IJ7nCEh/TDG2zf+NMnHtFp2+E3XAMGOijvdRA+cwAAAABJRU5ErkJggg==";
      carrot["src"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYCRoeq/kCuwAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAmElEQVQY083QMUoDARSE4W92U2xhkxSpAgY0AXMQ29zCM3iSXEXQTrCws44Im85CkO1jnq2KKQWnGxiY+Yd/oXw1tTrr7D86CYdD5Xk3HA8vTi8la7xW1T5Jg8IEN6PvPXnEAkOa5l5Viwuc4yk/d9VyfoLrqnpIMmCGu2z79/wGUsv5FFcYY5Nt/wKjI+BvuEWnbfu///kTargo75QVC5oAAAAASUVORK5CYII=";
    } else {
      carrot["src"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYCRoeq/kCuwAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAmElEQVQY083QMUoDARSE4W92U2xhkxSpAgY0AXMQ29zCM3iSXEXQTrCws44Im85CkO1jnq2KKQWnGxiY+Yd/oXw1tTrr7D86CYdD5Xk3HA8vTi8la7xW1T5Jg8IEN6PvPXnEAkOa5l5Viwuc4yk/d9VyfoLrqnpIMmCGu2z79/wGUsv5FFcYY5Nt/wKjI+BvuEWnbfu///kTargo75QVC5oAAAAASUVORK5CYII=";

      //carrot["src"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYBgYTOGNkZgAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAiklEQVQYV83PIQoCURSF4e+JME2MxkkDrkGtFpfgDlyJ2Z24DYNGYUCcYpIZBIsGfQadNq974Jafn8s5/EVCF4xFnmGKQyirpuW9LhkZFljGIh+2sJ+QocYJ81jke5xTn8PvjrhghUlKhhfeGKDBI1Uj+FacYYwNrik5YoQb1qGs7qQHPrHFrhXhA2IhHrUQNp+6AAAAAElFTkSuQmCC";

      carrot["src"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQYCR020Q08hgAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAiklEQVQY07XPIQoCURSF4e8NAzYxGicNuAa1WlyCO3AlZnfiNgwahQFxikkcBIsGfaZpzgODJ/4c/nMvPyR8g7EsephgH6q6aXnWIelhjkUsi0EL88TqFUfMYlnscMoS5wUccMYS4yxhfuGNPho88oQ5xxQjrHHpKkcMccMqVPU99eATG2zb4n/zAS4OHrV1hIB/AAAAAElFTkSuQmCC";
      this.style.height = "14px";
    }
  }, true);
  document.getElementsByTagName("body")[0].appendChild(box);
}

if (document.location.hostname.match('amazon.com') && !document.location.href.match('rate-this')) {
  isbn = checkISBN(document.location.href.match(/\/([0-9X]{10})(\/|\?|$)/)[1]);
  if (isbn) burro( 'amazon', isbn );
}

if (document.location.hostname.match('barnesandnoble.com')) {
  isbn = checkISBN( document.location.href.match(/[iI][sS][Bb][Nn]=([0-9X]{10})(\&|\?|$)/)[1] );
  if (isbn) burro( 'bn', isbn );
}

if (document.location.hostname.match('bokus.com')) {
  isbn = checkISBN( document.location.href.match(/\/([0-9X]{10})\.html(\?|$)/)[1] );
  if (isbn) burro( 'bokus', isbn );
}

if (document.location.hostname.match('internetbokhandeln.se')) {
  isbn = checkISBN( document.location.href.match(/\/bok([0-9X]{10})\.html(\?|$)/)[1] );
  if (isbn) burro( 'ibse', isbn );
}

if (document.location.hostname.match('adlibris.se')) {
  isbn = checkISBN( document.location.search.match(/[?&]isbn=([0-9X]{10})(&|$)/i)[1] );
  if (isbn) burro( 'alse', isbn );
}

if (document.location.hostname.match('akademibokhandeln.se')) {
  var tds = document.getElementsByTagName( 'td' ), i;
  for( i=0; i<tds.length-1; i++ )
    if( (tds[i].innerHTML == 'ISBN') &&
	(isbn = checkISBN( tds[i+1].innerHTML )) )
    {
      burro( 'abse', isbn );
      break;
    }
}

if (document.location.hostname.match('bibl.liu.se')) {
  var a = document.getElementsByTagName( 'a' ), i;
  for( i=0; i<a.length-1; i++ )
    if( a[i].innerHTML.match( /ISBN\/ISSN:/i ) &&
	(isbn = /^[0-9X]*/i.exec( a[i+1].innerHTML )) &&
	(checkISBN( isbn[0] )) )
    {
      burro( 'liubibl', isbn );
      break;
    }
}

if (document.location.hostname.match('buy.com')) {
  isbn = checkISBN( document.title.match(/ISBN ([0-9X]{10})/)[1] );
  if (isbn) burro( 'buy', isbn );
}

if (document.location.hostname.match('powells.com')) {
  bs = document.getElementsByTagName('b');
  for (i=0; i<bs.length; i++)
    if (bs[i].innerHTML.match('ISBN:')) {
      isbn = checkISBN(bs[i].nextSibling.nextSibling.text);
      if (isbn) burro( 'powells', isbn );
    }
}

if (document.location.hostname.match('half.ebay.com')) {
  bs = document.getElementsByTagName('b');
  for (i=0; i<bs.length; i++)
    if (bs[i].innerHTML.match('ISBN:')) {
      isbn = checkISBN(bs[i].nextSibling.text);
      if (isbn) burro( 'half', isbn );
    }
}

if (document.location.hostname.match('alldirect.com')) {
  bs = document.getElementsByTagName('b');
  for (i=0; i<bs.length; i++)
    if (bs[i].innerHTML.match('ISBN:')) {
      isbn = checkISBN(bs[i].nextSibling.nextSibling.text);
      if (isbn) burro( 'alldirect', isbn );
    }
}

})();

