// http://www.howtocreate.co.uk/perfectPopups.html
// with modifications by Graham Hill 23rd January 2011
// MODS:
// altInfo parameter added to function so title='+altInfo+'
// true removed from first return
// false removed from last return
// see onblur/onclick parameters below
// End MODS:
// Calling Method:
// (1) <a href="URL of image" onclick="return popImage(this.href,'Name&nbsp;of&nbsp;Image','Alt&nbsp;text&nbsp;of&nbsp;Image');"><link></a>
// or
// (2) <a href="javascript:popImage('URL of image','Name&nbsp;of&nbsp;Image','Alt&nbsp;text&nbsp;of&nbsp;Image')"><link>/a>
// Dependent on (1) or (2) set "return;" parameters below.
// Do not mix calling methods - but if needed then set up two distinct scripts and change function name accordingly.
// End Calling Parameters:

// NOTE: DO NOT USE <base target="?????"> Parameter in calling HTML page.

// really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 40;
defaultHeight = 20;

// kinda important
var AutoClose = true;

// I have modified the incoming parameters and some code - GH 
function popImage(imageURL,imageTitle,altInfo)
{
  if (navigator.appVersion.indexOf('Chrome')>0) 
     {defaultWidth  = 10, defaultHeight = 10;}  // Trying to fix a bug in Chrome

// Scrollbars best set ON which forces verticle bar and horizontal bar only if needed.
// Resizeable best set ON which allows for the possibility of verticle spacing in some browser implementations.
 
  var imgWin = window.open('','_blank','scrollbars=1,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);

// Dependent on calling method, choose from following options by commenting out:

   if( !imgWin ) {return true;} // Set this if using calling method (1) - Popup blocker will return TRUE and image will be displayed in HTML calling page.
// if( !imgWin ) {return;}      // Set this if using calling method (2) - Popup blocker will need to be set OFF.

  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+

// Code to reduce image size if too latge for screen.
// Problem with this is that there's no way to be certain that 150/50 are valid for all implementations.
'var mH = screen.availHeight-150, mW = screen.availWidth-50;\n'+
'if( oH > mH || oW > mW ) {\n'+
'mH = mH \/ oH; mW = mW \/ oW;\n'+
'var zoomFactor = ( mH < mW ) ? mH : mW;\n'+
'oH = Math.floor( oH * zoomFactor );\n'+
'oW = Math.floor( oW * zoomFactor );\n'+
'document.images[0].height = oH;\n'+
'document.images[0].width = oW;\n'+
'}\n'+
// End code to reduce image size if too latge for screen.

    'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
    'window.doneAlready = true;\n'+ //for Safari and Opera
    'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
    'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
    'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
    'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
    'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
    'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
    '}\n'+
    '<\/script>'+
// Choose from following options by commenting out:
//  '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+ // Allows single window    (onclick="self.close(); is default for I.E.) - Original
//  '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="             "':'')+'>'+ // Allows multiple windows (onclick="self.close(); is default for I.E.)
//  '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();" onclick="self.close();"':'')+'>'+  // Allows single window    (onclick="self.close(); mandated)
    '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="             " onclick="self.close();"':'')+'>'+  // Allows multiple windows (onclick="self.close(); mandated)
    (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
    '<img src="'+imageURL+'" alt="Loading image ..." title='+altInfo+' onload="resizeWinTo()">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }

// Dependent on calling method, choose from following options by commenting out:
   return false; // Set this if using calling method (1).
// return        // Set this if using calling method (2).
}

