var browser = "";

var galleryId = 'gallery';    /* change this to the ID of the gallery list */
var baseOpacity = 30;         /* Opacity of lowlighted thumbnails */
var gallery;                  /* this will be the object reference to the list later on */
var galleryImages;            /* array that will hold all child elements of the list */
var galleryNames = null       /* array of name -> index pointers */
var timers = new Array();     /* Array of timer handles based on object id */
var curOpacity;               /* Array of faded object opacities */
var curObj;
var currentImage;             /* keeps track of which image should currently be showing */
var previousImage;
var paused = true;            /* Pause the slide show */
var slideShowTimer = null;    /* Slide show timer handle */
var timer;
var initialised = false;

var qsParm = new Array();

function qs()
{
   var query = window.location.search.substring(1);
   var parms = query.split('&');
   for (var i=0; i<parms.length; i++)
   {
      var pos = parms[i].indexOf('=');
      if (pos > 0)
      {
      var key = parms[i].substring(0,pos);
      var val = parms[i].substring(pos+1);
      qsParm[key] = val;
      }
   }
}

/*
 * Iterate through the gallery list and initialise each picture. 
 * 
 */
 		
function fadeInit()
{
   qsParm['img'] = null;
   qs();
   defaultID = qsParm['img'];
   
	if ((document.getElementById) && (gallery = document.getElementById(galleryId)))
	{
		var idx = 0;

		galleryImages = new Array();
		galleryNames  = new Array();

		currentImage = 0;
		
      for (var i = 0; i < gallery.childNodes.length; i++)
      {
         n = gallery.childNodes[i];
         if (n.id != null && n.className == "photocaption")
         {
            galleryImages[idx] = n;            
            galleryNames[n.id] = idx;
            if (n.id == defaultID)
               currentImage = idx;
            idx++;
         }
      }
						
		/* initialise a few parameters to get the cycle going */
		previousImage = galleryImages.length - 1;

      /* Remove the loading screen */
      document.getElementById('loading').style.display = "none";

      /* Reveal the thumb nail list */
      document.getElementById('disabledlinks').style.visibility = "visible";

      /* Show the first image */
      show(galleryImages[currentImage]);
      instantSet(document.getElementById(galleryImages[currentImage].id+'_tn'), 100);


      if (document.recalc)
      {
         /* IE6 bug - need to resize the body and recalc to reposition bottom bar correctly */
         document.body.style.height = document.body.style.height + 1;
         document.recalc(true);
      }
      
      initialised = true;
	}
}

function startSlideShow()
{
   paused = false;
   setTimeout('showNextImage()', 1000);
   document.getElementById(slideShowButtonID).innerHTML = slideShowStopText;      
}

function stopSlideShow()
{
   paused = true;
   document.getElementById(slideShowButtonID).innerHTML = slideShowStartText;
}      

/*
 * Called by the document to initialise the slide show button
 */

function setUpSlideShowButton(objID, startText, pauseText)
{
   slideShowButtonID = objID;
   slideShowStartText = startText;
   slideShowStopText = pauseText;   
}

/*
 * Called by the document to toggle the current slide show on/off
 */

function slideShowToggle()
{
   /* Obj should be the button object that was pressed to toggle the slide show */
   if (paused == false)
      stopSlideShow() ;
   else
      startSlideShow() ;
}

/*
 * Slide show function to display the next image in the list
 */
   
function showNextImage()
{
   if (initialised == false)
   {
      fadeInit();
      return;
   }
   
   if (paused == true)
      return
      
   previousImage = currentImage;
   currentImage +=1;
   
   if (currentImage>=galleryImages.length)
      currentImage=0;
   
   /* Highlight the thumbnails */
   instantSet(document.getElementById(galleryImages[previousImage].id+'_tn'), 30);
   instantSet(document.getElementById(galleryImages[currentImage].id+'_tn'), 100);

   /* Fade out the old image while fading in the new one */
   hide(galleryImages[previousImage]);
   show(galleryImages[currentImage]);

   setTimeout('showNextImage()', 4000);   
}

/*
 * View a given image.  Usually called by a click on the corresponding thumbnail.
 */

function viewImage(id)
{
   stopSlideShow();
   
   if (initialised == false)
   {
      fadeInit();
      return;
   }
   
   previousImage = currentImage;
   currentImage = galleryNames[id];

   if (previousImage == currentImage)
      return;
   
   /* Highlight the thumbnails */
   instantSet(document.getElementById(galleryImages[previousImage].id+'_tn'), 30);
   instantSet(document.getElementById(id+'_tn'), 100);

   /* Fade out the old image while fading in the new one */
   hide(galleryImages[previousImage]);
   show(galleryImages[currentImage]);
}

/*
 * Functions to highlight/lowlight a thumnail.  This will ensure that the current image thumb
 * remains highlighted.
 */

function highlightButton(obj)
{
   if (initialised == false)
   {
      fadeInit();
      return;
   }

   instantSet(obj, 100);
}


function lowlightButton(obj, baseOpacity)
{
   if (initialised == false)
   {
      fadeInit();
      return;
   }

   /* Only lowlight a button if it isn't the current image */
   if (obj.id != galleryImages[currentImage].id+'_tn')
      instantSet(obj, baseOpacity);
}

/*
 * Functions to start the various fading possibilities
 */

function slowHigh(obj, speed)
{
   if (timer != null)
      clearInterval(timer);
      
   curObj = obj;
   timer = setInterval("fadeUp()", speed)
}

function instantSet(obj, degree)
{
   getBrowser(obj);

   if (browser == "ie")
   {
      obj.style.filter = "alpha(opacity="+degree+")";
   }
   else if (browser == "moz") obj.style.MozOpacity = degree / 100;
   else if (browser == "css3") obj.style.opacity = degree / 100;
}

function show(obj)
{

   instantSet(obj, 50);
   curOpacity = 50;
   obj.style.display = "block";
   slowHigh(obj, 50);
}

function hide(obj)
{
   if (timer)
      clearInterval(timer);

   timer = null;
   instantSet(obj, 0);
   obj.style.display = "none";
}


/*
 * The fading code itself.
 */

function fadeUp()
{     
   getBrowser(curObj);
   
   if (curOpacity < 100)
   {
      curOpacity += 20;
   
      if (browser == "ie")
      {
         curObj.style.filter = "alpha(opacity="+curOpacity+")";
      }
      else if (browser == "moz")
      {
         curObj.style.MozOpacity = curOpacity/100 - 0.001;
      }
      else if (browser == "css3")
      {
         curObj.style.opacity = curOpacity/100 - 0.001;
      }
   }
   else
   {
      clearInterval(timer);
      timer = null;
   }
}

/*
 * Initialisation functions.
 */

function addEvent(elm, evType, fn, useCapture) 
{
   if (elm.addEventListener)
   {
      elm.addEventListener(evType, fn, useCapture);
      return true;
   }
   else if (elm.attachEvent)
   {
      var r = elm.attachEvent("on"+evType, fn);
      return r;
   }
} 

function getBrowser(obj)
{
   if (browser != "")
      return browser;
      
   if (obj.style.opacity != null)
	   browser = 'css3';
 	else if (obj.style.MozOpacity != null)
      browser = 'moz';
	else if (obj.style.filter != null)
      browser = 'ie';
      
   return browser;
}

/* initialise fader by hiding image object first */
addEvent(window, 'load', fadeInit)


