/**********************
Released under Creative Commons License
http://creativecommons.org/licenses/by/2.0/

Author: Kornel Lesinski
http://pornel.ldreams.net/pornpups
**********************/

/* click handler */
function pp_click()
{
	/* toggle window */
	if (this.pp_win && this.pp_win.close && !this.pp_win.closed) {this.pp_win.close(); this.pp_win=false; return false;}

	/* collect title from image/link */
	var imgs = this.getElementsByTagName('img');
	var title = imgs[0].getAttribute('title')?imgs[0].getAttribute('title'):this.getAttribute('title');
	var alt = imgs[0].getAttribute('alt');

	var winopts = "dependent=yes,toolbar=no,resizable=no,"+pp_titl2size(this.getAttribute('title')?this.getAttribute('title'):title);

	/* do the boogie */
  if (win = window.open(this.href,'_blank',winopts))
  {
	  this.pp_win = win;
	  pp_writedoc(this.href,win.document,title,alt);
    return false;
  }
  /* follow link on failure */
  return true;
}

/* finds dimensions in given string, format: (<width>x<height>), outputs width=<width>,height=<height> */
function pp_titl2size(str)
{
	if (str)
	{
		var out = str.match(new RegExp('\(([0-9]+)x([0-9]+)\)'));
		return "width="+(parseInt(out[2])+15)+",height="+(parseInt(out[3])+15);
	}
	return "width=550,height=420";
}

/* create new document. Code it using DOM if you wish :> */
function pp_writedoc(href,doc,title,alt)
{
  doc.open();
  doc.write('<?xml version="1.0" encoding="ISO-8859-2" ?>');
  doc.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">');
	doc.write('<html xmlns="http://www.w3.org/1999/xhtml">');
	doc.write('<head><head><title>'+alt+'</title>');
	doc.write('<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2" />');
	doc.write('<style type="text/css">body {	margin: 5px;	padding: 0;	background: #FFFFFF;	font-size: 10px;	font-family: Verdana, Arial, Helvetica, sans-serif;	color: #333333;}  img {background: white; padding: 0px; border: 0px;} </style>');
	doc.write('</head><body onclick="window.close()">');
	doc.write('<p><img title="'+title+'" src="'+href+'" alt="'+alt+'"></p></body></html>');
  doc.close();
}

/* set click handler on descendants */
function pp_init(element)
{
	if (!element.getElementsByTagName) {return false;}

	var as = element.getElementsByTagName('a');
	for(i=0;i<as.length;i++)
		as[i].onclick = pp_click;

	return true;
}

/* you need something smarter than that in production code! */
function pp_init_body()
{
	pp_init(document.body);
}
window.onload = pp_init_body;
