function attachPopupBlock(e) {
  if (this.popupContainer) {
    if (this.popupContainer.getStyle('display') == 'none') {
      Effect.Appear(this.popupContainer);
    } else {
      Effect.Fade(this.popupContainer);
    }
  } else {
    href = this.getAttribute("href"); 
    this.ajax = new Ajax.Request(href,
                                 {
                                   method: "post",
                                   onSuccess: function(r) {
                                     this.popupContainer = document.createElement("div");
                                     this.popupContainer.innerHTML = r.responseText;
                                     Element.hide(this.popupContainer);
                                     this.parentNode.insertBefore(this.popupContainer, this.nextSibling);
                                     Effect.Appear(this.popupContainer);
                                  }.bind(this)
                                 });
  }
  Event.stop(e);
}

function applyPopupBlocks() {
  links = document.getElementsByTagName("a");
  for (i = 0; i < links.length; i++) {
    if (Element.hasClassName(links[i],"document") &&
        links[i].href.match('/([^.]+)$')) {
      Event.observe(links[i], "click", attachPopupBlock.bind(links[i]), false);
    }
  }
}

Event.observe(window, "load", applyPopupBlocks, false);
