var maxNegativeMargin;
var maxPosMargin;
var photoInner;
var back;
var forward;
var photoInnerWidth;
var moveLeft = false;
var currentMargin = 0;
var moveRate = 5;
var milliSeconds = 1;
var mousePos = 0;
var photoDiv;

window.onload = function()
{
  photoInner = document.getElementById('photoInner');
  photoDiv = document.getElementById('photoDiv');
  back = document.getElementById('back');
  forward = document.getElementById('forward');
  if(photoInner && back && forward)
  {
    photoInnerWidth = photoInner.style.width.substr(0, photoInner.style.width.length - 2);
    maxNegativeMargin = (photoInnerWidth * -1);
    maxPosMargin = 0;
    forward.onmouseover = startMovingLeft;
    forward.onmouseout = stopMoving;
    back.onmouseover = startMovingRight;
    back.onmouseout = stopMoving;
  }
}

function startMovingLeft()
{
  moveLeft = true;
  moveRight = false;
  movePhotoInner();
}

function stopMoving()
{
  moveLeft = false;
  moveRight = false;
}

function startMovingRight()
{
  moveLeft = false;
  moveRight = true;
  movePhotoInner();
}

function movePhotoInner()
{
  if(moveLeft)
  {
    if(currentMargin > (maxNegativeMargin + photoDiv.offsetWidth))
    {
      currentMargin = currentMargin - moveRate;
      classSwapping('remove', back, 'hidden');
    }
    else
    {
      classSwapping('add', forward, 'hidden');
    }
  }
  else if(moveRight)
  {
    if(currentMargin < maxPosMargin)
    {
      currentMargin = currentMargin + moveRate;
      classSwapping('remove', forward, 'hidden');
    }
    else
    {
      classSwapping('add', back, 'hidden');
    }
  }
  photoInner.style.left = currentMargin + 'px';
  if(moveLeft || moveRight)
  {
    setTimeout("movePhotoInner()", milliSeconds);
  }
}
