function LajtBoks()
{
  this.imageArray = new Array();
  this.activeImage = null;
  this.doc = document;
  this.ie7 = (this.ie && window.XMLHttpRequest);
  var anchors = document.getElementsByTagName('a');
  for (var i = 0; i < anchors.length; i++)
  {
    var relAttribute = String(anchors[i].getAttribute('rel'));
    if (anchors[i].getAttribute('href'))
    {
      if (relAttribute.toLowerCase().match('lajtboks'))
      {
        anchors[i].onclick = function ()
        {
          mojLajtboks.start(this);
          return false;
        }
      }
    }
  }
  if (this.doc.getElementById('lbWarstwa'))
  {
    this.doc.getElementsByTagName("body").item(0).removeChild(this.doc.getElementById("lbWarstwa"));
    this.doc.getElementsByTagName("body").item(0).removeChild(this.doc.getElementById("lbGlowna"));
  }
  var objWarstwa = this.doc.createElement("div");
  objWarstwa.setAttribute('id','lbWarstwa');
  if ((this.ie && !this.ie7) || (this.ie7 && this.doc.compatMode == 'BackCompat'))
  {
    objWarstwa.style.position = 'absolute';
  }
  objWarstwa.style.display = 'none';
  this.doc.getElementsByTagName("body").item(0).appendChild(objWarstwa);
  var objLajtboks = this.doc.createElement("div");
  objLajtboks.setAttribute('id','lbGlowna');
  objLajtboks.style.display = 'none';
  this.doc.getElementsByTagName("body").item(0).appendChild(objLajtboks);
  var objKontenerZewn = this.doc.createElement("div");
  objKontenerZewn.setAttribute('id','lbKontenerZewn');
  objLajtboks.appendChild(objKontenerZewn);
  var objLajtboksObraz = this.doc.createElement("img");
  objLajtboksObraz.setAttribute('id','lbObraz');
  objKontenerZewn.appendChild(objLajtboksObraz);
  var objLadowanie = this.doc.createElement("div");
  objLadowanie.setAttribute('id','lbLadowanie');
  objKontenerZewn.appendChild(objLadowanie);
  var objKontenerOpisy = this.doc.createElement("div");
  objKontenerOpisy.setAttribute('id','lbKontenerOpisy');
  objLajtboks.appendChild(objKontenerOpisy);
  var objPoprzedni = this.doc.createElement("a");
  objPoprzedni.setAttribute('id','lbPoprzedni');
  objPoprzedni.setAttribute('href','#');
  objKontenerOpisy.appendChild(objPoprzedni);
  var objNastepny = this.doc.createElement("a");
  objNastepny.setAttribute('id','lbNastepny');
  objNastepny.setAttribute('href','#');
  objKontenerOpisy.appendChild(objNastepny);
  var objPodpis = this.doc.createElement("p");
  objPodpis.setAttribute('id','lbPodpis');
  objKontenerOpisy.appendChild(objPodpis);
  var objZamknij = this.doc.createElement("a");
  objZamknij.setAttribute('id','lbZamknij');
  objZamknij.setAttribute('href','#');
  objKontenerOpisy.appendChild(objZamknij);
};
LajtBoks.prototype.start = function(imageLink)
{
  var pageSize = this.getPageSize();
  this.doc.getElementById('lbWarstwa').style.height = pageSize[1] + "px";
  this.doc.getElementById('lbWarstwa').style.opacity = 0.8;
  this.doc.getElementById('lbWarstwa').style.MozOpacity = 0.8;
  this.doc.getElementById('lbWarstwa').style.KhtmlOpacity = 0.8;
  this.doc.getElementById('lbWarstwa').style.filter = "alpha(opacity=90)";
  this.doc.getElementById('lbWarstwa').style.display = '';
  this.imageArray = [];
  this.imageNum = 0;
  var anchors = document.getElementsByTagName('a');
  if (imageLink.getAttribute('rel').indexOf('lajtboks') != -1)
  {
    for (var i = 0; i < anchors.length; i++)
    {
      if (anchors[i].getAttribute('href') && (anchors[i].getAttribute('rel') == imageLink.getAttribute('rel')))
      {
        this.imageArray.push(new Array(anchors[i].getAttribute('href'), anchors[i].getAttribute('title')));
      }
    }
    while(this.imageArray[this.imageNum][0] != imageLink.getAttribute('href'))
    {
      this.imageNum++;
    }
  }
  this.doc.getElementById('lbGlowna').style.top = (this.getPageScroll() + pageSize[3] / 10) + "px";
  this.doc.getElementById('lbGlowna').style.display = '';
  this.doc.getElementById('lbWarstwa').onclick = function()
  {
    mojLajtboks.doc.getElementById('lbGlowna').style.display = 'none';
    mojLajtboks.doc.getElementById('lbWarstwa').style.display = 'none';
    return false;
  }
  this.doc.getElementById('lbGlowna').onclick = function()
  {
    var id = (window.event.target ? window.event.target.id : window.event.srcElement.id);
    if (id == 'lbGlowna')
    {
      mojLajtboks.doc.getElementById('lbGlowna').style.display = 'none';
      mojLajtboks.doc.getElementById('lbWarstwa').style.display = 'none';
      return false;
    }
  }
  this.doc.getElementById('lbObraz').onclick = function()
  {
    mojLajtboks.doc.getElementById('lbGlowna').style.display = 'none';
    mojLajtboks.doc.getElementById('lbWarstwa').style.display = 'none';
    return false;
  }
  this.doc.getElementById('lbZamknij').onclick = function()
  {
    mojLajtboks.doc.getElementById('lbGlowna').style.display = 'none';
    mojLajtboks.doc.getElementById('lbWarstwa').style.display = 'none';
    return false;
  }
  this.changeContent(this.imageNum);
};
LajtBoks.prototype.changeContent = function(imageNum)
{
  this.activeImage = imageNum;
  this.doc.getElementById('lbLadowanie').style.display = '';
  this.doc.getElementById('lbObraz').style.display = 'none';
  this.doc.getElementById('lbKontenerOpisy').style.display = '';
  this.doc.getElementById('lbPoprzedni').style.display = '';
  this.doc.getElementById('lbNastepny').style.display = '';
  this.doc.getElementById('lbZamknij').style.display = '';
  this.doc.getElementById('lbPodpis').style.display = '';
  this.doc.getElementById('lbPodpis').innerHTML = mojLajtboks.imageArray[this.activeImage][1] + "<span> (" + (this.activeImage + 1) + "/" + mojLajtboks.imageArray.length + ")</span>";
  imgPreloader = new Image();
  imgPreloader.onload = function()
  {
    var imageWidth = imgPreloader.width;
    var imageHeight = imgPreloader.height;
    var lbObraz = mojLajtboks.doc.getElementById('lbObraz');
    lbObraz.src = (mojLajtboks.imageArray[mojLajtboks.activeImage][0]);
    lbObraz.width = imageWidth;
    lbObraz.height = imageHeight;
    mojLajtboks.doc.getElementById('lbLadowanie').style.display = 'none';
    mojLajtboks.doc.getElementById('lbObraz').style.display = '';
    mojLajtboks.doc.getElementById('lbKontenerOpisy').style.width = imageWidth + "px";
    mojLajtboks.doc.getElementById('lbNastepny').onclick = function()
    {
      if ((mojLajtboks.imageArray.length - 1) > mojLajtboks.activeImage)
      {
        mojLajtboks.changeContent(mojLajtboks.activeImage + 1);
      }
      else
      {
        mojLajtboks.changeContent(0);
      }
      return false;
    }
    mojLajtboks.doc.getElementById('lbPoprzedni').onclick = function()
    {
      if (mojLajtboks.activeImage > 0)
      {
        mojLajtboks.changeContent(mojLajtboks.activeImage - 1);
      }
      else
      {
        mojLajtboks.changeContent(mojLajtboks.imageArray.length - 1);
      }
      return false;
    }
    imgPreloader.onload = function() {};
  }
  imgPreloader.src = (this.imageArray[this.activeImage][0]);
};
LajtBoks.prototype.getPageScroll = function()
{
  if (self.pageYOffset)
  {
    return this.isFrame ? parent.pageYOffset : self.pageYOffset;
  }
  else if (this.doc.documentElement && this.doc.documentElement.scrollTop)
  {
    return this.doc.documentElement.scrollTop;
  }
  else if (document.body)
  {
    return this.doc.body.scrollTop;
  }
};
LajtBoks.prototype.getPageSize = function()
{
  var xScroll, yScroll, windowWidth, windowHeight;
  if (window.innerHeight && window.scrollMaxY)
  {
    xScroll = this.doc.scrollWidth;
    yScroll = (self.innerHeight) + (self.scrollMaxY);
  }
  else if (this.doc.body.scrollHeight > this.doc.body.offsetHeight)
  {
    xScroll = this.doc.body.scrollWidth;
    yScroll = this.doc.body.scrollHeight;
  }
  else
  {
    xScroll = this.doc.getElementsByTagName("html").item(0).offsetWidth;
    yScroll = this.doc.getElementsByTagName("html").item(0).offsetHeight;
    xScroll = (xScroll < this.doc.body.offsetWidth) ? this.doc.body.offsetWidth : xScroll;
    yScroll = (yScroll < this.doc.body.offsetHeight) ? this.doc.body.offsetHeight : yScroll;
  }
  if (self.innerHeight)
  {
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  {
    windowWidth = this.doc.documentElement.clientWidth;
    windowHeight = this.doc.documentElement.clientHeight;
  }
  else if (document.body)
  {
    windowWidth = this.doc.getElementsByTagName("html").item(0).clientWidth;
    windowHeight = this.doc.getElementsByTagName("html").item(0).clientHeight;
    windowWidth = (windowWidth == 0) ? this.doc.body.clientWidth : windowWidth;
    windowHeight = (windowHeight == 0) ? this.doc.body.clientHeight : windowHeight;
  }
  var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
  var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
  return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
};
if (window.addEventListener)
{
  window.addEventListener("load", initLajtboks, false);
}
else if (window.attachEvent)
{
  window.attachEvent("onload", initLajtboks);
}
else
{
  window.onload = function()
  {
    initLajtboks();
  }
}
function initLajtboks()
{
  mojLajtboks = new LajtBoks();
}
