﻿var scrollBox = document.getElementById("scrollBox");
var viewHeight = document.documentElement.clientHeight;
var viewPos = 0; //viewHeight;
var viewSpeed = 30;
var viewPixels = 1;
var boxHeight = 0;
var viewMove = viewPixels;
var itemPause = 2000;
var pausePos = 0;
var pauseIndex = 1;
var browserSpanOffset = 0;

function out() { viewMove = viewPixels; }
function over() { viewMove = 0; }

function startScrolling() {

  if (scrollBox == null) return;

  if (scrollBox.offsetHeight) {
    boxHeight = scrollBox.offsetHeight;
  }
  else {
    if (document.defaultView != null) {
      boxHeight = document.defaultView.getComputedStyle(document.getElementById("scrollBox"), "").getPropertyValue("height");
      boxHeight = eval(boxHeight.substring(0, boxHeight.indexOf("<div>")));
    }
  }
  boxHeight = 0 - (boxHeight * 1.5);

  // Get frame height from parent
  if (window.name == "")
    return;   
  var parentFrame = window.parent.document.getElementById(window.name);
  if (parentFrame != null)
    boxHeight = parentFrame.scrollHeight * -2;

  scrollBox.style.visibility = "visible";

  // Handle span stop offset issue in FF
  if (navigator.userAgent.indexOf("Firefox") != -1)
    browserSpanOffset = 10;

  //scrollBox.style.top = viewHeight;
  //setTimeout("continueScrolling()", itemPause);
  continueScrolling();
}

function continueScrolling() {
  viewPos -= viewMove;
  scrollBox.style.top = viewPos + "px";
  var pauseFor = viewSpeed;

  if (viewPos < pausePos && pauseIndex != -1) {
    var span = document.getElementById("span" + pauseIndex);
    if (span != null) {
      pausePos = -(span.offsetTop - browserSpanOffset);
      pauseIndex++;
    }
    else {
      pauseIndex = -1;
    }
    pauseFor = itemPause;
  }

  if(pauseIndex == -1) {
    if (viewPos < (pausePos + (boxHeight/2))) {
      viewPos = viewHeight; pausePos = 0; pauseIndex = 1;
    }
  }
  setTimeout("continueScrolling()", pauseFor);
}

startScrolling();

