var m_selectedindex = 0

function onPrevious()
{
  if (m_selectedindex > 0)
  {
    onPhoto(m_selectedindex - 1)
  }
  
  return false
}

function onNext()
{
  if (m_selectedindex < $("thumbnailtable").rows.length - 1)
  {
    onPhoto(m_selectedindex + 1)
  }
  
  return false
}

function onPhoto(index)
{
  $("photo").fade({ duration: 0.5 })
  $("thumbnail" + m_selectedindex).className = "thumbnail"
  m_selectedindex = index

  var img = $("thumbnail" + m_selectedindex),
      div = $("thumbnaildiv"),
      location = findRelativePos(img, $("thumbnailtable"))
  img.className = "selectedthumbnail"
	  
  // Scroll if necessary
	  
  if (div.scrollTop > location.top)
  {
    $("thumbnaildiv").scrollTop = location.top
  }
  else if (div.scrollTop + div.offsetHeight < location.top + img.offsetHeight)
  {
    $("thumbnaildiv").scrollTop = location.top + img.offsetHeight - div.offsetHeight + 1
  }

  $("previous").className = m_selectedindex > 0 ? "" : "disabled"
  $("next").className = m_selectedindex < $("thumbnailtable").rows.length - 1 ? "" : "disabled"

  setTimeout("loadPhoto('" + $("thumbnaillink" + index).href + "')", 500)
  return false
}

function loadPhoto(href)
{
  $("photo").src = href
}

function onPhotoLoaded()
{
  $("photo").appear({ duration: 0.5 })
}
