/*
 License

Album Shaper is copyright (c) 2003-2005 Will Stokes. Album Shaper is licensed under the GNU General Public License, under which you may read and modify the source, distribute copies and modifications as you see fit. You must preserve the copyright notice, which entails you to make the source code available when redistributing copies or modifications you make.

From albumshaper.sourceforge.net/

http://albumshaper.sourceforge.net/sampleAlbums/Nature_WEB/subalbum_3_slideshow.html
http://albumshaper.sourceforge.net/sampleAlbums/Creations_WEB/slideshow_3.html?36
*/

//=====================================================        
//by default do not automatically advance in either direction
var autoNext = 0;
var autoPrev = 0;

// Advance delay in milliseconds.  A value of "500" is half a second, "5000" is five seconds.
var autoAdavanceDelay = 500;
var timerID = -1;
//=====================================================        
function keyHeldEvent()
{
  //If the user has since relesaed the 2nd button that was pressed, but the primary
  //button is still held, reset its value to 1 so it can still have an effect
  if( autoNext == 2 && autoPrev == 0)
    autoNext = 1;
  if( autoPrev == 2 && autoNext == 0)
    autoPrev = 1;

  //auto advance
  if( autoNext == 1)
      nextImage();
  else if( autoPrev == 1 )
      previousImage();

  //wait delay period, then check again  
  if( autoNext == 1 || autoPrev == 1 )
   timerID = setTimeout("keyHeldEvent()", autoAdavanceDelay);
}
//=====================================================        
//Handle key release event
function keyUpEvent(key) 
{
  if (!key) 
  {
    key = event;
    key.which = key.keyCode;
  }
  switch (key.which) 
  {
    case 37: // leftkey
    case 38: // upkey
      autoPrev = 0;
      break;
    case 39: // rightkey
    case 40: // downkey
      autoNext = 0;
      break;
  }
}
//=====================================================        
//Handle key pressed by changing the current photo as appropriate
function keyDownEvent(key) 
{
  clearTimeout(timerID);

  if (!key) 
  {
    key = event;
    key.which = key.keyCode;
  }
  switch (key.which) 
  {
/*
    case 36: // home
      firstImage()
      break;
    case 35: // end
      lastImage()
      break;
*/
    case 37: // leftkey
//    case 38: // upkey
      previousImage();
      autoPrev = 1;
      //if right/down key still pressed, degrade it but remember it's being held. this
      if( autoNext == 1 ) autoNext = 2;
      break;
    case 39: // rightkey
//    case 40: // downkey
      nextImage(); 
      autoNext = 1; 
      //if left/up key still pressed, degrade it but remember it's being held. this
      if( autoPrev == 1 ) autoPrev = 2;
      break;
  }
  
  //wait delay period, then check if keys still held  
  if( autoNext == 1 || autoPrev == 1 )
    timerID = setTimeout("keyHeldEvent()", autoAdavanceDelay);
}
