
var speed = 4;
var timer;
var intervalTime = 50;
var totalBlocks;
var blockHeight = 405; 
var currentBlock = 1; 
var slideshowTimer;

function initialise()
{
  totalBlocks = parseInt(document.getElementById("collectionCount").innerHTML);
  document.getElementById("Main").firstChild.style.paddingBottom = (document.body.clientHeight - blockHeight) + "px";
  document.getElementById("Main").onscroll = checkScroll;
  checkFooter();
}

function checkScroll()
{
  clearTimeout(timer);
  timer = setTimeout("checkBlockFade()", 100);
  checkFooter();
}

function fadeBlock(nextBlock)
{
  setTimeout("document.getElementById('Collection'+" + currentBlock + ").style.color='#CDCDCD'",0);
  setTimeout("document.getElementById('Collection'+" + currentBlock + ").style.color='#EDEDED'",50); 
  setTimeout("document.getElementById('Collection'+" + currentBlock + ").style.color='#FFFFFF'",100); 
    
  setTimeout("document.getElementById('Collection'+" + nextBlock + ").style.color='#FFFFFF'",150);
  setTimeout("document.getElementById('Collection'+" + nextBlock + ").style.color='#CDCDCD'",250);
  setTimeout("document.getElementById('Collection'+" + nextBlock + ").style.color='#ADADAD'",300);
  setTimeout("document.getElementById('Collection'+" + nextBlock + ").style.color='#9D9D9D'",350);
  setTimeout("document.getElementById('Collection'+" + nextBlock + ").style.color='#8D8D8D'",400);
  setTimeout("document.getElementById('Collection'+" + nextBlock + ").style.color='#6D6D6D'",450);
    
  currentBlock = nextBlock;
}

function calcDestination(currentPosition, direction)
{
  if(direction == 1)
    return Math.floor((currentPosition + blockHeight) / blockHeight) * blockHeight;
  else
    return Math.ceil((currentPosition - blockHeight) / blockHeight) * blockHeight;
}

function calcNearestBlock()
{
  return Math.round(document.getElementById('Main').scrollTop / blockHeight) + 1;
}

function moveTo(destinationPosition, direction)
{
  var currentPosition = document.getElementById('Main').scrollTop;
  if(direction == 1)
    var nudgeDistance = Math.ceil((destinationPosition - currentPosition) / speed) + direction;
  else
    var nudgeDistance = Math.floor((destinationPosition - currentPosition) / speed) + direction;
	
  if(document.getElementById("Main").scrollTop != destinationPosition)
    {
      document.getElementById("Main").scrollTop += nudgeDistance;
      checkBlockFade();
      clearTimeout(timer);
      timer = setTimeout("moveTo(" + destinationPosition + "," + direction + ")", intervalTime);
    }
}

function slide(direction)
{
  var currentPosition = document.getElementById('Main').scrollTop;
	
  var destinationPosition = calcDestination(currentPosition,direction);
	
  if(checkBoundary(destinationPosition)) 
    { 
      moveTo(destinationPosition,direction);
    }
  else
    {
      // return false;
    }
}

function checkBoundary(destinationPosition)
{
  var bottomValidPosition = (totalBlocks-1) * blockHeight;
  if(destinationPosition < 0 || destinationPosition > bottomValidPosition )
    {
      return false;
    }
  else 
    {	
      return true;
    }
}


function slideshow(action)
{
  clearTimeout(slideshowTimer);
	
  if (action == 0)
    {
      document.getElementById("slideShowButton").className = "button-slideshow";
    }
  else if (action == 1)
    {
      if(document.getElementById("slideShowButton").className == "button-slideshow-on")
	{
	  document.getElementById("slideShowButton").className = "button-slideshow";
	}
      else
	{
	  document.getElementById("slideShowButton").className = "button-slideshow-on";
	}
    }

  if(document.getElementById("slideShowButton").className == "button-slideshow-on" || action == 2)
    {
      slide(1);
      slideshowTimer = setTimeout("slideshow(2)",4000);
    }

}

function checkBlockFade()
{
  var nextBlock = calcNearestBlock();
  if(nextBlock != currentBlock)
    {
      fadeBlock(nextBlock);
    }
}

window.onload = initialise;
parent.document.write('<style>.collection P {color:#FFFFFF}</style>');
parent.document.write('<div class="slideshow"><p><a class="button-previous" href="javascript:slideshow(0)\; slide(-1)\;" onclick="this.blur()\;">Previous</a><a class="button-next" href="javascript:slideshow(0)\; slide(1)\;" onclick="this.blur()\;">Next</a></p><p><a id="slideShowButton" class="button-slideshow" href="javascript:slideshow(1)\;" onclick="this.blur()\;">Slideshow</a></p></div>');
