var scrollPeriod = 100;
var scrolling = false;

function getStyleById(id, property) {
  var element = document.getElementById(id);
  var style = eval("element.style." + property);

  if ((style != "") && (style != null)) {
    return style;
  }

  if (element.currentStyle) {
    style = eval("element.currentStyle." + property);
    if ((style != "") && (style != null)) {
      return style;
    }
  }

  return null;
}

function setStyleById(id, property, value) {
  var element = document.getElementById(id);
  element.style[property] = value;
}

// FIXME: this should be moved to a utility library
function getElementHeight(element) {
  var height = null;
  try {
  if (typeof document.defaultView != "undefined" && typeof document.defaultView.getComputedStyle != "undefined") {
    var computedStyle = document.defaultView.getComputedStyle(element, "");
    height = computedStyle.getPropertyValue("height");
  }
  if ((height == null) || (height.length < 1)) {
    height = element.offsetHeight;
  }
  } catch (e){
     // no one will know actually.
  }
  return parseInt(height);
}

function scroll(containerId, contentId, direction, amount) {
  var containerDiv = document.getElementById(containerId);
  var contentDiv = document.getElementById(contentId);
  var topOffset = parseInt(getStyleById(contentId, 'top'));
  if (isNaN(topOffset)) {
    topOffset = 0;
  }
  var containerHeight = getElementHeight(containerDiv);  
  var contentHeight = getElementHeight(contentDiv);
  var newOffset;
  if (direction == 'top') {
    newOffset = 0;
  }
  else if (direction == 'bottom') {
    newOffset = containerHeight - contentHeight;
  }
  else if (direction == 'up') {
    newOffset = topOffset + amount;
  }
  else if (direction == 'down') {
    newOffset = topOffset - amount;
  }
  else if (direction == 'to') {
    newOffset = 0 - amount;
  }
  if (newOffset > 0) {
    newOffset = 0;
  }
  if ((containerHeight - newOffset) > contentHeight) {
    newOffset = containerHeight - contentHeight;
  }

  contentDiv.style['top'] = newOffset;

  return true;
}

function scrollUp(containerId, contentId) {
  return scroll(containerId, contentId, 'up');
}
function scrollDown(containerId, contentId) {
  return scroll(containerId, contentId, 'down');
}
function scrollToTop(containerId, contentId) {
  return scroll(containerId, contentId, 'top');
}
function scrollToBottom(containerId, contentId) {
  return scroll(containerId, contentId, 'bottom');
}

function scrollContinuous(containerId, contentId, direction, amount) {
  if (scrolling) {
    scroll(containerId, contentId, direction, amount);
    window.setTimeout("scrollContinuous('" + containerId + "', '" + contentId
                      + "', '" + direction + "', " + amount + ");", scrollPeriod);
  }
}

function scrollStart(containerId, contentId, direction, amount) {
  scrolling = true;
  scrollContinuous(containerId, contentId, direction, amount);
}

function scrollStop() {
  scrolling = false;
}

function scrollSetup(containerId, contentId, controlsId) {
  var container = document.getElementById(containerId);
  var content = document.getElementById(contentId);
  var controls = document.getElementById(controlsId);
  var containerHeight = getElementHeight(container);  
  var contentHeight = getElementHeight(content);

  if (contentHeight <= containerHeight) {
    controls.style['visibility'] = 'hidden';
  }
}

