var newWindow;
var timerID;

function fitimg() {
  tnImage = newWindow.document.getElementById('tnImage');

  if (tnImage.complete==undefined || tnImage.complete) { 
    clearInterval(timerID); 

    h=tnImage.height;
    w=tnImage.width;

    newWindow.resizeTo(w + 30, h + 90);
    newWindow.document.close();
  }
}

function popup_window(url) {
  var path=url;
  var title=path.substring(path.lastIndexOf('/',path.length) + 1, path.length);
  var title=title.substring(title.lastIndexOf('.', title.length), - title.length);

  newWindow=window.open("","popup_window","width=320,height=240");

  newWindow.document.open();
  newWindow.document.write("<html><head><title>" + title + "</title></head>");
  newWindow.document.write("<body style=”Ēmargin:0; padding:0;'>");
  newWindow.document.write("<a href='javascript:void(0);' onClick='window.close();'>");
  newWindow.document.write("<img src='" + url + "' id='tnImage' border='0' />");
  newWindow.document.write("</a>");
  newWindow.document.write("</body></html>");

  newWindow.focus();

  timerID=setInterval('fitimg()', 100);
}