// Configurable Variables:

var imgsPerPg = 6; // number of img elements in the html
var imgsMax = 18;  // total number of images
var slideTimeout = 5; // seconds before loading the next slide

var gPath = 'images/gc/';  // gallery files (thumbnails) path, include trailing slash
var gPrefix = '';
var gSuffix = 'tn';
var gExt = '.jpg';
var gZeros = false; // filename uses leading zeros?
var gDigits = 4    // total digits in filename, including leading zeros

var sPath = 'images/gc/'; // slideshow files (big imgs) path, include trailing slash
var sPrefix = '';
var sSuffix = '';
var sExt = '.jpg';
var sZeros = false; // filename uses leading zeros?                     
var sDigits = 4    // total digits in filename, including leading zeros

var captions = new Array();
// There must be (imgsMax + 1) captions.
// captions[0] is currently not used.
captions[0] = "caption 0";
captions[1] = "Main square in front of the City Conference Centre";
captions[2] = "Opening Ceremony of the World Water Week";
captions[3] = "UNSGAB & UN-Water: (Left) Chair of UNSGAB HRH Willem-Alexander Prince of Orange and (right) Chair UN-Water Pasquale Steduto";
captions[4] = "UNSGAB & UN-Water: (Left) Deputy Chair of UNSGAB Ms Eid Uschi and (right) Chair UN-Water";
captions[5] = "Ms Clarissa Brocklehurst, Chair UN-Water Task Force on Sanitation";
captions[6] = "Presentation at the UN-Water Seminar by the chair UN-Water";
captions[7] = "Presentation at the UN-Water Seminar by the chair UN-Water";
captions[8] = "Key note speech at the UN-Water Seminar by the Chair of UNSGAB";
captions[9] = "Presentation at the UN-Water Seminar by Ms Kenza Robinson,Focal Point for the International Year of Sanitation,2008";
captions[10] = "Presentation at the UN-Water Seminar by Ms Kenza Robinson";
captions[11] = "Presentation at the UN-Water Seminar by Mr Jamie Bartram, former UN-Water Chair";
captions[12] = "Interventions during the UN-Water seminar";
captions[13] = "Interventions during the UN-Water seminar";
captions[14] = "A view at the participants of the UN-Water Seminar";
captions[15] = "Presentation of the discussion summary at the UN-Water Seminar";
captions[16] = "   ";
captions[17] = "   ";
captions[18] = "   ";

// End Configurable Variables

window.onload = function()
{
  xImgGallery();
};

/* Gallery (thumbnail) image pathnames: gPath + gPrefix + seq_num + gSuffix + gExt
   SlideShow (large image) pathnames:   sPath + sPrefix + seq_num + sSuffix + sExt
   Leading zeros (as part of seq_num) are optional.
*/
var galMode = false;
var autoSlide = false;
var slideTimer = null;
var slideCounter = 0;
var currentSlide = 1;

function xImgGallery()
{
  if (document.getElementById && document.getElementById('navigation').style) {
    var n = 1, a = xGetURLArguments();
    if (a.length) {
      var arg = a['g'] || a['s'];
      if (arg) {
        n = parseInt(arg, 10);
        if (n <= 0 || n > imgsMax) { n = 1; } 
        if (a['s']) { galMode = false; }
      }
    }
    gal_paint(n);
    document.getElementById('time').style.display = 'none';
  }
}
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, img, id, src, ipp, idPrefix, imgSuffix, imgPrefix;
  var zeros, digits, capEle, capPar;
  if (galMode) {
    ipp = imgsPerPg;
    idPrefix = 'g';
    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) + "";
      img.title = imgTitle;
      img.alt = src;
      img.src = pth + imgPrefix + src + imgSuffix; // img to load now
      img.onerror = imgOnError;
      if (galMode) {
        img.style.cursor = 'pointer';
        img.slideNum = n + i; // slide img to load onclick
        img.onclick = imgOnClick;
      }
      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 imgOnClick()
{
  galMode = false;
  gal_paint(this.slideNum);
}
function imgOnError()
{
  var p = this.parentNode;
  if (p) p.style.display = 'none';
}
function gal_setNav(n)
{
  var ipp = galMode ? imgsPerPg : 1;
  // Next
  var e = document.getElementById('next');
  if (n + ipp <= imgsMax) {
    e.nextNum = n + ipp;
    e.onclick = next_onClick;
    e.style.display = 'inline';
  }
  else {
    e.nextNum = 1;
  }
  // Previous
  e = document.getElementById('prev');
  e.style.display = 'inline';
  e.onclick = prev_onClick;
  if (n > ipp) {
    e.prevNum = n - ipp;
  }
  else {
    e.prevNum = galMode ? normalize(imgsMax) : imgsMax;
  }
  // Back
  e = document.getElementById('back');
  if (!galMode) {
    e.onclick = back_onClick;
    e.style.display = 'inline';
    e.backNum = normalize(n);
  }
  else {
    e.style.display = 'none';
  }
  // Auto Slide
  e = document.getElementById('auto');
  if (!galMode) {
    e.onclick = auto_onClick;
    e.style.display = 'inline';
  }
  else {
    e.style.display = 'none';
  }
}
function normalize(n)
{
  return 1 + imgsPerPg * (Math.ceil(n / imgsPerPg) - 1);
}
function next_onClick(e)
{
  gal_paint(this.nextNum);
}
function prev_onClick(e)
{
  gal_paint(this.prevNum);
}
function back_onClick(e)
{
  galMode = true;
  if (slideTimer) {
    clearTimeout(slideTimer);
  }
  autoSlide = false;
  gal_paint(this.backNum);
  document.getElementById('time').style.display = 'none';
}
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; 
    gal_paint(currentSlide);
    slideCounter = 0;
  }
}

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++) {
      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;
}
function openwater(what,type){
         msgPage=window.open(what,"displayWindow","toolbar=no,scrollbars=no,width=600,height=380,resizable=no")
         msgPage.focus()
      }

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/*function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  this.focus();
}*/
function MM_openBrWindow(what,type){
         msgPage=window.open(what,"displayWindow","toolbar=no,scrollbars=no,width=800,height=600,resizable=no")
         msgPage.focus()
      }