﻿// Source: http://www.howtocreate.co.uk/perfectPopups.html

//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 800;
defaultHeight = 600;

//kinda important
var AutoClose = false;

//don't touch
function popImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','scrollbars=yes,resizable=0,status=0,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'var winW = 0;'+
    'var winH = 0;'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    '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;winW = 20; winH = 20;}\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight;}\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight;winW = 20; winH = 20;}\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'var maxW = 1000;\n'+
    'var maxH = 1000;\n'+
    'winW = oW + (( oW + 200 ) - myW) + winW;\n'+
    'winW = winW > maxW ? maxW : winW;\n'+    
    'winH = oH + ((oH + 200 ) - myH) + winH;\n'+    
    'winH = winH > maxH ? maxH : winH;\n'+    
    'x.resizeTo(winW, winH);\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>'+
    '<\/head><body onload="resizeWinTo();" style="margin:10px 0px 10px 10px;"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="margin-right:10px;display:table;">'))+
    '<img src="'+imageURL+'" alt="Loading image ..." title="" onload="resizeWinTo();">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}

function popImage2(imageURL,imageTitle,winWidth,winHeight,selfClose){
  var imgWin = window.open('','_blank','scrollbars=no,toolbar=no,menubar=no,directories=no,location=no,resizable=0,status=0,width='+winWidth+',height='+winHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'var winW = 0;'+
    'var winH = 0;'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    '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;winW = 20; winH = 20;}\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight;}\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight;winW = 20; winH = 20;}\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'var maxW = 1000;\n'+
    'var maxH = 1000;\n'+
    'winW = oW + (( oW + 200 ) - myW) + winW;\n'+
    'winW = winW > maxW ? maxW : winW;\n'+    
    'winH = oH + ((oH + 200 ) - myH) + winH;\n'+    
    'winH = winH > maxH ? maxH : winH;\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>'+
    '<\/head><body style="margin:10px;" '+(selfClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="text-align:center">'))+
    '<img src="'+imageURL+'" alt="Loading image ..." title="">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}
