


// Accessible Scroller by Mike Foskett (http://www.websemantics.co.uk/). Retain this message and you may use the code freely.

var speed=30        // speed of scroller. Its more a delay. The smaller, the faster.
var step=3          // smoothness of movement
var top="#top"      // name of anchor used as page top when clicking start / stop / show
var offset = 5    // Offset.
var Start= "Start"  // Text for start link
var Stop = "Stop"   // Text for stop link
var Show = "Show"   // Text for show link
var scrollers = new Array();

var x, x1, scroll, scroll1, textW, divW, p, called, called1, sText=""

//Browser detection
// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();
var is_gecko = (agt.indexOf('gecko') != -1);
var is_opera = (agt.indexOf("opera") != -1);


function onclickIE(idAttr,handler,call){
  if ((document.all)&&(document.getElementById)){idAttr[handler]=new Function(call)}
}

function addLink(id,call,txt){
  var e=document.createElement('A')
  e.setAttribute('href',top)
  e.setAttribute('onclick',call)
  var linktext=document.createTextNode(txt)
  e.appendChild(linktext)
  document.getElementById(id).appendChild(e)
}

function addControls(){
  addLink('controls','clickAction(0)',Stop)
  onclickIE(document.getElementById('controls').childNodes[0],"onclick",'clickAction(0)')
  document.getElementById('controls').appendChild(document.createTextNode(' | '))
  addLink('controls','clickAction(2)',Show)
  onclickIE(document.getElementById('controls').childNodes[2],"onclick",'clickAction(2)')
}

function stopScroller(){clearTimeout(scroll)}

function setAction(node,callvalue,txt){
  var c=document.getElementById('controls')
  c.childNodes[node].setAttribute('onclick','clickAction('+callvalue+')')
  onclickIE(document.getElementById('controls').childNodes[node],"onclick",'clickAction('+callvalue+')')
  c.childNodes[node].firstChild.nodeValue=txt
}

function clickAction(no){
  switch(no) {
    case 0:
      stopScroller()
      setAction(0,1,Start)
      setAction(2,2,Show)
      break
    case 1:
      startScroller()
      setAction(0,0,Stop)
      setAction(2,2,Show)
      break
    case 2:
      stopScroller()
      setAction(0,1,Start)
      setAction(2,3,Start)
      x=0
      document.getElementById('tag').style.whiteSpace='nowrap'
      document.getElementById('tag').style.left='0px'
      break
    case 3:
      startScroller()
      setAction(0,0,Stop)
      setAction(2,2,Show)
      x=divW
      document.getElementById('tag').style.left=x+'px'
  }
}

//The first scroller. Will call the second scroller as soon as its end reaches the right border of the scroller div.
function startScroller(){
    //Check if the text has reached the end of the screen.
  if (x < 1 - textW - offset) {
    //Text has reached the end of the screen. Stop scrolling. Will be restarted by the startScroller1() function.
  } else {
      if(document.getElementById('scroller')!=null)
	{
  	  divW=document.getElementById('scroller').offsetWidth
    }
    //Check if the end of the scroller text has reached the right border of the scroller div.
    //The startScroller1 function should only be invoked once. For that reasen, it also checks fo the "called"
  	//variable which will be set to 1 as soon as the startScroller1 function was been invoked once.
    if (x < 1 - (textW - divW) - offset && called == 0) {
      called=1
      called1=0
      x1=document.getElementById('scroller').offsetWidth
      //p.style.display='inline'
      setTimeout('startScroller1()',speed)
    }

    //Set new x coordinate
    x-=step
	if(document.getElementById('tag')!=null)
	{
    document.getElementById('tag').style.left=x+'px'
    }
    //Set new timeout
    scroll=setTimeout('startScroller()',speed)
  }
}

function helper(turn) {
	scrollers[turn].startScrolling();
}

