window.onload = galInit;
var il=0;
var linia=4;
var okno=null;
var aDir=null;
var ilZdj=0;
var img = null;
var div = null;
var link = null;
var link2 = null;
var sp = null;
var dv = null;
var prevImg=null;

function galInit() {
	//if(document.getElementById('i1') != null) {
	il = document.getElementsByTagName('img').length - 1;
	if(il > 0) {
	for(var i=1;i<=il;i++) {
	var ob = document.getElementById('i'+i);
	
		/*if(document.getElementById('i'+i).width>document.getElementById('i'+i).height) {
			document.getElementById('i'+i).style.left=((i-1)%linia)*180+'px';
		} else {
			document.getElementById('i'+i).style.left=((i-1)%linia)*180+35+'px';
			var wsp = (parseInt(document.getElementById('i'+i).style.width)/parseInt(document.getElementById('i'+i).style.height));
			//document.getElementById('i'+i).style.width='180px';
			//document.getElementById('i'+i).style.height = (document.getElementById('i'+i).style.width / wsp) + 'px';
		}
		document.getElementById('i'+i).style.position='relative';
		document.getElementById('i'+i).style.top=parseInt((i-1)/linia)*150+'px';
		document.getElementById('i'+i).style.zIndex=1;
		document.getElementById('i'+i).onmouseover=pokaz;
		*/
		document.getElementById('i'+i).onclick=noweOkno;
	}
		if(il>20) { 
			document.getElementById('cntr').style.height=830 + ((il-20)%4)*170 + 'px';
		}
	}
	//}
}

function pokaz() {
	var left = ((this.id.substring(1, this.id.length)-1)%4)*180;
	var top = parseInt((this.id.substring(1, this.id.length)-1)/linia)*150;
	//this.width=2*this.width;
	this.height=2*this.height;
	this.style.zIndex=2;
	if(this.width>this.height) {
		this.style.left=(left-1/4*this.width)+'px';
	} else {
		this.style.left=(left-1/4*this.width)+35+'px';
	}
	this.style.top=(top-1/4*this.height)+'px';
	document.getElementById(this.id).onmouseout=ukryj;
}

function ukryj() {
	//this.width=1/2*this.width;
	this.height=1/2*this.height;
	this.style.zIndex=1;
	if(this.width>this.height) {
		this.style.left=(((this.id.substring(1, this.id.length)-1)%linia)*180)+'px';
	} else {
		this.style.left=(((this.id.substring(1, this.id.length)-1)%linia)*180+35)+'px';
	}
	this.style.top=(parseInt((this.id.substring(1, this.id.length)-1)/linia)*150)+'px';
}

function noweOkno() {
	//aDir = aZdj.split(";");
	//okno = window.open('img.php','', 'menubar=no, toolbar=no, location=no, scrollbars=no, status=no, resizable=yes,width=' + (0.6*window.innerWidth+15) + ',height=' + ((0.6*window.innerWidth+50)*this.height/this.width+50));
	if(this.width>this.height) {
		okno = window.open('img.php?id=' + this.id + '&cat='+location.search.split('=')[2]+'&ct='+il,'', 'menubar=no, toolbar=no, location=no, scrollbars=no, status=no, resizable=no,width=650,height='+ (this.height*500/this.width+150));
	} else {
		okno = window.open('img.php?id=' + this.id + '&cat='+location.search.split('=')[2]+'&ct='+il,'', 'menubar=no, toolbar=no, location=no, scrollbars=no, status=no, resizable=no,width=500,height='+ (this.height*350/this.width+150));	
	}
	//okno.location.href='img.php';
	okno.screenX=(window.innerWidth-okno.window.innerWidth)/2;
	okno.screenY=(window.innerHeight-okno.window.innerHeight)/2;
	/*
	img = okno.document.createElement('<img>');
	div = okno.document.createElement('<div>');
	link = okno.document.createElement('<a>');
	link2 = okno.document.createElement('<a>');
	sp = okno.document.createElement('<span>');
	dv = okno.document.createElement('<div>');
	okno.document.getElementsByTagName('body')[0].style.textAlign='center';
	okno.document.getElementsByTagName('body')[0].appendChild(link);
	okno.document.getElementsByTagName('body')[0].appendChild(sp);
	okno.document.getElementsByTagName('body')[0].appendChild(link2);
	okno.document.getElementsByTagName('body')[0].appendChild(dv);
	okno.document.getElementsByTagName('body')[0].appendChild(img);
	okno.document.getElementsByTagName('body')[0].appendChild(div);
	dv.innerHTML = this.id.substr(1) + ' z ' + il;
	sp.style.padding='10px';
	sp.innerHTML='|';
	link.innerHTML = "&lt;&lt; poprzednie";
	link.href='javascript:zmien()';
	link.id=0;//parseInt(parseInt(this.id.substr(1))-1);
	link.onclick=zmien;
	link.style.color='#6f7f99';
	link2.innerHTML = "nastepne &gt;&gt;";
	link2.href="javascript:zmien()";
	link2.id=2;//parseInt(parseInt(this.id.substr(1))+1);
	link2.onclick=zmien;
	link2.style.color='#6f7f99';
	div.style.width='100%';
	div.style.textAlign='center';
	div.innerHTML=this.title;
	div.style.marginTop='4px';
	img.src = this.src.replace('miniatury/', '');
	img.height = 0.65*window.innerHeight;
	img.style.padding='10px 0 0 0';
	okno.document.getElementsByTagName('body')[0].style.background='#f0f7ff';
	okno.document.getElementsByTagName('body')[0].style.fontWeight='bold';
	okno.document.getElementsByTagName('body')[0].style.color='#6f7f99';
	*/
	img = document.getElementsByTagName('img')[0].onclick=zamknij;
}

function zmien() {
	//document.write(this.id + ':');
	img.src = document.getElementById('i'+this.id).src.replace('miniatury','.');
	//okno.document.getElementsByTagName('link')[0].id=parseInt(parseInt(this.id)-1);
	//okno.document.getElementsByTagName('link2')[0].id=parseInt(parseInt(this.id)+1);
	div.innerHTML = document.getElementById('i'+this.id).title
	dv.innerHTML=parseInt(this.id) + ' z ' + il;
	link.id=parseInt(parseInt(this.id)-1);
	link2.id=parseInt(parseInt(this.id)+1);
	//dv.innerHTML=this.id;;
	
}

function zamknij() {
	if(okno && !okno.closed) {
		okno.close();
	}
}