

/*
 * Go to given url.  This is often used in onclick handlers.
 */
function visi_goto (url) {
  location.href = url;
}


/*
 * Replace textual e-mail addresses with mailto links within the document.
 * For example, given element
 *
 *   <span id="visi_mailto">turnkey (at) spam dot invalid</span>
 *
 * turn that into
 * 
 *   <span id="visi_mailto"><a
 *   href='mailto:turnkey@spam.invalid'>turnkey@spam.invalid</a></span>
 *   
 * This ought to make the address invisible to email harvesting robots.
 */
function visi_mailto() {
  var arr = document.getElementsByTagName("span");
  for (i = 0; i < arr.length; i++) {
    var f = arr[i];
    if (f.className == "visi_mailto") {
      var addr = f.innerHTML;
      addr = addr.replace(/\(at\)/, "@");
      addr = addr.replace(/[ \t\n\r]at[ \t\r\n]/, "@");
      addr = addr.replace(/[ \t\r\n]dot[ \t\r\n]/g, ".");
      addr = addr.replace(/[ \t\r\n]/g, "");
      f.innerHTML = "<a href='mailto:" + addr + "'>" + addr + "</a>";
    }
  }
}


/*
 * Highlight area in a client-side image map.  See visisom.php for an
 * example.
 */
function visi_highlight(area,img,title) {
  var borderwidth = 2;

  /* extract coordinates of the area */
  var coords = area.coords.split(",");
  var x      = parseInt(coords[0]) - borderwidth;
  var y      = parseInt(coords[1]) - borderwidth;
  var width  = parseInt(coords[2]) - x - 2*borderwidth;
  var height = parseInt(coords[3]) - y - 2*borderwidth;

  /* compute top-left position of image (will be few pixels off in MSIE) */
  var basex = 0;
  var basey = 0;
  var f = document.getElementById(img);
  if (f  &&  f.offsetParent) {
    basex = f.offsetLeft;
    basey = f.offsetTop;
    while (f = f.offsetParent) {
      basex += f.offsetLeft;
      basey += f.offsetTop;
    }
  }

  /* create highlight element as necessary */
  var ht = document.getElementById("visi_highlight");
  if (!ht) {
    ht = document.createElement ("div");
    ht.id = "visi_highlight";
    document.body.appendChild (ht);
  }

  /*
   * Attempt to display a description of the area.  This works only
   * barely in MSIE 7.  Changing window status message is prohibited by
   * default and the hint is displayed if only the highlight element is
   * not completely transparent.
   */
  ht.title      = title;
  window.status = title;

  /* highlight rectangular area under mouse cursor */
  ht.style.display     = "block";
  ht.style.position    = "absolute";
  ht.style.left        = (basex + x) + "px";
  ht.style.top         = (basey + y) + "px";
  ht.style.width       = width + "px";
  ht.style.height      = height + "px";
  ht.style.borderWidth = borderwidth + "px";
  ht.style.borderColor = "red";
  ht.style.borderStyle = "solid";

  ht.onclick = function() {
      return false;
    };
  ht.onmouseout = function() {
      var ht = document.getElementById("visi_highlight");
      if (ht) {
        ht.style.display = "none";
      }
    };
}


/*
 * Move visi_tocimg element to the upper right corner of the page.  If
 * there are several elements named visi_tocimg, then move the first one.
 */
function visi_tocimg () {
  var content = document.getElementById("visi_content");
  var tocimage = document.getElementById("visi_tocimg");
  if (content  &&  tocimage) {

    /* compute the width of the largest image within tocimage */
    var imgs = tocimage.getElementsByTagName("img");
    var w = 150;
    for (var i = 0; i < imgs.length; ++i) {
      if (imgs[i].width > w) {
        w = imgs[i].width;
      }
      if (imgs[i].style.width > w) {
        w = imgs[i].style.width;
      }
    }

    /* 
     * Set the maximum width according to the largest image.  This is
     * important to fold long image captions automatically.
     */
    if (w > 0  &&  !tocimage.style.maxWidth) {
      tocimage.style.maxWidth = w + "px";
    }

    /* move toc image as the first child of content node */
    content.insertBefore (tocimage, content.childNodes[0]);
  }
}


/*
 * Install page features.
 */
visi_mailto ();
visi_tocimg ();
