var top,czas,czas2,startpos,agent;

function wczytaj_fotke(width,height,fotka,opis)
{

   if(fota.complete)
   {
   clearInterval(czas2);

   document.getElementById("fota_img").style.display="none";
   document.getElementById("fota_img").src="";
   document.getElementById("fota_img").style.margin="0";
   document.getElementById("fota_img").style.width=width+"px";
   if(agent==1) document.getElementById("fotka").style.height=height+"px";
   document.getElementById("fotka").style.minHeight=height+"px";
   document.getElementById("fota_img").src=fota.src;
   document.getElementById("fota_img").style.display="block";
   if(opis!="") document.getElementById("opis_fotki").style.display="block";
   }
}

function fotka(width,height,fotka,opis)
{
zamknij("fotka");

opis=opis.replace(/\s[0-9]{1,}x[0-9]{1,}/g,"");

if(navigator.userAgent.indexOf("Gecko")!=-1) agent=0;
else if(navigator.userAgent.indexOf("Opera")!=-1) agent=2;
else agent=1;

var offset=100;
var mnoznik;

if(width>(document.documentElement.clientWidth-offset) || height>(document.documentElement.clientHeight-offset))
{
   if(width>(document.documentElement.clientWidth-offset))
   {
   mnoznik=(((document.documentElement.clientWidth-offset)*100)/width)/100;
   width=Math.round(width*mnoznik);
   height=Math.round(height*mnoznik);
   }

   if(height>(document.documentElement.clientHeight-offset))
   {
   mnoznik=(((document.documentElement.clientHeight-offset)*100)/height)/100;
   height=Math.round(height*mnoznik);
   width=Math.round(width*mnoznik);
   }
}

var h,w,h_img,w_img;

if(opis.indexOf(fotka.substring(fotka.lastIndexOf("/")+1,fotka.length))!=-1) opis="";

top=document.documentElement.scrollTop;
czas=setInterval("zmien_pozycje('fotka')",500);

if(agent!=2) h=document.documentElement.scrollTop+((document.documentElement.clientHeight-height)/2);
else h=document.documentElement.scrollTop+((document.body.clientHeight-height)/2);
w=(document.documentElement.clientWidth-width)/2;

//alert(document.body.clientHeight);

h_img=(height-32)/2;
w_img=(width-32)/2;

document.getElementById("fotka").style.top=h+"px";
document.getElementById("fotka").style.left=w+"px";
document.getElementById("fotka").style.width=width+"px";
if(agent==1) document.getElementById("fotka").style.height=height+"px";
document.getElementById("fotka").style.minHeight=height+"px";
document.getElementById("fotka").innerHTML="<img id=\"fota_img\" src=\"gfx/loading100.gif\" alt=\"\" style=\"margin:"+h_img+"px 0 0 "+w_img+"px;\" /><a href=\"javascript:zamknij('fotka');\"><img id=\"close\" src=\"gfx/close.gif\" alt=\"zamknij\" title=\"zamknij\" /></a><p id=\"opis_fotki\" class=\"opis_fotki\">"+opis+"</p>";
document.getElementById("fotka").style.display="block";

fota=new Image();
fota.src=fotka;
opis=opis.replace("'","\\'");
czas2=setInterval("wczytaj_fotke('"+width+"','"+height+"','"+fotka+"','"+opis+"')",500);
}

function zamknij(element)
{
clearInterval(czas);
document.getElementById(element).style.display="none";
}

function zmien_pozycje(element)
{
var ust_stare;
var ust_nowe;
var czas2;

   if(top!=document.documentElement.scrollTop)
   {

      ust_stare=parseInt(document.getElementById(element).style.top);
      ust_nowe=(ust_stare+(-(top-document.documentElement.scrollTop)));

      while(ust_stare!=ust_nowe)
      {
         if(ust_stare<ust_nowe)
         {

         document.getElementById(element).style.top=(ust_stare+1)+"px";
         ust_stare=ust_stare+1;
         }
         else
         {
         document.getElementById(element).style.top=(ust_stare-1)+"px";
         ust_stare=ust_stare-1;
         }
      }

      top=document.documentElement.scrollTop;
   }
}

function foto_linki()
{
   var i;
   var linki=document.getElementsByTagName('a');

   for(i=0;i<linki.length;i++)
   {
      if(linki[i].className=='fotka')
      {
      var link=linki[i];
      link.onclick=function(){fotka(this.title.match(/\s[0-9]{1,}/g)[0],this.title.match(/[0-9]{1,}$/g)[0],this.href,this.title);return false;}
      }
   }
}
