var top,czas,czas2,startpos,agent,start_height;
var czas;
var fota;
var current_foto=0;
var current_popup_foto="";

function load_foto(width,height,file,opis)
{

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

   var parent_box=document.getElementById("fotka");

   for(i=0;i<parent_box.childNodes.length;i++)
   {
	if(parent_box.childNodes[i].tagName=="IMG") parent_box.removeChild(parent_box.childNodes[i]);
   }

	var img = document.createElement("img");
	img.setAttribute("src", file);
	img.setAttribute("alt", opis);
	parent_box.appendChild(img);
	
	var img_close = document.createElement("img");
	img_close.setAttribute("src", "./gfx/close.gif");
	img_close.setAttribute("alt", opis);
	img_close.setAttribute("id", "close");
	var img_close_ref = parent_box.appendChild(img_close);
	
	img_close_ref.onclick=function(){close_all();}
	
	var p = document.createElement("p");
	if(agent>0)
	{
	parent_box.style.height="auto";
	p.className="opis_fotki";
	}
	else p.setAttribute("class","opis_fotki");
	
	var p_ref = parent_box.appendChild(p);	
	
	var prev_foto=current_popup_foto-1;
	if(fotki[prev_foto])
	{
	var img_prev = document.createElement("img");
	img_prev.setAttribute("src", "./gfx/serwis/foto_prev.gif");
	img_prev.setAttribute("alt", "");
	if(agent>0) img_prev.className="prev";
	else img_prev.setAttribute("class", "prev");
	var img_prev_ref=parent_box.appendChild(img_prev);
	img_prev_ref.onclick=function(){setCurrentPopupValue(-1);fotka(fotki[prev_foto][2],fotki[prev_foto][3],'./img/' + fotki[prev_foto][4],fotki[prev_foto][1]);return false;}
	}

	var next_foto=current_popup_foto+1;
	if(fotki[next_foto])
	{	
	var img_next = document.createElement("img");
	img_next.setAttribute("src", "./gfx/serwis/foto_next.gif");
	img_next.setAttribute("alt", "");
	if(agent>0) img_next.className="next";
	else img_next.setAttribute("class", "next");
	var img_next_ref=parent_box.appendChild(img_next);	
	img_next_ref.onclick=function(){setCurrentPopupValue(1);fotka(fotki[next_foto][2],fotki[next_foto][3],'./img/' + fotki[next_foto][4],fotki[next_foto][1]);return false;}
	}
	
	if(opis!="")	
	{
	if(agent>0) p_ref.innerText=opis;
	else p_ref.textContent=opis;		
	}
   }
}

function fotka(width,height,fotka,opis)
{
	if(current_popup_foto==="") current_popup_foto=current_foto;
close_fotka();

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

if(parseInt(height)+parseInt(document.documentElement.scrollTop)+20>parseInt(document.getElementById('main').offsetHeight)) 
{
start_height=parseInt(document.getElementById('main').offsetHeight);
document.getElementById('main').style.height=parseInt(height)+parseInt(document.documentElement.scrollTop)+100+"px";
}

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

var offset=100;
var mnoznik;

var client_height;

if(agent==0) client_height=self.innerHeight;
else client_height=document.documentElement.offsetHeight;


var h,w,h_img,w_img;

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

top=document.documentElement.scrollTop;

if(height>client_height-10) h=document.documentElement.scrollTop+20;
else h=document.documentElement.scrollTop+((client_height-height)/2);

w=((document.documentElement.clientWidth-width)/2)-5;
h_img=(height-32)/2;
w_img=(width-32)/2;


var fotka_box = document.createElement("div");
var fotka_box_content = document.createTextNode("");

 if(agent>0)
 {
 fotka_box.id="fotka";
 fotka_box.style.top=h+"px";
 fotka_box.style.left=w+"px";
 fotka_box.style.height=height+"px";
 fotka_box.style.width=width+"px";
 }
 else
 {
 fotka_box.setAttribute("id", "fotka");
 fotka_box.setAttribute("style","top:" + h + "px;left:" + w + "px;min-height:" + height + "px;width:" + width + "px;");
 }

fota=new Image();
fota.src=fotka;
	
fotka_box.appendChild(fotka_box_content);

var main_box = document.getElementById("main");
var parentDiv = main_box.parentNode;

var fotka_box_ref=parentDiv.insertBefore(fotka_box, main_box);
setBackgroundGray();

if(fota.complete==false) 
{
	var preloader_img = document.createElement("img");
	preloader_img.setAttribute("src", "./gfx/loading100.gif");
	preloader_img.setAttribute("alt", "");
	
	if(agent>0)
	{
	preloader_img.style.position="relative";
	preloader_img.style.top=h_img + "px";
	preloader_img.style.left=w_img + "px";
	}
	else 
	{
	preloader_img.setAttribute("style","position:relative;top:" + h_img + "px;left:" + w_img + "px;");
	}
	fotka_box_ref.appendChild(preloader_img);
}

opis=opis.replace("'","\\'");
czas2=setInterval("load_foto('"+width+"','"+height+"','"+fotka+"','"+opis+"')",50);
}

