// =======================================================================
// Photoshare by Jorn Lind-Nielsen (C) 2002.
// ----------------------------------------------------------------------
// For POST-NUKE Content Management System
// Copyright (C) 2002 by the PostNuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WIthOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// =======================================================================

// Expect the following variables to be produced in the HTML:
//    array  'photoshareImages' containing the image information.
//    int    'startImageIndex' pointing to the initial image.
//    string 'thumbnailURL' pointing to the thumbnails view

// =======================================================================
// Globals
// =======================================================================
var currentImage = 0;
var currentShowTime = 8;

  // reference to JavaScript timer
var timeoutId = null;


// =======================================================================
// Event handlers
// =======================================================================

function handleOnLoad()
{
  currentImage = startImageIndex;
  updateCurrentImage();
  updateImage(currentImage);
}

function handleTimeout()
{
  handleOnClickNext();
  timeoutId = setTimeout("handleTimeout()", currentShowTime*1000);
}


function handleOnClickPlay()
{
  if (timeoutId == null)
  {
      // No timer started means the player is paused.
      // Go to next image and start timer

    nextImage();

    var imagePlay = document.images['imagePlay']; 
    imagePlay.src = imageBaseURL + "/button_pause.gif";
    imagePlay.title = translations.pause;

    timeoutId = setTimeout("handleTimeout()", currentShowTime*1000);
  }
  else
  {
    var imagePlay = document.images['imagePlay']; 
    imagePlay.src = imageBaseURL + "/button_play.gif";
    imagePlay.title = translations.start;

    clearTimeout(timeoutId);
    timeoutId = null;
  }
}


function handleOnClickNext()
{
  nextImage();
}


function handleOnClickPrev()
{
  --currentImage;

  if (currentImage < 0)
    currentImage = photoshareImages.length-1;

  updateImage(currentImage);
  updateCurrentImage();
}


function handleOnClickThumbnails()
{
  window.location = thumbnailsURL;
}


function handleChangeTime()
{
  var selector = document.getElementById('timeSelector');
  currentShowTime = selector.value;

  if (timeoutId == null)
    handleOnClickPlay();
}


// =======================================================================
// 
// =======================================================================

function updateCurrentImage()
{
  var imageInfoTD = document.getElementById('imageInfoText');
  imageInfoTD.innerHTML = (currentImage+1) + "/" + photoshareImages.length;
}


function nextImage()
{
  ++currentImage;

  if (currentImage > photoshareImages.length-1)
    currentImage = 0;

  updateImage(currentImage);
  updateCurrentImage();
}


function updateImage(index)
{
  document.images['imageLocation'].src = photoshareImages[index].url;
  document.images['imageLocation'].alt = photoshareImages[index].title;

    // Get DOM reference to title and set it
  var titleRef = document.getElementById('imageTitleText');
  titleRef.innerHTML = photoshareImages[index].title;

    // Get DOM reference to description and set it
  var descriptionRef = document.getElementById('imageDescriptionText');
  descriptionRef.innerHTML = photoshareImages[index].description;
}



