$(function() {

  ids = $("span.calculating_price").map(function() {
    return $(this).attr("data-asin");
  });
  if (ids.length) {
    combined_ids = Array.prototype.join.call(ids, ",");
    $.ajax({
      success: function(request) { 
        $.each(request, function(asin, price) {
          $(".calculating_price[data-asin='" + asin + "']").html(price).removeClass("calculating_price");
        });
        if ($(".calculating_price").length) {
          $(".add_to_cart").empty();
          $(".calculating_price").html("Currently Unavailable").removeClass("calculating_price");
        }
      },
      dataType: 'json',
      type: 'get', 
      url: "/FindPrice/?asins=" + combined_ids
    });
  }
  
  $("a").click(function() {
    id = $('meta[name=session_token]').attr('content');
    h = $(this).attr("href");
    if (h.indexOf("http://" + window.location.hostname) >= 0) {
      return;
    }
    if (h.indexOf("#") >= 0) {
      return;
    }
    $(this).attr("href", h + ((h.indexOf("?") >= 0) ? "&" : "?") + "__router=" + id);
  });
  
});
