var ie,
    ie6,
    o,
    id,
    pokaz=false,
    stronazal=false,
    work=false,
    dodwym=new Array(0,0,0),
    zdjdane=new Object(),
    isloaded=false,
    maxdim,
    scrollpos,
    windowh,
    imageh=0,
    sto,
    sint,
    zdjecie;

zdjdane.tytul=new Array();
zdjdane.url=new Array();
zdjdane.szer=new Array();
zdjdane.wys=new Array();

 // Dodatkowe wymiary okna zdjęcia w px (ramki i białe pole warstwy photos_bg).

dodwym[0]=20; // po prawej i lewej stronie zdjęcia
dodwym[1]=79; // nad i pod zdjęciem
dodwym[2]=3; // ramka

 // Funkcja ładująca elementy JavaScript.

function preloadphotos() {
 var j=0, element, iebug="";
 work=true;
 zdjecia=document.getElementsByTagName("a");
 for(i=0;i<zdjecia.length;i++) {
  if(zdjecia[i].getAttribute("rel")=="newlayer") {
   if(zdjecia[i].firstChild.nodeType!=3) zdjdane.tytul[j]=zdjecia[i].firstChild.getAttribute("title");
    else zdjdane.tytul[j]=zdjecia[i].firstChild.nodeValue;
   zdjdane.url[j]=zdjecia[i].getAttribute("href");
   zdjecia[i].removeAttribute("href");
   zdjecia[i].removeAttribute("target");
   if(zdjecia[i].firstChild.nodeType!=3) zdjecia[i].innerHTML=zdjecia[i].innerHTML.replace(/>$/gi," onclick=\"showphoto("+j+")\">");
    else zdjecia[i].innerHTML="<font onclick=\"showphoto("+j+")\">"+zdjecia[i].innerHTML+"</font>";
   zdjecia[i].style.cursor="pointer";
   j++;
  }
 }
 if(document.images) {
  getgraphics(sciezka+"pliki/photos_close.gif");
  getgraphics(sciezka+"pliki/photos_fog.gif");
  getgraphics(sciezka+"pliki/photos_glass.gif");
  getgraphics(sciezka+"pliki/photos_left.gif");
  getgraphics(sciezka+"pliki/photos_loading.gif");
  getgraphics(sciezka+"pliki/photos_right.gif");
 }
 if(!document.getElementById("photos_div")) {
  if(ie) iebug="ie";
  element=document.createElement("div");
  element.setAttribute("id","photos_div");
  document.body.insertBefore(element,document.body.firstChild);
  document.getElementById("photos_div").innerHTML="<div id=\"photos_fog"+iebug+"\" onclick=\"showphoto('off')\">&nbsp;</div><div id=\"photos_load\" onclick=\"showphoto('off')\">&nbsp;</div><div id=\"photos_bg\"><div id=\"photos_head\"><div id=\"photos_esc\" onclick=\"showphoto('off')\"></div><div id=\"photos_desc\"></div></div><div id=\"photos_navi\"><div id=\"photos_navil_1\"><div id=\"photos_navil_2\" onmouseover=\"changephoto('l','i')\" onmouseout=\"changephoto('l','o')\" onclick=\"changephoto('l','c')\"></div></div><div id=\"photos_navir_1\"><div id=\"photos_navir_2\" onmouseover=\"changephoto('r','i')\" onmouseout=\"changephoto('r','o')\" onclick=\"changephoto('r','c')\"></div></div><img id=\"photo\" src=\"\" width=\"0\" alt=\"\" onload=\"isloaded=true\"></div><div id=\"photos_count\"></div></div>";
 }
 stronazal=true;
 work=false;
}

 // Funkcje do wyświetlania grafik.

