﻿var boxHeight;
var repeatHeight;
var stopScroll = 0;
var scrollSpeed = 100;
var pauseBeforeStart = 1000;
var x;

getNewsSource();

function initScroll() {
  boxHeight = $('scrollcontent').style.height.replace('px', '');
  repeatHeight = $('scrollcontent').scrollHeight; //get the current height so we know when to wrap
  $('scrollcontent').innerHTML = $('scrollcontent').innerHTML + $('scrollcontent').innerHTML; //add a second copy so we can scroll down to the wrap point
}

function scrollMe() {
  clearTimeout(x);
  if (stopScroll == 1) {
    return;
  }
  $('scrollcontent').scrollTop = $('scrollcontent').scrollTop + 1;
  if ($('scrollcontent').scrollTop <= repeatHeight) {
    // keep on scrolin'
    x = setTimeout("scrollMe()", scrollSpeed);
  }
  else { //we have hit the wrap point
    $('scrollcontent').scrollTop = 0;
    x = setTimeout("scrollMe()", scrollSpeed);
  }
}

function getNewsSource() {
  var url = "/news/";
  url = url + "?sid=" + Math.random();
  new Ajax.Request
  (
    url,
    {
      method: "get",
      onSuccess: function(transport) {
        var json = eval(transport.responseText);
        var div = $('scrollcontent');
        var html = "";
        for (j = 0; j < json.length; j++) {
          html = html + '<a href="' + json[j].link + '" target="_blank">' + json[j].title + '</a><br /><br />';
        }
        div.innerHTML = html;
        initScroll();
        x = setTimeout("scrollMe()", pauseBeforeStart);
      },
      onFailure: function() {
        alert("An error occured while trying to start the news scroller. Please try again later.");
      }
    }
  );
}