/*========================================
Released under Creative Commons License
http://creativecommons.org/licenses/by/2.0/

Author: Kornel Lesinski
Modified by: Łukasz Muchlado
Version: 0.3 05r1003

Skrypt generujący okienko pop-up z obrazkiem po kliknięciu na link zawierający odnośnik do obrazka
v0.3 (05r1003) dodanie wyświetlania pionoweko suwaka jeśli obrazek nie mieści się w oknie, usunięcie wymogu aby wewnątrz a było <img
v0.2 (05r0906)
========================================*/

function pp_click()
{
	if (this.pp_win && this.pp_win.close && !this.pp_win.closed) {
		this.pp_win.close(); this.pp_win=false; return false;
	}

	var imgs = this.getElementsByTagName('img');
	var title = imgs[0].getAttribute('title')?imgs[0].getAttribute('title'):this.getAttribute('title');
	var alt = imgs[0].getAttribute('alt')?imgs[0].getAttribute('alt'):this.getAttribute('alt');

	var winopts = "dependent=yes,toolbar=no,resizable=yes,scrollbars=yes,"+pp_titl2size(this.getAttribute('title')?this.getAttribute('title'):title);

  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])+30)+",height="+(parseInt(out[3])+50);
	}
	return "width=550,height=420";
}

function pp_writedoc(href,doc,title,alt){
  doc.open();
    doc.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">');
	doc.write('<html xmlns="../../../www.w3.org/1999/xhtml.html">');
	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">html{overflow-y:auto;overflow-x:hidden;}body {margin:0; padding:0; background:#110807; font-size:80%; font-family:arial,sans-serif; color:white;} p{margin:0; text-align:center;font-size:0.9em;} img{margin-top:1em;background:#433D2D;padding:3px;border:3px double gray;}</style>');
	doc.write('</head><body>');
	doc.write('<p><img title="'+title+'" src="'+href+'" alt="'+alt+'"></p><p>'+title+'</p></body></html>');
  doc.close();
}

function pp_init(element){
	if (!element.getElementsByTagName) {return false;}

	var as = element.getElementsByTagName('a');
	
	var re=/jpg|jpeg|gif|png|bmp|tiff$/i;
	//var re2=/\)/;//kończy się nawiasem

	for(i=0;i<as.length;i++){
		//imgs = as[i].getElementsByTagName('img');
		//Ustawiam otwieranie popupa tylko dla linków w których w src i treści linku jest jeden obrazek
		if(re.test(as[i].href)/* && (imgs.length == 1) */){
			as[i].onclick = pp_click;
		}
	}
	return true;
}

function pp_init_body(){
	pp_init(document.body);
}
window.onload = pp_init_body;
