  /*
  
    imgWin.js
    
    2003-06-02:
      - also unset opener.imgWinOpened[imgObj.src] on Esc-triggered close (mv)

    2003-05-27:
      - close window on keypress Esc (mv)

    2002-12-12:
      - show background-image specified by imgObj.bg (mv)

    2002-12-11:
      - added overflow:hidden; to generated body style (mv)
    
    2002-12-04:
      - autodetect width/height if not specified (mv)
      - use src as name if not specified (mv)
      - 'sluit venster' button instead of link (mv)
    
    2002-07-16:
      - use imgObj.info instead of imgObj.alt for text below img (mv)
      - use imgObj.infoAlign for text-align (mv)
    
    2002-07-04:
      - added fgResizeImg() (mv)
    
    2002-07-02:
      - show imgObj.alt below image (mv)
    
    2002-06-06:
      - genImgBody() shows imgObj.alt, if set (mv)
    
    2002-05-29:
      - created (mv)
  
  */


  /*
  **  imgWin()
  **
  **    opens image popup with 'Sluit venster' link,
  **    draggable by mousedown/mousemove on image.
  **
  **    executing the function again with same param
  **    closes the window (if previously opened)
  **
  **
  **  example usage:
  **
  **    <a href="dog.jpg"
  **      onclick="return imgWin({src:'dog.jpg', name:'Nice doggie', w:640, h:480,
  **        alt:'This is our dog, he is nice.'});">
  **
  */

  imgWinOpened = new Array();

  function imgWin(imgObj)
  {
    if (imgWinOpened[imgObj.src])
    {
      imgWinOpened[imgObj.src].close();
      imgWinOpened[imgObj.src] = null;
    }
    else
    {
      // try to determine width/height, if not specified on function call
      if (!imgObj.w || !imgObj.h)
      {
        var tmp = new Image();
        tmp.src = imgObj.src;
        imgObj.w = tmp.width;
        imgObj.h = tmp.height;
      }
      
      if (!imgObj.name)
      {
        imgObj.name = imgObj.src;
      }
  
      w = window.open(
        '',
        '_blank',
        'width='+imgObj.w+',height='+((imgObj.info?100:24)+parseInt(imgObj.h)));
      w.document.write(genImgBody(imgObj));
      w.document.title = imgObj.name;
      
      imgWinOpened[imgObj.src] = w;
    }
    
    return false;
  }
  
  

  /*
  **  genImgBody()
  **
  **    helper function for imgWin(), generates popup html
  **
  */

  function genImgBody(imgObj)
  {
    return '<html><body '
      + 'onkeypress="if(event.keyCode==27){if(opener && opener.imgWinOpened){opener.imgWinOpened[\''+imgObj.src+'\']=null;}self.close();}" '
      + 'style="margin:0px;background-color:#ffffff;font-family:sans-serif;overflow:hidden;'
      + (imgObj.bg ? 'background-image:url('+imgObj.bg+')' : '')
      + '" '
      + '>'
      + '<img '
      + (imgObj.alt ? 'alt="' + imgObj.alt + '" ' : '')
      + 'onmousedown="this.domove=1;this.curX=event.clientX;this.curY=event.clientY;" '
      + 'onmouseup="this.domove=0" '
      + 'onmousemove="if(this.domove){self.moveBy(event.clientX-this.curX, event.clientY-this.curY);}" '
      + 'ondragstart="window.event.returnValue=false" '
      + 'src='+(imgObj.bg?'/images/trans.gif':imgObj.src)+' '
      + (imgObj.w ? 'width='+imgObj.w+' '  : '')
      + (imgObj.h ? 'height='+imgObj.h+' ' : '')
      + 'style="border-bottom:1px solid black'
      + '" />'
      + '<div style="text-align:'+(imgObj.infoAlign?imgObj.infoAlign:'center')+';font-size:0.9em; border-bottom:0px solid red;height:80px;overflow-x:hidden;overflow-y:auto;" />'
      + (imgObj.info?imgObj.info:(imgObj.alt?imgObj.alt:''))
      + '</div>'
      + '<br />'
      + '<div style="">'
      + '<input type="button" style="position:absolute;left:0px;width:100%;bottom:0px;text-align:center;background:#ffffff;font-family:arial,verdana,sans-serif;font-size:11px;height:22px;" onclick="if(opener && opener.imgWinOpened){opener.imgWinOpened[\''+imgObj.src+'\']=null;}self.close();" value="sluit venster" />'
      + '</div>'
      + '</body></html>';
  }



  /*
  **  fgResizeImg()
  **
  **    set width/height of imgtag (in correct proportions) to prevent
  **    ugly runtime resizing while imgs are loading
  **
  */
  
  function fgResizeImg(id, rW, rH, tW, tH)
  {
    var nW = tW;
    var nH = tH;
    if (rW > rH) { nH = rH / (rW/tW); }
    if (rH > rW) { nW = rW / (rH/tH); }
    if (document.getElementById)
    {
      if (document.getElementById(id))
      {
        with(document.getElementById(id).style)
        {
          width   = nW;
          height  = nH;
        }
      }
    }
    else if (document.all)
    {
      if (document.all[id])
      {
        with(document.all[id].style)
        {
          width   = nW;
          height  = nH;
        }
      }
    }
  }

