/*
	 Released under Creative Commons License
	 http://creativecommons.org/licenses/by/2.0/

	 Original author: Kornel Lesinski
	 http://pornel.net/pups/
*/

var pups =
{
	init: function(element)
	{
		if (!element || !element.getElementsByTagName) {return false;}

		var as = element.getElementsByTagName('a');
		for(var i=0;i<as.length;i++)
		{
			if ((as[i].href+'').match(/\.(jpe?g|png|gif)/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 XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' +
		'<html>' +
		'<head><title>' + alt + '</title>' +
		'<meta http-equiv="Content-Type" content="text/html; charset="iso-8859-2">' +
		'<meta name="author" content="Adrian Biegañski" />' +
		'<meta name="keywords" content="white sims,the sims,the,sims,bia³e simy,biale simy,simy adka,simy,adka,adek" />' +
		'<meta name="description" content="SimyAdka.pl - nieoficjalna strona internetowa o bogatej kolekcji gier z serii The Sims&trade; prowadzona przez Adka." />' +
		'<meta http-equiv="imagetoolbar" content="false">' +
		'<link rel="stylesheet" type="text/css" href="pups.css">' +
		(h?'<style type="text/css">.obrazek {min-height: ' + h + 'px} #iesux#popup .obrazek {height: ' + h + 'px;}</style>':'') +
		'</head><body onclick="window.close();">' +
		'<img src="'+href+'" alt="'+alt+'" title="Kliknij, aby zamknąć"><br /><div><b>Copyright &copy; Simy Adka 2010.</b></div>' +
		'</body></html>'
		);
		doc.close();
	},

	title2size: function(str)
	{
		if (str)
		{
			var out = str.match(/\(([0-9]+)x([0-9]+)\)/);

			if (out) return new Array(str.replace(/\(([0-9]+)x([0-9]+)\)/g,''),parseInt(out[1]),parseInt(out[2]));
		}
		return new Array(str,screen.width,screen.height);
	},

	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 imgs = this.getElementsByTagName('img');
			var title = imgs[0].getAttribute('title')?imgs[0].getAttribute('title'):this.getAttribute('title');
			var alt = imgs[0].getAttribute('alt');

			var titleandsize = pups.title2size(title);
			var winopts = "dependent=yes,toolbar=no,resizable=yes,scrollbars=yes,width=" + (titleandsize[1]+40) + ',height=' + (titleandsize[2]+40);

			var win = window.open(this.href,'_blank',winopts);
			if (win && win.opener)
			{
				this.pp_win = win;
				pups.writedoc(win,this.href,titleandsize[0],alt,titleandsize[2]);
				if (ev.stopPropagation) ev.stopPropagation();
				return false;
			}
		}

		catch(e){}

		return true;
	},

	initNow: function()
	{
		this.init(document.body);
	},

	initLoad: function()
	{
		var oldOnload = window.onload;
		var that = this;
		window.onload = function()
		{
			if (oldOnload) try{oldOnload();}catch(e){/*explorer dziwne rzeczy plecie*/}
			that.initNow();
		}
	}
};

/** wystarczy wywolac jedno z ponizszych */

/** inicjalizacja przy onload (po zaladowaniu wszsystkich miniatur) */
pups.initLoad();

/** inicjalizacja natychmiastowa. MUSI byc wykonana w body ZA wszystkimi linkami, na ktore ma dzialac */
pups.initNow();
