// Begin User Configurable Variables:

var imgsPerPg = 9; // number of img elements in the html
var imgsMax = 81;  // total number of images
var slideTimeout = 5; // seconds before loading the next slide

var gPath = '/filemanager/images/gc/';  // gallery files (thumbnails) path, include trailing slash
var gPrefix = '';
var gSuffix = 'tn';
var gExt = '.jpg';
var gZeros = true; // filename uses leading zeros?
var gDigits = 4    // total digits in filename, including leading zeros

var sPath = '/filemanager/images/gc/'; // slideshow files (big imgs) path, include trailing slash
var sPrefix = '';
var sSuffix = '';
var sExt = '.jpg';
var sZeros = true; // 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] = "not used";
captions[1] = "Cezanne Rose Bridal Bouquet";
captions[2] = "Pimms O'Clock Design";
captions[3] = "Fireplace Display with Silver Vases";
captions[4] = "Purple Table Centre";
captions[5] = "White &amp; Green Long &amp; Low Display";
captions[6] = "Vintage Bridal Bouquet";
captions[7] = "Pink &amp; White Column Vase";
captions[8] = "Roses &amp; Snowberry Bouquet";
captions[9] = "Illusion, Amnesia &amp; Sweet Avalanche Rose Display";
captions[10] = "Avalanche, Sweet Avalanche &amp; Mimi Eden Bouquet";
captions[11] = "Vintage Long and Low Arrangement";
captions[12] = "Chair Arrangement of Hydrangea, Roses and Bouvardia";
captions[13] = "Summer Party Table Runner";
captions[14] = "Bridesmaid Bouquet of Old Dutch Roses and Green Thistle";
captions[15] = "Church Garland of Hydrangea, Roses and Liles";
captions[16] = "Hand Tied of Akito Roses, Bouvardia and Calla Lilies";
captions[17] = "Lavender Stair Posy";
captions[18] = "Phalaenopsis Orchid Bouquet";
captions[19] = "Sweet Avalanche Bouquet";
captions[20] = "Roses, Peony &amp; Hydrangea Cube Vase";
captions[21] = "Purple Country Display";
captions[22] = "White &amp; Green Silver Bowls";
captions[23] = "Pastel Pink Bridal Bouquet";
captions[24] = "Rose Wedding Cake";
captions[25] = "Flower Clusters in Silver Vases";
captions[26] = "Aqua Pink &amp; Purple Bouquet";
captions[27] = "Table Runner Display";
captions[28] = "Pretty Pink Bridal Bouquet' Antique Bridal Bouquet";
captions[29] = "Lily Vase Display";
captions[30] = "Buttonholes of Vandella Roses and Hydrangea";
captions[31] = "Church Garland";
captions[32] = "Snowberries and Hydrangea Gate Arrangements";
captions[33] = "Vendela and Cezanne Rose Buttonholes";
captions[34] = "Country Candelabra";
captions[35] = "Top Table Runner";
captions[36] = "Mirrored Country Table Centres";
captions[37] = "Hand Tied Bouquet of Roses, Thistle, Scabious &amp; Mint";
captions[38] = "Hydrangea, Berries and Rose Globes";
captions[39] = "Mirrored Globe of Roses";
captions[40] = "Willow Urn Display";
captions[41] = "Christmas Table Wreath";
captions[42] = "Sex and the City Martini Vase";
captions[43] = "Sweet Avalanche and Metallina Roses";
captions[44] = "Cream Roses with Scabious &amp; Mint";
captions[45] = "Vintage Birdcage Display";
captions[46] = "Grand Prix Bouquet";
captions[47] = "Large Pedestal Arrangement";
captions[48] = "Grand Prix &amp; Black Baccara Rose Display";
captions[49] = "Tear Drop Arrangement of Hydrangeas, Lilies and Veronica";
captions[50] = "Grand Prix Cube";
captions[51] = "Calla Lilies, Orchids and Orange Rose Bouquet";
captions[52] = "Cymbidium Orchid and Feather Bouquet";
captions[53] = "Pretty Pew Ends";
captions[54] = "Tropical Bouquet of Roses, Calla Lilies and Gloriosa";
captions[55] = "Phalaenopsis Orchid Buttonhole";
captions[56] = "Cylinder Vase of Hydrangea, Ammi and Mint";
captions[57] = "Table Centre of Hydrangea, Roses and Peonies";
captions[58] = "Lavender and Prunus Garland";
captions[59] = "Pink , Purple and Golden Spring Wreath";
captions[60] = "Cake Garland Arrangement";
captions[61] = "Old Dutch Roses with Green Thistle";
captions[62] = "Table Centre Runner of Roses, Hydrangeas and Peonies";
captions[63] = "Country Floral Vase";
captions[64] = "Vintage Rose Cake";
captions[65] = "Calla Lily Bouquet";
captions[66] = "Vintage Bridesmaids Posies";
captions[67] = "Lavender Napkin Centre";
captions[68] = "Decorated Wedding Cake";
captions[69] = "Candle and Flower Table Centre";
captions[70] = "Gate Arrangements";
captions[71] = "Top Table with Silver Vases";
captions[72] = "Bride and Bridesmaid Bouquets";
captions[73] = "Shabby Chic Bridal Bouquet";
captions[74] = "Church Gate Arrangement";
captions[75] = "Vintage Cake Stand";
captions[76] = "Silver Bowls with Lights";
captions[77] = "Rose Buttonhole with Berries and Ivy";
captions[78] = "Long and Low";
captions[79] = "Cake Decoration";
captions[80] = "Wind Light with Rose Wreath";
captions[81] = "Garland of Silverston and Vendela Roses";

// End User Configurable Variables

window.onload = function()
{
  xImgGallery();
};

/* Development Notes and Script Assumptions:

1) Optional URL arguments: g = 'gallery first num', s = 'slideshow img num'.
   For example:
   img_gallery_2.php?g=21
   img_gallery_2.php?s=30

2) IMG IDs begin with 'g' (for gallery) or 's' (for slideshow)
   then are numbered sequentially beginning with 1. No leading zeros.

3) 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.

4) The captions array initializations could be generated server-side.

5) This file does not have to be a PHP file - I use it here for my page-template, not for this demo.
*/

/* xImgGallery, Copyright (C) 2004,2005 Michael Foster (Cross-Browser.com)
   Distributed under the terms of the GNU LGPL.
*/

// don't change these:
var galMode = true;
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);
    //JIM: 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) {
    //JIM: e.onclick = auto_onClick;
    //JIM: e.style.display = 'inline';
  }
  else {
    //JIM: 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);
  //JIM: 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;
  }
}

/* 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++) {
      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;
}
