/*
	 Skrypt jest darmowy, poki ten komentarz w nim pozostaje.
	 Released under Creative Commons License
	 http://creativecommons.org/licenses/by/2.0/

	 Original author: Kornel Lesinski
	 http://pornel.net/pups/
*/

var fotoPopup = {
	initElement: function(element) {
		if (!element || !element.getElementsByTagName) {return false;}

		var as = element.getElementsByTagName('a');
		for(var i=0;i<as.length;i++) {
			if(as[i].className.match(/fotoPopup/i)) {
				as[i].onclick = this.click;
			}
		}

		return true;
	},

	writedoc: function(win,href,title,alt,h){
		var doc = win.document;
		doc.open('text/html;charset=ISO-8859-2');
		doc.write(
		'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' +
		'<html id="popup">' +
		'<head><title>' + alt + '</title>' +
		'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' +
		'<meta http-equiv="imagetoolbar" content="false">' +
		'<link rel="stylesheet" href="css/fotoPopup.css">' +
		(h?'<style type="text/css">.obrazek {min-height: ' + h + 'px} #iesux#popup .obrazek {height: ' + h + 'px;}</style>':'') +
		'</head><body onclick="window.close();">' +
		'<p class="obrazek"><img src="'+href+'" alt="'+alt+'"></p>' +
		'<p class="klik">(kliknij, aby zamknąć)</p>' +
		'</body></html>'
		);
		doc.close();
	},

	title2size: function(str) {
		return new Array(str,450,470);
	},

	click: function(ev) {
		ev = ev||event;
		try {
			if (this.pp_win && this.pp_win.close && !this.pp_win.closed) {this.pp_win.close(); this.pp_win=false;}
		}
		catch(e){}

		try {
			var title = this.getAttribute('title');
			var alt = this.getAttribute('title');

			var titleandsize = fotoPopup.title2size(title);
			
			var width = titleandsize[1]+30; var height = titleandsize[2]+60;
			var left = parseInt(screen.width / 2 - width / 2);
			var top = parseInt(screen.height / 2 - height / 2);
			var winopts = "dependent=yes,toolbar=no,resizable=yes,left=" + (left) +",top= " + (top) + ",width=" + (width) + ',height=' + (height);

			var win = window.open(this.href,'_blank',winopts);
			if (win && win.opener) {
				this.pp_win = win;
				fotoPopup.writedoc(win,this.href,titleandsize[0],alt,titleandsize[2]);
				if (ev.stopPropagation) ev.stopPropagation();
				return false;
			}
		}
		catch(e){}
		return true;
	},

	init: function() {
		var oldOnload = window.onload;
		var that = this;
		window.onload = function()
		{
			if (oldOnload) try{oldOnload();}catch(e){}
			that.initElement(document.body);
		}
	}
};

commonForm.init();
fotoPopup.init();