/**
 * Released under Creative Commons License
 * http://creativecommons.org/licenses/by/2.0/
 *
 * Author: Kornel Lesinski
 * http://pornel.ldreams.net/pornpups
 *
 * Modified by: Patryk Stepien
 */

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 = this.getAttribute('title');
	var alt = this.getAttribute('title');
	var size = (this.getAttribute('title') ? this.getAttribute('title') : title).match(new RegExp('\(([0-9]+)x([0-9]+)x([0-9]+)\)'));
	var mwidth = (parseInt(size[2]));
	var mheight = (parseInt(size[3]));
	var ajdii = (parseInt(size[4]));
	var winopts = "left="+((screen.availWidth/2)-(mwidth/2)-20)+",top="+((screen.availHeight/2)-(mheight/2)-30)+",dependent=yes,toolbar=no,resizable=yes,width=" + (mwidth+30) + ",height=" + (mheight+50);
	if(win = window.open(this.href, '_blank', winopts)) {
		this.pp_win = win;
		pp_writedoc(this.href, win.document, title, alt, ajdii);
		return false;
	}
	return true;
}


function pp_writedoc(href, doc, title, alt, ajdi) {
	doc.open();
	doc.write('<?xml version="1.0" encoding="iso-8859-2"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/XHTML1/DTD/XHTML1-strict.dtd">\n');
	doc.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">\n\n');
	doc.write('\t<head>\n\n');
	doc.write('\t\t<meta http-equiv="Content-type" content="text/html; charset=iso-8859-2" />\n');
	doc.write('\t\t<title>'+title+'</title>\n');
	doc.write('\t\t<style type="text/css">\n');
	doc.write('\t\t\tbody { margin: 0; margin-top: 10px; padding: 0; background: #333; font-size: 12px; font-family: verdana,arial,sans-serif; color: white; }\n');
	doc.write('\t\t\tp { margin: 0; text-align: center; }\n');
	doc.write('\t\t\timg { background: black; padding: 3px; border: 3px double gray; }\n');
/*	doc.write('\t\t\tul { text-align: center; margin: 5px 0; padding: 0; }\n');
	doc.write('\t\t\tul li { margin: 0; padding: 0; display: inline; list-style-type: none; }\n');
	doc.write('\t\t\tul a { background: #eee; font-size: 10px; padding: 2px 5px; margin: 0 0 0 1px; border: 1px solid #a6afa5; color: #111; text-decoration: none; }\n');
	doc.write('\t\t\tul a:hover { border-color: #111; background: #fff; color: #000; }\n');
	doc.write('\t\t\tul #prev { margin-right: 10px; }\n');
	doc.write('\t\t\tul #next { margin-left: 10px; }\n');
*/	doc.write('\t\t</style>\n');
/*	doc.write('\t\t<script type="text/javascript" src="mgallery.php?p=' + ajdi + '"> </script>\n\n');
	doc.write('<script type="text/javascript">\n');
	doc.write('	var ajdi = ' + ajdi + '\n');
	doc.write('	var img1 = new Image();\n');
	doc.write('	img1.src = "foto/2006/" + (ajdi-1) + ".jpg";\n');
	doc.write('	var img2 = new Image();\n');
	doc.write('	img2.src = "foto/2006/" + (ajdi+1) + ".jpg";\n');
	doc.write('	function prev() {\n');
	doc.write('		document.getElementById("imgid").src = img1.src;\n');
	doc.write('		img1.src = "foto/2006/" + (ajdi-1) + ".jpg";\n');
	doc.write('		img2.src = "foto/2006/" + (ajdi+1) + ".jpg";\n');
	doc.write('		mwidth = document.getElementById("imgid").width;\n');
	doc.write('		mheight = document.getElementById("imgid").height;\n');
	doc.write('		self.moveTo(((screen.availWidth/2)-(mwidth/2)-20), ((screen.availHeight/2)-(mheight/2)-30));\n');
	doc.write('		self.resizeTo((mwidth+30), (mheight+100));\n');
	doc.write('		ajdi --;\n');
	doc.write('	}\n');
	doc.write('	function next() {\n');
	doc.write('		document.getElementById("imgid").src = img2.src;\n');
	doc.write('		img1.src = "foto/2006/" + (ajdi-1) + ".jpg";\n');
	doc.write('		img2.src = "foto/2006/" + (ajdi+1) + ".jpg";\n');
	doc.write('		mwidth = document.getElementById("imgid").width;\n');
	doc.write('		mheight = document.getElementById("imgid").height;\n');
	doc.write('		self.moveTo(((screen.availWidth/2)-(mwidth/2)-20), ((screen.availHeight/2)-(mheight/2)-30));\n');
	doc.write('		self.resizeTo((mwidth+30), (mheight+100));\n');
	doc.write('		ajdi ++;\n');
	doc.write('	}\n');
	doc.write('</script>\n');*/
	doc.write('\t</head>\n\n');
	doc.write('<body>\n\n');
	doc.write('<p><img title="' + title + '" src="' + href + '" alt="' + alt + '" onclick="window.close()" id="imgid" /></p>\n');
//	doc.write('<ul><li id="prev"><a href="javascript:prev()">&laquo;&laquo;</a></li><li>' + title + '</li><li id="next"><a href="javascript:next()">&raquo;&raquo;</a></li></ul>\n\n');
	doc.write('</body>\n\n');
	doc.write('</html>\n');
	doc.close();
}


function pp_init(element) {
	if(!element.getElementsByTagName) {
		return false;
	}
	var as = element.getElementsByTagName('a');
	for(i = 0; i < as.length; i ++) if(as[i].href.substr(as[i].href.length-3, 3) == 'jpg' || as[i].href.substr(as[i].href.length-3, 3) == 'gif' || as[i].href.substr(as[i].href.length-3, 3) == 'png') as[i].onclick = pp_click;
	return true;
}


function pp_init_body() {
	pp_init(document.body);
}


window.onload = pp_init_body;
