var imgPopupWindow = null;
var image = null;

    
function init() {
  var links = document.getElementsByTagName('a');
  for(var i = 0; i < links.length; i++) {
    if(links[i].className.indexOf('pu-image') != -1) {
      links[i].onclick = function()
      {
	  popupImage(this);
        return false;
      }
    }
  }
}

function popupImage(lObj) {
  var url   = lObj.getAttribute('href');
  var title = lObj.getAttribute('title');
  image = new Image();
  image.src = url;
  var h = image.height + 60;
  var w = image.width + 20;

  var str = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=' + w + ',height=' + h;
  if(!imgPopupWindow || imgPopupWindow.closed) {
    imgPopupWindow = window.open(url, imgPopupWindow, str);
  }
  imgTag = '<img src="' + url + '" alt="' + title + '" />'
  writeWindow(imgPopupWindow, title, imgTag);
  imgPopupWindow.document.close();
  waitForImage();
  imgPopupWindow.focus();
}


function waitForImage(url, title) {
  (image.complete) ? imgPopupWindow.resizeTo(image.width + 20, image.height + 60) : 
                     setTimeout(function(){waitForImage();}, 200);
}
/*
function buildWindow(url, title) {
  var h = image.height + 20;
  var w = image.width + 20;

  var str = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=' + w + ',height=' + h;
  if(!imgPopupWindow || imgPopupWindow.closed) {
    imgPopupWindow = window.open(url, imgPopupWindow, str);
  }
  imgTag = '<img src="' + url + '" alt="' + title + '" />'
  writeWindow(imgPopupWindow, title, imgTag);
  imgPopupWindow.document.close();
  imgPopupWindow.focus();
}
*/

function writeWindow(win, titleText, bodyText) {
  win.document.writeln('<?xml version="1.0" encoding="iso-8859-1"?>');
  win.document.writeln('\t<!DOCTYPE html PUBLIC');
  win.document.writeln('\t"-//W3C//DTD XHTML 1.1 Strict//EN"');
  win.document.writeln('\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
  win.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
  win.document.writeln('<head>');
  win.document.writeln('\t<title>' + titleText + '</title>');
  win.document.writeln('\t<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');	
  win.document.writeln('</head>');
  win.document.writeln('<body>' + bodyText + '</body></html>');
}

addLoadEvent(init);