$(document).ready(function(){

  var callback = 'ajax/reblock';

  // Array of blocks to reload after page load to replace page cached content
  var blocks = Drupal.settings.reblock;

  // Retain the initial querystring to pass to the reblocker
  var q = window.location.pathname;
      q = q.substring(q.indexOf(Drupal.settings.basePath) + Drupal.settings.basePath.length);
      q = encodeURI(q);

  // Loop over blocks and reload them one at a time
  for (var i = 0; i < blocks.length; i++) {

    var block_id = blocks[i];
        block_id = 'block-' + block_id.replace("/", "-"); // block id format, make sure this is the same as it is implemented on block.tpl.php

    if ($('#' + block_id).length > 0) {
      var url = Drupal.settings.basePath + callback + '/' + blocks[i] + '?oq=' + q + '&' +  document.location.search.substring(1) + '&callback=?';
      $.getJSON(url, function(data) {
        Drupal.attachBehaviors($('#' + data.id + ' .content').html(data.content)); // Update DOM and reattach behaviours
      });
    }

  }

});