function photos(pid) {
 var pageheight, iebug="";
 if(stronazal && work) {
  if(pid.toString().match(/^[0-9]+$/)) {
   id=pid;
   isloaded=false;
   if(pokaz) {
    document.getElementById("photos_bg").style.visibility="hidden";
    document.getElementById("photos_desc").innerHTML="";
    document.getElementById("photo").src="";
   }
   if(ie) iebug="ie";
   maxdim=getwindowdim();
   windowh=maxdim[1];
   pageheight=getpagedim();
   scrollpos=getscrollpos();
   document.getElementById("photos_load").style.top=Math.round(scrollpos+0.5*(windowh-30))+"px";
   document.getElementById("photos_div").style.height=pageheight+"px";
   document.getElementById("photos_fog"+iebug).style.height=pageheight+"px";
   if(!pokaz) document.getElementById("photos_div").style.visibility="visible";
   if(zdjdane.tytul[id]) document.getElementById("photos_desc").innerHTML=zdjdane.tytul[id];
   document.getElementById("photos_count").innerHTML="Zdjęcie "+(id+1)+" z "+zdjdane.url.length;
   if(!zdjdane.szer[id]) {
    zdjecie=getgraphics(zdjdane.url[id]);
    zdjdane.szer[id]=zdjecie.width;
    zdjdane.wys[id]=zdjecie.height;
    if(zdjdane.szer[id]==0 || zdjdane.wys[id]==0) {
     sto=0;
     sint=setInterval("getimagedim()",100);
    }
    else zdjecie.onload=printphoto();
   }
   else printphoto();
  }
  else if(pokaz) {
   id=false;
   document.getElementById("photos_div").style.visibility="hidden";
   document.getElementById("photos_bg").style.visibility="hidden";
   document.getElementById("photos_desc").innerHTML="";
   document.getElementById("photo").src="";
   imageh=0;
   pokaz=false;
   work=false;
  }
 }
}
function printphoto() {
 setimagedim(false);
 document.getElementById("photo").src=zdjdane.url[id];
 document.getElementById("photos_navil_1").style.backgroundPosition="left "+(Math.floor(imageh/2)-25)+"px";
 document.getElementById("photos_navir_1").style.backgroundPosition="right "+(Math.floor(imageh/2)-25)+"px";
 if(isloaded) {
  document.getElementById("photos_bg").style.visibility="visible";
  pokaz=true;
  work=false;
 }
 else {
  sto=0;
  sint=setInterval("loadphoto()",100);
 }
}
function loadphoto() {
 if(isloaded) {
  clearInterval(sint);
  document.getElementById("photos_bg").style.visibility="visible";
  pokaz=true;
  work=false;
 }
 else if(sto>=100) {
  clearInterval(sint);
  document.getElementById("photos_div").style.visibility="hidden";
  pokaz=false;
  work=false;
 }
 sto++;
}

 // Funkcje do zarządzania ładowaniem grafik.

function showphoto(pid) {
 if(stronazal && !work) {
  work=true;
  photos(pid);
 }
}
function changephoto(zwrot,tryb) {
 if(!work) {
  work=true;
  if(zwrot!="l") zwrot="r";
  if(tryb=="i") {
   if(zwrot=="l" && id>0) {
    document.getElementById("photos_navil_1").style.background="url("+sciezka+"pliki/photos_left.gif) left "+(Math.floor(imageh/2)-25)+"px no-repeat";
    document.getElementById("photos_navir_1").style.background="";
   }
   else if(zwrot=="r" && (id+1)<zdjdane.url.length) {
    document.getElementById("photos_navil_1").style.background="";
    document.getElementById("photos_navir_1").style.background="url("+sciezka+"pliki/photos_right.gif) right "+(Math.floor(imageh/2)-25)+"px no-repeat";
   }
   work=false;
  }
  else if(tryb=="o") {
   document.getElementById("photos_navi"+zwrot+"_1").style.background="";
   work=false;
  }
  else if(tryb=="c" && ((zwrot=="l" && id>0) || (zwrot=="r" && (id+1)<zdjdane.url.length))) {
   if((zwrot=="l" && id==1) || (zwrot=="r" && (id+2)==zdjdane.url.length)) document.getElementById("photos_navi"+zwrot+"_1").style.background="";
    else document.getElementById("photos_navi"+zwrot+"_1").style.background="url("+sciezka+"pliki/photos_"+(zwrot=="r"?"right":"left")+".gif) "+(zwrot=="r"?"right":"left")+" "+(Math.floor(imageh/2)-25)+"px no-repeat";
   document.getElementById("photos_navi"+(zwrot=="r"?"l":"r")+"_1").style.background="";
   if(zwrot=="l") photos(id-1);
    else photos(id+1);
  }
  else work=false;
 }
}

 // Funkcja do skalowania grafiki.