function close_fotka()
{
	var child=document.getElementById("fotka");
	if(child)
	{
	clearInterval(czas);
	var parent=child.parentNode;
	if(child) parent.removeChild(child);
	}
}

function close_all()
{
	current_popup_foto="";
	var element="fotka";
	var child=document.getElementById(element);
	
	if(child)
	{
	var parent=child.parentNode;
	
	clearInterval(czas);

	var item=document.getElementById('background');
	if(item) item.parentNode.removeChild(item); 



	if(child) parent.removeChild(child);

	if(agent!=1) 
	{
	document.getElementById('main').removeAttribute("style");
	//document.getElementById('main').style.minHeight=parseInt(start_height)+"px";
	}
	else document.getElementById('main').style.height=parseInt(start_height)+"px";

	}

}

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;}
      }
   }
}


function setFoto(element,foto)
{
if(fota)
{	
   if(fota.complete)
   {
   clearInterval(czas);
   removePreloader();

   var img = document.createElement("img");
   img.setAttribute("src", "./img/" + foto);
   img.setAttribute("alt", "");
   document.getElementById(element).appendChild(img);
   
   img.onclick=function(){fotka(fotki[current_foto][2],fotki[current_foto][3],'./img/' + fotki[current_foto][4],fotki[current_foto][1]);return false;}
   
   }
}   
}


function nextFoto(value)
{
	if(fotki[current_foto+value])
	{
	var current_foto_name=fotki[current_foto+value][0];
	
	removeFoto();
	
	fota=new Image();
	fota.src='./img/' + current_foto_name;
	
	if(fota.complete==false) setPreloader();	
	
	setCurrent(value);

	clearInterval(czas);
	
	if(fota) {czas=setInterval("setFoto('foto_box','" + current_foto_name + "')",50);}
	}
}

function removeFoto()
{
	removePreloader();
	var parent=document.getElementById('foto_box');
	var childs=parent.childNodes;

	for(i=0;i<childs.length;i++)
	{
	parent.removeChild(childs[i]);
	}
}

function setPreloader()
{
	var parent=document.getElementById('foto_box');
	if(document.all) parent.style.background="url(./gfx/loading100.gif) no-repeat 50% 50%;";
	else parent.setAttribute("style","background:url(./gfx/loading100.gif) no-repeat 50% 50%;");
}

function removePreloader()
{
	var parent=document.getElementById('foto_box');
	
	if(document.all) parent.style.background="";
	else parent.removeAttribute("style");
}

function setCurrentPopupValue(value)
{
current_popup_foto=current_popup_foto+value;
}

function setCurrent(value)
{
	current_foto=current_foto+value;
	var parent=document.getElementById('foto_box_nav');
	var child=parent.firstChild;
	
	if(child && child.nodeName=="SPAN")
	{
		var text,tab;
		if(document.all) text=child.innerText;
		else text=child.textContent;
		
		tab=text.split("/");
		text=text.replace(tab[0],current_foto+1);
		
		if(document.all) child.innerText=text;
		else child.textContent=text;
	}
}



function setBackgroundGray()
{
if(!document.getElementById("background"))
{
var height,width;

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

  if(agent==0)
  {	
	if(document.getElementById('main').offsetHeight>self.innerHeight) height=document.getElementById('main').offsetHeight;
	else height=self.innerHeight;
  }
  else
  {
  	if(document.getElementById('main').offsetHeight>document.documentElement.clientHeight) height=document.getElementById('main').offsetHeight;
	else height=document.documentElement.offsetHeight;
  }

 var sp1 = document.createElement("div");
 var sp1_content = document.createTextNode("");
 sp1.setAttribute("id", "background");
 
 
 if(agent>0)
 {
  width=document.documentElement.clientWidth;
	
  sp1.style.position='absolute';
  sp1.style.top='0';
  sp1.style.left='0';
  sp1.style.height=height + 'px';
  sp1.style.width=width + 'px';
  sp1.style.zIndex='100';
  sp1.className="background";
 }
 else
 {
 width=document.documentElement.offsetWidth;
 
 sp1.setAttribute("class", "background");
 sp1.setAttribute("style","position:absolute;top:0;left:0;height:" + height + "px;width:" + width + "px;background:url(gfx/tlo.png);z-index:100;");
 }
 sp1.appendChild(sp1_content);

 var sp2 = document.getElementById("main");
 var parentDiv = sp2.parentNode;

 parentDiv.insertBefore(sp1, sp2);
}   
}

