// JavaScript Document

/*************************************************
 * Photo Gallery routines for the Sacrament Bike *
 * Hikers website.  Version 1.0 6/4/2008         *
 * Scripts by Big Bear Studios                   * 
 * ***********************************************/
   
var ssInterval = 3000; // slide show interval in mSec's
var slideShowOn = false; // slideshow off or on
var SlideShowTimer = null;


// function advances one picture, then, if slideshow is on it calls the 
// timer function. the function raps around, last to first.
// if moveForward is false the function moves backwards
function AdvanceToNextImage(moveForward)
{
  var rows = dsGallery.getData();
  var curRow = dsGallery.getCurrentRow();
  
  if (rows.lenght < 1)
    return;
  
  for (i=0; i<rows.length; i++)
  {
    if (rows[i]==curRow)
    {
      if (moveForward)
	    ++i;
	  else
	    --i;
      break;
    }
  }
  
  if (moveForward && i>=rows.length)
    i = 0;
  else if (!moveForward && i<0)
    i = rows.length -1;
  
  curRow = rows[i];
  hiLighter(curRow["ds_RowID"]);
  //dsGallery.setCurrentRow(curRow["ds_RowID"]);
  
  if (slideShowOn)
    SetSlideShowTimer();
}

// function clears the timer (SlideShowTimer), then sets it to null
function KillSlideShowTimer()
{
  //alert("KillSlideShowTimer()... SlideShowTime: " + SlideShowTimer);
  if (SlideShowTimer)
  {
    clearTimeout(SlideShowTimer);
    SlideShowTimer = null;
  }
}

// Thumbnail click function - Stops the slideshow, and call's the hi-lighter
// routine. The actual picture change happens in the hiLighter() funct.
// tnID - thumbnail's ID
function ThumbnailClick(tnID)
{
  if (slideShowOn)
  {
    StopSlideShow();
  }
  hiLighter(tnID);
  //dsGallery.setCurrentRow(tnID);
}

// function builds the timer and attaches AdvanceToNextImage()
// after time-out, ssInterval, the AdvanceToNextImages runs which in turn
// runs this function ... so the slideshow keeps going.
function SetSlideShowTimer()
{
  KillSlideShowTimer();
  SlideShowTimer = setTimeout("AdvanceToNextImage(true)",ssInterval);
}

// function starts the slideshow and changes the label on the Play/Pause
// button
function StartSlideShow()
{
  var playLabel = document.getElementById("playLabel");
  if (playLabel)
    playLabel.firstChild.data = "Pause";
  AdvanceToNextImage(true);
  slideShowOn = true;
  SetSlideShowTimer();
}

// function stops the slideshow and changes the label on the Play/Pause
// button
function StopSlideShow()
{
  slideShowOn = false;
  //alert("Stop Slideshow ... " + slideShowOn);
  KillSlideShowTimer();
  var playLabel = document.getElementById("playLabel");
  if (playLabel)
    playLabel.firstChild.data = "Play";
}

// function removes the thumbnail hi-light from the current thumbnail, then
// sets the new current thumbnail, then hi-light's the thumbnail. The actual
// picture update happens via spry
function hiLighter(id)
{
  var imgList = document.getElementsByTagName('img');
  
  var curRowNo = dsGallery.getCurrentRowNumber();
  var curImg = imgList[curRowNo];
  
  curImg.style.border = "2px solid #494949";
  dsGallery.setCurrentRow(id);
  curRowNo = dsGallery.getCurrentRowNumber();
  curImg = imgList[curRowNo];
  curImg.style.border = "2px solid #FF0000";
}

// this observer stops the slideshow whenever a new gallery is selected
// from the gallery dropdown.
dsGallery.addObserver(function(nType, notifier, data)
{
  if (nType == "onPreLoad" && slideShowOn)
    StopSlideShow();
});

function navHover(id)
{
	id.style.backgroundImage = "url(../images/Gallery/GalleryNav_Hov.jpg)";
}

function navOut(id)
{
	id.style.backgroundImage = "url(../images/Gallery/GalleryNav_Up.jpg)";
}