function setimagedim(isresize) {
 var lastdim=new Array(zdjdane.szer[id],zdjdane.wys[id]), pageheight, iebug="", toppos=0;
 if(ie) iebug="ie";
 if(!isresize) {
  maxdim[0]-=50+dodwym[0]+2*dodwym[2];
  maxdim[1]-=30+dodwym[1]+2*dodwym[2];
  if(maxdim[0]<400) maxdim[0]=400;
  if(maxdim[1]<300) maxdim[1]=300;
  if((lastdim[0]/lastdim[1])>(maxdim[0]/maxdim[1])) {
   if(lastdim[0]>maxdim[0]) {
    lastdim[1]=Math.round(maxdim[0]*lastdim[1]/lastdim[0]);
    lastdim[0]=maxdim[0];
   }
  }
  else {
   if(lastdim[1]>maxdim[1]) {
    lastdim[0]=Math.round(lastdim[0]*maxdim[1]/lastdim[1]);
    lastdim[1]=maxdim[1];
   }
  }
  document.getElementById("photo").width=lastdim[0];
  document.getElementById("photo").height=lastdim[1];
  document.getElementById("photos_bg").style.width=(lastdim[0]+dodwym[0])+"px";
  document.getElementById("photos_navil_1").style.height=lastdim[1]+"px";
  document.getElementById("photos_navir_1").style.height=lastdim[1]+"px";
  document.getElementById("photos_navil_2").style.height=lastdim[1]+"px";
  document.getElementById("photos_navir_2").style.height=lastdim[1]+"px";
  imageh=lastdim[1];
 }
 else {
  maxdim=getwindowdim();
  windowh=maxdim[1];
  pageheight=getpagedim();
  scrollpos=getscrollpos();
  document.getElementById("photos_load").style.top=Math.round(scrollpos+0.5*(windowh-30))+"px";
  document.getElementById("photos_div").style.height=pageheight+"px";
  document.getElementById("photos_fog"+iebug).style.height=pageheight+"px";
 }
 toppos=Math.round(scrollpos+0.5*(windowh-imageh-dodwym[1]-2*dodwym[2]));
 if(toppos<15) toppos=15;
  else if(toppos>(pageheight-imageh-dodwym[1]-2*dodwym[2]-15)) toppos=pageheight-imageh-dodwym[1]-2*dodwym[2]-15;
 document.getElementById("photos_bg").style.top=toppos+"px";
 if(isresize) work=false;
}

 // Funkcje zwracające wymiary okna, strony, obrazka i pozycję scrollowania.

function getwindowdim() {
 var windowwidth=0, windowheight=0;
 if(typeof(window.innerWidth)=="number") {
  windowwidth=window.innerWidth;
  windowheight=window.innerHeight;
 }
 else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
  windowwidth=document.documentElement.clientWidth;
  windowheight=document.documentElement.clientHeight;
 }
 else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
  windowwidth=document.body.clientWidth;
  windowheight=document.body.clientHeight;
 }
 return new Array(windowwidth,windowheight);
}
function getpagedim() {
 var pageh;
 if(typeof(document.height)=="number") pageh=document.height;
  else if(document.body && document.body.scrollHeight) pageh=document.body.scrollHeight;
 if(pageh>=windowh) return pageh;
  else return windowh;
}
function getimagedim() {
 zdjdane.szer[id]=zdjecie.width;
 zdjdane.wys[id]=zdjecie.height;
 if(zdjdane.szer[id]>0 && zdjdane.wys[id]>0) {
  clearInterval(sint);
  zdjecie.onload=printphoto();
 }
 else if(sto>=100) {
  clearInterval(sint);
  document.getElementById("photos_div").style.visibility="hidden";
  pokaz=false;
  work=false;
 }
 sto++;
}
function getscrollpos() {
 if(typeof(window.pageYOffset)=="number") return window.pageYOffset;
  else if(document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
  else if(document.body && document.body.scrollTop) return document.body.scrollTop;
  else return 0;
}

 // Funkcja zwracająca informacje o przeglądarce.

function getbrowser() {
 var przegladarka=navigator.userAgent.toLowerCase();
 if(przegladarka.indexOf(" msie ")>0 || przegladarka.indexOf("opera")>0) {
  ie=true;
  o=true;
  if(parseFloat(przegladarka.replace(/^.+ msie ([0-9\.]+);.+$/gi,"$1"))<7) ie6=true;
 }
}
getbrowser();

 // Funkcja do zmiany ustawień w przypadku przeskalowania okna.

function windowresizing() {
 if(pokaz && !work) {
  work=true;
  setimagedim(true);
 }
}

 // Instrukcja inicjacji funkcji przy zmianie wymiarów okna.

if(window.attachEvent) window.attachEvent("onresize",windowresizing);
 else window.onresize=windowresizing;
