var ie  = document.all;
var ns6 = document.getElementById && !document.all;

function ietruebody()
{
  var iebody = document.body;
  if (document.compatMode && document.compatMode != "BackCompat" && !window.opera)
  {
    iebody = document.documentElement;
  }
  return iebody;
}

function enlarge(which, e, alignment)
{
  if (alignment == null) alignment = 'normal';
  
  if (ie||ns6)
  {
    if (ns6)
    {
      curtop    = pageYOffset;
      curleft   = pageXOffset;
    }
    else
    {
      var iebody = ietruebody();
      curtop    = iebody.scrollTop;
      curleft   = iebody.scrollLeft;
    }
    mouseX = ns6 ? e.clientX : event.clientX;
    mouseY = ns6 ? e.clientY : event.clientY;
    
    waitobj = document.getElementById ? document.getElementById("waitforimage") : document.all.waitforimage;
    waitobj.innerHTML = '<img src="/img/wait.gif">';
    waitobj.style.left = (curleft + mouseX - 10) + "px";
    waitobj.style.top  = (curtop  + mouseY - 10) + "px";
    waitobj.style.visibility = "visible";

    crossobj = document.getElementById ? document.getElementById("showimage") : document.all.showimage;
    crossobj.innerHTML = '<div onClick="closepreview()" onMouseOut="closepreview()"><img id="previewimage" src="'+which+'"></div>';
    imageobj = document.getElementById ? document.getElementById("previewimage") : document.all.previewimage;

    showpreview(alignment);

    return false;
  }
  else //if NOT IE 4+ or NS 6+, simply display image in full browser window
  {
    return true; 
  }
}

function showpreview(alignment)
{
  if (imageobj.complete)
  {
    if (ns6)
    {
      curtop    = pageYOffset;
      curleft   = pageXOffset;
      curheight = innerHeight;
      curwidth  = innerWidth;
    }
    else
    {
      var iebody = ietruebody();
      curtop    = iebody.scrollTop;
      curleft   = iebody.scrollLeft;
      curheight = iebody.offsetHeight;
      curwidth  = iebody.offsetWidth;
    }
    
    // Set image size
    var scalefactor = 1;
    var imgwidth  = imageobj.width;
    var imgheight = imageobj.height;
    if (imgwidth > curwidth - 40 || imgheight > curheight - 40)
    {
      var scalefactorx = imgwidth  / (curwidth  - 40);
      var scalefactory = imgheight / (curheight - 40);
      scalefactor = scalefactorx > scalefactory ? scalefactorx : scalefactory;
    }
    imgwidth /= scalefactor; imgheight /= scalefactor;
    imageobj.style.width  = imgwidth  + "px";
    imageobj.style.height = imgheight + "px";

    // Align image
    if (alignment == 'normal')
    {
      horzpos = curleft + mouseX - 20;
      if (mouseX + imgwidth > curwidth) horzpos = curleft + curwidth - imgwidth - 20;
      if (horzpos - curleft < 20) horzpos = curleft + 20;
      crossobj.style.left = horzpos + "px";
  
      vertpos = curtop + mouseY - 20;
      if (mouseY + imgheight > curheight) vertpos = curtop + curheight - imgheight - 20;
      if (vertpos - curtop < 20) vertpos = curtop + 20;
      crossobj.style.top = vertpos + "px";
    }
    else if (alignment == 'center')
    {
      crossobj.style.left = (curleft + (curwidth  - imgwidth ) / 2) + "px";
      crossobj.style.top  = (curtop  + (curheight - imgheight) / 2) + "px";
    }

    waitobj.style.visibility  = "hidden";
    crossobj.style.visibility = "visible";
  }
  else
  {
    window.setTimeout("showpreview('" + alignment + "')", 100);
  }
}

function closepreview()
{
  crossobj.style.visibility = "hidden";
}
