// JavaScript Document ------------------------------------------
// (c) 2006 Martin Kovarik, nihi@atlas.cz, http://www.nihique.com
// --------------------------------------------------------------

// Global variables for Photogallery

var galMode = true;
var autoSlide = false;
var slideTimer = null;
var ssArg = false; // condition on ss argument of file
var slideCounter = 0;
var currentSlide = 1;
var thisFile = window.location.pathname; // nazev akt. souboru
thisFile = thisFile.substr(thisFile.lastIndexOf('/') + 1); // nazev akt. souboru

// End of Global variables

window.onload = function()
{
    if (window.winOnLoad) window.winOnLoad();
}

window.onunload = function()
{
    if (window.winOnUnload) window.winOnUnload();
}

function winOnLoad()
{
  xImgGallery();
}

function xImgGallery()
{
  if (document.getElementById && document.getElementById('navigation').style) {
    var n = 1, a = xGetURLArguments();
    if (a.length) {
      var arg = a['g'] || a['s'] || a['ss'];
      if (arg) {
        n = parseInt(arg, 10);
        if (n <= 0 || n > imgsMax) { n = 1; } 
        if (a['s']) { galMode = false; }
				if (a['ss']) { 
					ssArg = true;
				}
      }
    }
    gal_paint(n);
  }
}
function gal_paint(n)
{
  gal_setImgs(n);
  gal_setNav(n);
}
function gal_setImgs(n)
{
  var ssEle = document.getElementById('slideshow');
  var galEle = document.getElementById('gallery');
  var i, imgTitle, pth, anch, img, id, idA, src, ipp, idPrefix, idAPrefix, imgSuffix, imgPrefix;
  var zeros, digits, capEle, capPar;
  if (galMode) {
    ipp = imgsPerPg;
    idPrefix = 'g';
		idAPrefix = 'ga';
    imgPrefix = gPrefix;
    imgSuffix = gSuffix + gExt;
    imgTitle = 'Click to view large image';
    ssEle.style.display = 'none';
    galEle.style.display = 'block';
    pth = gPath;
    zeros = gZeros;
    digits = gDigits;
  }
  else {
    currentSlide = n;
    ipp = 1;
    idPrefix = 's';
    imgPrefix = sPrefix;
    imgSuffix = sSuffix + sExt;
    imgTitle = '';
    ssEle.style.display = 'block';
    galEle.style.display = 'none';
    pth = sPath;
    zeros = sZeros;
    digits = sDigits;
  }
  for (i = 0; i < ipp; ++i) {
    id = idPrefix + (i + 1);
	  img = document.getElementById(id);
    capEle = document.getElementById((galMode ? 'gc':'sc') + (i + 1));
    if (capEle) capPar = capEle.parentNode;
    if ((n + i) <= imgsMax) {
      if (zeros) src = xPad(n + i, digits, '0', true);
      else src = (n + i) + "";
			if (galMode) {	
				idA = idAPrefix + (i + 1);
				anch = document.getElementById(idA);
				anch.href = thisFile + '?s=' + (n + i);
			};
      img.title = imgTitle;
      img.alt = src;
      img.src = pth + imgPrefix + src + imgSuffix; // img to load now
      img.onerror = imgOnError;
			if (!galMode) img.onload = imgSsOnLoad;
      if (galMode) {
        img.style.cursor = 'pointer';
        img.slideNum = n + i; // slide img to load onclick
      }
      if (capEle) {
        capEle.innerHTML = captions[i + n];
        if (capPar) capPar.style.display = 'block';
      }
      img.style.display = 'inline';
    }
    else {
      img.style.display = 'none';
      if (capEle) {
        if (capPar) capPar.style.display = 'none';
      }
    }
  }  
}
function imgOnError()
{
  var p = this.parentNode;
  if (p) p.style.display = 'none';
}
function imgSsOnLoad()
{
	if (ssArg) { auto_onClick(null); }
}
function gal_setNav(n)
{
  var ipp = galMode ? imgsPerPg : 1;

  // Next
  var e = document.getElementById('next');
  if (n + ipp <= imgsMax) {	e.href = thisFile + '?s=' + (n + ipp); }
  else { e.href = thisFile + '?s=1'; }

  // Previous
  e = document.getElementById('prev');
  if (n > ipp) { e.href = thisFile + '?s=' + (n - ipp); }
  else { e.href = galMode ? (thisFile + '?s=' + normalize(imgsMax)) : (thisFile + '?s=' + imgsMax); }

  // Back
  e = document.getElementById('back');
  if (!galMode) { e.href = thisFile + '?'; }

	// Auto Slide
  e = document.getElementById('auto');
  e.onclick = auto_onClick;
}
function normalize(n)
{
  return 1 + imgsPerPg * (Math.ceil(n / imgsPerPg) - 1);
}
function auto_onClick(e)
{
  var ele = document.getElementById('time');
  autoSlide = !autoSlide;
  if (autoSlide) {
    slideCounter = 0;
    slideTimer = setTimeout("slide_OnTimeout()", slideTimeout);
    ele.style.display = 'inline';
  }
  else if (slideTimer) {
    clearTimeout(slideTimer);
    ele.style.display = 'none';
  }
}
function slide_OnTimeout()
{
  slideTimer = setTimeout("slide_OnTimeout()", 1000);
  ++slideCounter;
  document.getElementById('time').innerHTML = slideCounter + '/' + slideTimeout;
  if (slideCounter == slideTimeout) {
    if (++currentSlide > imgsMax) currentSlide = 1; 
    window.location = thisFile + '?s=' + currentSlide + '&ss=1';
    slideCounter = 0;
  }
}

/* xGetURLArguments and xPad are part of the X library,
   distributed under the terms of the GNU LGPL,
   and maintained at Cross-Browser.com.
*/
function xGetURLArguments()
{
  var idx = location.href.indexOf('?');
  var params = new Array();
  if (idx != -1) {
    var pairs = location.href.substring(idx+1, location.href.length).split('&');
    for (var i=0; i<pairs.length; i++) {
      var nameVal = pairs[i].split('=');
      params[i] = nameVal[1];
      params[nameVal[0]] = nameVal[1];
    }
  }
  return params;
}
function xPad(str, finalLen, padChar, left)
{
  if (typeof str != 'string') str = str + '';
  if (left) { for (var i=str.length; i<finalLen; ++i) str = padChar + str; }
  else { for (var i=str.length; i<finalLen; ++i) str += padChar; }
  return str;
}

