window.onload = function() {
  popups();
} 

/** 
 * popup windows
 * open a new window with an image or a page
*/
function popups()
{
  if (!document.body.getElementsByTagName) return false;
  var as = document.body.getElementsByTagName('a');
  for(var i=0; i<as.length; i++)
    if ((as[i].className+'').match(/\bpopup\b/))
      as[i].onclick = popup;
  return true;
}
function popup()
{
  var url = this.getAttribute('href');
  var title = this.getAttribute('title');
  if (title) var size = title.match(/\(([0-9]+)x([0-9]+)\)/);
  if (size) title = title.replace(size[0],'');
  if (url.match(/png|gif|jpg|jpe|jpeg/)) var img = true;
  
  /* when size is larger then screen */
  var scrollbars = 0;
  if (size && img) {
    if (size[1] > window.screen.width) {
      size[1] = window.screen.width - 16;
      scrollbars = 1;
    }
    if (size[2] > window.screen.height - 200) {
      size[2] = window.screen.height - 200;
      size[1] = parseInt(size[1]) + 16;
      if (size[1] == window.screen.width) size[1] = window.screen.width - 16;
      scrollbars = 1;
    }
  }  
  if (!size) var size = new Array('', 640, 480);

  /* window options */
  var win_options = 'left=0,top=0,menubar=0,toolbar=0,location=0,status=0,scrollbars='+scrollbars+',resizable=1,dependent=1,width='+size[1]+',height='+size[2];

  /* creates new window */
  if (win = window.open(url,'',win_options)) {
    if (img) {
      win.document.open();
      win.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
      win.document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
      win.document.write('<head><title>'+title+'</title>');
      win.document.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
      win.document.write('<style type="text/css">body {margin:0; padding:0; background:#fff;}</style>');
      win.document.write('</head><body>');
      win.document.write('<img src="'+url+'" alt="'+title+'" onclick="window.close()" />');
      win.document.write('</body></html>');
      win.document.close();
    }
    return false;
  }
  
  /* follows link on failure */
  return true; 
}
