var CrossFadeDuration = 0.5;
var PageLoaded = false;
var preLoad = new Array();
var CurrentPhoto = 0;
var PhotoCount = Picture.length-1;
var Photo_Loading_DIV;
var Photo_PictureBox_DIV;
var Photo_PictureTemp_DIV;
function control(action,id)
{
  if (document.getElementById("photo_loading"))
  {
    Photo_Loading_DIV = document.getElementById("photo_loading");
    //Photo_Loading_DIV.style.visibility = 'visible';
  }
  if (document.getElementById("PictureBox"))
  {
    Photo_PictureBox_DIV = document.getElementById("PictureBox");
  }
  if (document.getElementById("PictureTemp"))
  {
    Photo_PictureTemp_DIV = document.getElementById("PictureTemp");
    Photo_PictureTemp_DIV.onload = AfterPhotoLoad;
  }

  if (PageLoaded)
  {
    if (action=="home") CurrentPhoto = 1;
    if (action=="next") CurrentPhoto = CurrentPhoto + 1;
    if (action=="prev") CurrentPhoto = CurrentPhoto - 1;
    if (action=="goto") CurrentPhoto = DefaultImagePos;
    if (CurrentPhoto > (PhotoCount)) CurrentPhoto = 1;
    if (CurrentPhoto < 1) CurrentPhoto = PhotoCount;
    if (typeof preLoad[CurrentPhoto] == 'object')
    {
      PreLoadImage();
    }
    else
    {
      opacity('photo_loading', 0, 100, 250);
      preLoad[CurrentPhoto] = new Image();
      preLoad[CurrentPhoto].src = Picture[CurrentPhoto];
      preLoad[CurrentPhoto].onLoad = PreLoadImage();
    }
  }
}

function PreLoadImage()
{
  Photo_PictureTemp_DIV.src = preLoad[CurrentPhoto].src;
}

function FadeImages()
{
  if (Photo_PictureBox_DIV)
  {
    if (document.all && Photo_PictureBox_DIV && Photo_PictureBox_DIV.filters)
    {
      Photo_PictureBox_DIV.style.filter = "blendTrans(duration=" + CrossFadeDuration + ")";
      Photo_PictureBox_DIV.filters.blendTrans.Apply();
    }
    Photo_PictureBox_DIV.src = Photo_PictureTemp_DIV.src;
    if (Photo_PictureBox_DIV) Photo_PictureBox_DIV.title = Caption[CurrentPhoto];
    if (document.getElementById("CurrentItemBox")) document.getElementById("CurrentItemBox").innerHTML = CurrentPhoto;
    if (document.getElementById("TotalItemsBox")) document.getElementById("TotalItemsBox").innerHTML = PhotoCount;
    if (document.getElementById("InfoBox")) document.getElementById("InfoBox").style.display = 'block';
    if (document.all && Photo_PictureBox_DIV && Photo_PictureBox_DIV.filters)
    {
      Photo_PictureBox_DIV.filters.blendTrans.Play();
    }
  }
}

function AfterPhotoLoad()
{
  FadeImages();
  opacity('photo_loading', 100, 0, 250);
}

function opacity(id, opacStart, opacEnd, millisec)
{
  //speed for each frame
  var speed = Math.round(millisec / 100);
  var timer = 0;
  var object = document.getElementById(id).style;

  if (object.MozOpacity == (opacEnd / 100))
  {
    return false;
  }

  if(opacStart > opacEnd)
  {
    for(i = opacStart; i >= opacEnd; i--)
    {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
      timer++;
    }
  }
  else if(opacStart < opacEnd)
  {
    for(i = opacStart; i <= opacEnd; i++)
    {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
      timer++;
    }
  }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}