function scroll(orginalElement, turn) {
	console.log('invoked. Turn: [' + turn + ']');

	this.turn = turn;

    //Create the second scroller and copy the HTML code from the first scroller.
    this.scroller=document.createElement('p');
    this.scroller.innerHTML = orginalElement.innerHTML;
    this.scroller.id ='scroller' + turn;

    this.called = 0;
    this.x = document.getElementById('scroller').offsetWidth;
	this.divW = document.getElementById('scroller').offsetWidth;


    //Style for the second scroller.
    this.scroller.style.position='relative';
    this.scroller.style.left = this.x+'px';
    this.scroller.style.whiteSpace='nowrap';
    //this.scroller.style.display='none';

    document.getElementById('scroller').appendChild(this.scroller)

    scrollers[turn] = this;

	this.startScrolling = function() {
		if (this.x < 1 - textW - offset) {
			//Text has reached the end of the screen. Stop scrolling. Will be restarted by the startScroller1() function.
			//document.getElementById('tag').style.display = 'none'
		} else {
			divW=document.getElementById('scroller').offsetWidth;

			//Check if the end of the scroller text has reached the right border of the scroller div.
			//The startScroller1 function should only be invoked once. For that reasen, it also checks fo the "called"
			//variable which will be set to 1 as soon as the startScroller1 function was been invoked once.
			if (this.x < 1 - (textW - this.divW) - offset && this.called == 0) {
				this.called = 1;
				var nextTurn = this.turn + 1;
			    nextScroller = new scroll(document.getElementById('tag'), nextTurn);
			    nextScroller.startScrolling();
			}

			//Set new x coordinate
			this.x-=step;
			this.scroller.style.left = this.x+'px';

			//Set new timeout
			scroll = setTimeout('helper(' + this.turn + ')',speed);
		}
	}
}

//The same as the startScroller function. Thats the second scroller, which profides an nonstop scrolling.
function startScroller1(){
  if (x1 < 1 - textW - offset) {
  	//Do nothing. Stop scrolling.
  } else {
      divW=document.getElementById('scroller').offsetWidth
	  if (x1 < 1 - (textW - divW) - offset && called1 == 0) {
	    called1=1
	    called=0
	    x=document.getElementById('scroller').offsetWidth
	    setTimeout('startScroller()',speed)
	  }

	  x1-=step
	  p.style.left=x1+'px'

	  scroll1=setTimeout('startScroller1()',speed)
  }
}

function initScroller(){
  if (document.getElementById && document.createElement && document.body.appendChild) {
    //addControls()
    if(document.getElementById('controls')!=null)
	{
    document.getElementById('controls').style.display='block';
	}
	if(document.getElementById('newsticker')!=null)
	{
    document.getElementById('newsticker').style.display='block';
    } 
    //Get the div width
	if(document.getElementById('scroller')!=null)
	{
    divW=document.getElementById('scroller').offsetWidth;
    x=divW;
    }
    //Get the text of the node
	if(document.getElementById('tag')!=null)
	{
    document.getElementById('tag').style.position='relative';
    document.getElementById('tag').style.left=divW+'px';
    document.getElementById('tag').style.whiteSpace='nowrap';
    }
    //Get the text width. Depending on the browser.
    if (is_gecko || is_opera) {
	   // JavaScript here for user agents implementing Gecko layout engine or Opera
	   if(document.getElementById('offset')!=null)
	{
	   textW = document.getElementById('offset').offsetLeft;
	   }
	} else  {
	   // JavaScript here for other browsers/engines
	   if(document.getElementById('tag')!=null)
	{
	   textW = document.getElementById('tag').offsetWidth;
	   }
	}

    //Create the second scroller and copy the HTML code from the first scroller.
    p=document.createElement('p')
	if(document.getElementById('tag')!=null)
	{
    p.innerHTML = document.getElementById('tag').innerHTML
	}
    p.id ='second'

    //Style for the second scroller.
    p.style.position='relative'
    //p.style.left=x+'px'
    //p.style.whiteSpace='nowrap'
    //p.style.display='none'

	if(document.getElementById('scroller')!=null)
	{
    document.getElementById('scroller').appendChild(p)
    }
    called=0
    scroll=setTimeout('startScroller()',speed)

//    Better version. Each ticker which reached the right side of the ticker instantiates a new scroller.
//    firstScroller = new scroll(document.getElementById('tag'), 0);
//    firstScroller.startScrolling();
  }
}

function addLoadEvent(func) {
  if (!document.getElementById | !document.getElementsByTagName) return
  var oldonload = window.onload
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload()
      func()
    }
  }
}

addLoadEvent(initScroller)
