$=function(a){return document.getElementById(a)}
pad=30;bord=10;gora=40;dol=35;done=1;


var zdjecia=[], curPic;
function initGallery(){
	if(!document.getElementsByTagName)return
	for(var i=0;ul=document.getElementsByTagName('ul')[i++];){
		if(ul.className=='galeria'){
			for(j=0;a=ul.getElementsByTagName('a')[j++];){
				a.onclick=showPicture;
				zdjecia.push(a);
			}
		}
	}
}



function showPicture(a,li,img){
	if($('bg'))removeGal();
	if(!done)return!1;
		done=0;
	var a=this,li=a.parentNode,img=a.getElementsByTagName('img')[0];
	for(curPic=-1;a!=zdjecia[++curPic];);

	now=[1,1]
	p=li;do{now[0]+=p.offsetLeft;now[1]+=p.offsetTop/*;alert(now+'\n'+p.tagName)*/}while(p=p.offsetParent);
	now[2]=155; now[3]=155;
	dest=doDest(img);

	bg=document.createElement('div');
		bg.id='bg';
		bg.style.top=dest[1]+'px';
		bg.style.height=wymOkna[1]+'px';
	div=document.createElement('div');
		div.id="ramka";
		div.style.left=now[0]+'px';
		div.style.top=now[1]+'px';
		div.style.width=now[2]+'px';
		div.style.height=now[3]+'px';
		div.style.backgroundImage="url('"+img.src+"')";
	imgD=document.createElement('img');
		imgD.src=a.href//+'a';
	prev=document.createElement('div');
		prev.id="prev";
		prev.appendChild(document.createTextNode('\xAB poprzednie'));
		prev.onclick=function(){changePicture(div,-1,next,prev)};
		if(curPic==0)prev.className='disabled';
	next=document.createElement('div');
		next.id="next";
		next.appendChild(document.createTextNode('nastêpne \xBB'));
		next.onclick=function(){changePicture(div,1,next,prev)};
		if(curPic==zdjecia.length-1)next.className='disabled';
	x=document.createElement('div');
		x.id='x';
		x.appendChild(document.createTextNode('x'));
		x.title="zamknij galeriê";
		x.onclick=removeGal;
	document.body.appendChild(bg);
	document.body.appendChild(div);

	dest[1]+=gora;

	resizeImage(div,now,dest,800,function(){
		imgD.style.width=dest[2]+'px';
		imgD.style.height=dest[3]+'px';
		div.appendChild(imgD)
		setTimeout(function(){
			div.appendChild(next);
			div.appendChild(prev);
			resizeImage(div,dest,[dest[0],dest[1],dest[2],dest[3]+dol],800,function(){
				prev.style.position=next.style.position='absolute';
				div.insertBefore(x,prev);
				done=1;
			})
		},500)
	})



	return false;
}

function resizeImage(a,b,c,k,f){
	start=+new Date();
	iVal=setInterval(function(){
		teraz=+new Date(); pos=cos((teraz-start)/k);
		if(teraz>=start+k||b[3]==c[3]){
			iVal=clearInterval(iVal);
			f?f():'';
		}else{
			a.style.left=b[0]+pos*(c[0]-b[0])+'px';
			a.style.top=b[1]+pos*(c[1]-b[1])+'px';
			a.style.width=b[2]+pos*(c[2]-b[2])+'px';
			a.style.height=b[3]+pos*(c[3]-b[3])+'px';
		}
	},13)
}

function changePicture(div,d,n,p){
	if(!done)return!1;
		done=0;
	a=zdjecia[curPic+=d];
	img=a.getElementsByTagName('img')[0]; imgD=div.getElementsByTagName('img')[0];
		if(!zdjecia[curPic+1]){n.className='disabled'}
		else if(n.className=='disabled'){n.className=''}
		if(!zdjecia[curPic-1]){p.className='disabled'}
		else if(p.className=='disabled'){p.className=''}

	now=[div.offsetLeft,div.offsetTop,div.offsetWidth-pad-bord,div.offsetHeight-pad-bord];
	dest=doDest(img,dol); dest[1]+=gora;
	if(now[2]!=dest[2]){
		div.removeChild(imgD);
		imgD=document.createElement('img');
		imgD.style.display='none';
		div.appendChild(imgD);
	}
	div.style.backgroundImage="url('"+img.src+"')";
	resizeImage(div,now,dest,800,function(){
		imgD.style.width=dest[2]+'px';
		imgD.style.height=dest[3]-dol+'px';
		imgD.src=a.href;
		imgD.style.display='block';
		done=1;
	})
}

function doDest(img,md){
	wymOkna=[	self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,
				self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight
			]
	return 	[	(wymOkna[0]-(img.width>img.height?640:480)+pad+bord)/2,
				(self.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop),
				img.width>img.height?640:480,
				(img.width<img.height?640:480)+(md||0)
			]
}

function removeGal(){
	if(!done)return!1;
	document.body.removeChild($('bg'));
	document.body.removeChild($('ramka'));
}

function cos(a){
	return ((-Math.cos(a*Math.PI)/2)+0.5);
}

;(function(){
	if($('podmenu')){initGallery(); return!0}
	setTimeout(arguments.callee,100)
})();
