var textObject = null;
var textObjectWidth = 0;
var headerWidth = 0;
var speed = 0;
var pause = 0;
var pauseCounter = 0;
var textPosition = 0;
var scrollTextInterval = null;
var scrollTextObjectWidth = 0;
var isScrollingOut = false;
var path = null;
var pathLength = 0;
var pIndex = 0;


function scroll() {
  
  if(textPosition <= pathLength && isScrollingOut == false) {
    textPosition -= path[pIndex++];
  }
  else  {
    textPosition -= speed;
  }
  
  if(textPosition < -textObjectWidth + headerWidth) {
    isScrollingOut = false;     
    textPosition = scrollTextObjectWidth;
    pIndex = 0;
  }
  
  if(textPosition <= headerWidth && isScrollingOut == false) {
    isScrollingOut = true; 
    stopScrollText();
    scrollTextInterval = setInterval("pauseScrollText()", 1000);
  }
  
  textObject.style.left = textPosition + "px";
}


function initScrollText(scrollTextObj, text, s, p) {  
  textObject = document.getElementById(scrollTextObj).getElementsByTagName('a')[0];
  textObject.innerHTML = text;  
  textObjectWidth = textObject.offsetWidth;
  headerWidth = 0;
  //uncomment following line unless #scrolltext_header is float left
  //headerWidth = document.getElementById('scrolltext_header').offsetWidth;
  speed = s;
  pause = p;
  scrollTextObjectWidth = document.getElementById(scrollTextObj).offsetWidth;
  textPosition = scrollTextObjectWidth;
  textObject.style.left = textPosition + "px";
  
  precalcPath();
  startScrollText();
}


function precalcPath() {
  pIndex = 0;
  path = new Array(90);
  var v1 = speed;
  var v2 = 0;
  var a = 0;
  
  for (i = 0; i < 90; i++) {
    a = 1 - Math.cos((i * Math.PI) / 180.0);
    path[i] = (v1 * (1 - a) + v2 * a);
    pathLength += Math.abs(path[i]);    
  }  
  pathLength += headerWidth;
}


function startScrollText() {
  stopScrollText();
  scrollTextInterval = setInterval("scroll()", 1000/60);  
}


function stopScrollText() {
  clearInterval(scrollTextInterval);
}


function pauseScrollText() {
  pauseCounter += 1;
  if(pauseCounter >= pause) {
    pauseCounter = 0;
    startScrollText();
  }
}




