// Global variables
var curPos = 0;
var oInterval = ""; 
var imageArraySize = pictureArray.length;
var playState = "Pause";
var uiImagePath = "../Images/Menu/Slideshow/";

// Default speed of the Slideshow in milliseconds (3 seconds)
var slideShowSpeed = 3000;                 


function RandStart()
{
  var d;
  var ms;
  var randPic;

  // Get a random number that is within the array size by dividing the array size
  // by the number of milliseconds in the current time and taking the modulus. 
  // modulus of 
  d = new Date();
  ms = d.getMilliseconds();
  randPic = (ms % imageArraySize); 

  ChangePic(randPic);
}


function PlayPause()
{
  if(playState == "Play")
  {
      Pause();
  }
  else if (playState == "Pause")
  {
      Play();
  }
}


function Play()
{
   // Start the Slideshow
   oInterval = window.setInterval("ChangePic(1)", slideShowSpeed);

   // Set the playState to Play
   playState = "Play";

   // Set the button image to Pause
   document.getElementById("PlayButton").src = (uiImagePath + "Pause.gif");
}


function Pause()
{
   // Pause the Slideshow
   window.clearInterval(oInterval); 

   // Set the playState to Pause
   playState = "Pause";

   // Set the button image to Play
   document.getElementById("PlayButton").src = (uiImagePath + "Play.gif");
}


function Previous()
{
  // Ensure that the slideshow is Paused before stepping
  if(playState == "Play")
  {
      Pause();
  }

  // Step to the previous picture
  ChangePic(-1);
}


function Next()
{
  // Ensure that the slideshow is Paused before stepping
  if(playState == "Play")
  {
      Pause();
  }

  // Step to the next picture
  ChangePic(1);
}


function Slower()
{
  // Lengthen the time between pictures by 500 milliseconds
  ChangeSpeed(1);
}


function Faster()
{
  // Shorten the time between pictures by 500 milliseconds
  ChangeSpeed(-1);
}
 

function ChangePic(num)
{
  newPos = curPos + num; 

  // If the end or beginning of the array is reached, then wrap around.
  if( newPos >= imageArraySize )
  {
    newPos = 0;
  } 

  if( newPos < 0 )
  {
    newPos = (imageArraySize - 1);
  }  

  // Set the new image
  document.getElementById("MainImage").src = ( picPath + pictureArray[newPos] );

  // Save the current position in the Array of images. 
  curPos = newPos;  
}
 

function ChangeSpeed(speed)
{
  // Add the speed value to the current speed to change it
  slideShowSpeed += (speed * 500); 

  // Assure that speed does not go below 500 milliseconds
  if(slideShowSpeed < 500)
  {
      slideShowSpeed = 500;
  }

  // If the slideshow is running, Pause it before changing the speed
  if(playState == "Play")
  {
      Pause();
  }

  Play();
}
