/*
2008-01-11:
  - from server1 2007-01-11: use imgObj.infoheight, imgObj.style (mv)
2006-11-16:
  - use imgObj.closebutton instead of default 'sluit venster' (mv)
2005-10-05:
  - hide button if imgObj.hidebutton is 1 (mv)
2005-08-09:
  - use imgObj.resizable if set (instead of default resizable=yes) (mv)
2004-12-15:
  - window.open first param '' instead of 'about:blank' (mv)
2004-10-19:
  - disabled instadrag events (mv)
  - added resizable=yes to window features (mv)
  - set width=100% to enable resize along with window resize (mv)
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)
  {
    //test
    if (location.host == 'www.nvos.nl') { imgObj.scrollbars = 1; }
  
    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;
      }

      //  when image smaller than screen              
      s_w = screen.width;
      s_h = screen.height;
      
      if ((s_w < imgObj.w) && (s_h < imgObj.h)){

      	correction = 1;
				
				if (s_w/s_h != imgObj.w/imgObj.h) {
					correction = (s_w/s_h) / (imgObj.w/imgObj.h);
				}	
				if (imgObj.w > imgObj.h) {
					
					imgObj.w = imgObj.w / (imgObj.h / (s_h / correction));
					imgObj.h = s_h / correction;
					
				}else if(imgObj.w < imgObj.h){
					
					imgObj.h = imgObj.h / (imgObj.w / (s_w / correction));
					imgObj.w = s_w / correction;
					
				}else{
					imgObj.h = imgObj.w = s_h;
				}
      }

      
      if (!imgObj.name)
      {
        imgObj.name = imgObj.src;
      }
  
      w = window.open(
        '',
        '_blank',
        ''
        + ((''+imgObj.resizable).length && (''+imgObj.resizable) != 'undefined' ? 'resizable=' + imgObj.resizable : 'resizable=yes') + ','
        + 'width='+imgObj.w+',height='+((imgObj.info?(imgObj.infoheight?imgObj.infoheight:100):24)+parseInt(imgObj.h))
        + (imgObj.scrollbars ? ',scrollbars=' + imgObj.scrollbars : '')
        );
      w.document.write(genImgBody(imgObj));
      w.document.title = imgObj.name;
      w.document.close();
      
      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:#999;font-family:sans-serif;overflow:'+(imgObj.scrollbars?'auto':'hidden')+';'
      + (imgObj.bg ? 'background-image:url('+imgObj.bg+')' : '')
      + (imgObj.style ? imgObj.style : '')
      + '" '
      + '>'
      + '<img '
      + (imgObj.alt ? 'alt="' + imgObj.alt + '" ' : '')
      
      // instadrag doesn't work since MSIE blocks window.moveby on non-domain urls?
      // disabled the events below to prevent errors appearing
      //
      //+ '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+' '  : '')  // disabled, do relative to window size instead:
      + (imgObj.w ? 'width=100% '  : '')
      
      //+ (imgObj.h ? 'height='+imgObj.h+' ' : '')  // probably unneeded, conflicts with width=100%
      
      + '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="' + (imgObj.hidebutton == 1 ? 'display: none;' : '') + '">'
      + '<input type="button" style="position:absolute;left:0px;width:100%;bottom:0px;text-align:center;font-family:sans-serif;font-size:16px;height:24px;" onclick="if(opener && opener.imgWinOpened){opener.imgWinOpened[\''+imgObj.src+'\']=null;}self.close();" value="'+(imgObj.closebutton?imgObj.closebutton:'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;
        }
      }
    }
  }
