// Accessible Scroller by Mike Foskett (http://www.websemantics.co.uk/). Retain this message and you may use the code freely.
// Aggiunte e Modifiche (c) 2008 Massimo Magnano

var speed=50        // speed of scroller
var step=3          // smoothness of movement
var insControls=false; //Vuoi i Controlli start /stop /show ?
var top="#TextScroller_top"      // name of anchor used as page top when clicking start / stop / show
var Start= "Start"  // Text for start link
var Stop = "Stop"   // Text for stop link
var Show = "Show"   // Text for show link

var x, scroll, divW, sText=""

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(){
 if (insControls) {	
  addLink('TextScroller_controls','clickAction(0)',Stop)
  onclickIE(document.getElementById('TextScroller_controls').childNodes[0],"onclick",'clickAction(0)')
  document.getElementById('TextScroller_controls').appendChild(document.createTextNode(' | '))
  addLink('TextScroller_controls','clickAction(2)',Show)
  onclickIE(document.getElementById('TextScroller_controls').childNodes[2],"onclick",'clickAction(2)')
 }
}

function stopScroller(){clearTimeout(scroll)}

function setAction(node,callvalue,txt){
 if (insControls) {	
  var c=document.getElementById('TextScroller_controls')
  c.childNodes[node].setAttribute('onclick','clickAction('+callvalue+')')
  onclickIE(document.getElementById('TextScroller_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('TextScroller_tag').style.whiteSpace='normal'
      document.getElementById('TextScroller_tag').style.left='0px'
      break
    case 3:
      startScroller()
      setAction(0,0,Stop)
      setAction(2,2,Show)
      x=divW
      document.getElementById('TextScroller_tag').style.left=x+'px'
  }
}

function startScroller(){
  var txt_tag=document.getElementById('TextScroller_tag')
  txt_tag.style.whiteSpace='nowrap'
  document.getElementById('TextScroller_scroller').style.visibility='visible'
  var p=document.createElement('P')
  p.id='TextScroller_testP'
  //p.style.fontSize='25%' //fix for mozilla. multiply by 4 before using
  x-=step
  if (txt_tag.className) p.className=txt_tag.className
  p.appendChild(document.createTextNode(sText))
  document.body.appendChild(p)
  pw=p.offsetWidth
  document.body.removeChild(p)
  //if (x<(pw*8)*-1){ x=divW;  }  originale
  if (x<pw*-1){   //txt_tag.offsetWidth*-1 č pių corretto , ma funziona solo con explorer
    x=divW;    
	  //alert(pw+' '+txt_tag.offsetWidth+' '+txt_tag.offsetHeight);
  }
  
  txt_tag.style.left=x+'px'
  scroll=setTimeout('startScroller()',speed)
}

function initScroller(){
  if (document.getElementById && document.createElement && document.body.appendChild) {
    addControls()
    if (insControls) { document.getElementById('TextScroller_controls').style.display='block' }
    divW=document.getElementById('TextScroller_scroller').offsetWidth
    x=divW
    document.getElementById('TextScroller_tag').style.position='relative'
    document.getElementById('TextScroller_tag').style.left=divW+'px'
    var ss=document.getElementById('TextScroller_tag').childNodes
    for (i=0;i<ss.length;i++) {sText+=ss[i].nodeValue+" "}
	
	//alert(document.getElementById('TextScroller_tag').offsetWidth+' '+document.getElementById('TextScroller_tag').offsetHeight);
    scroll=setTimeout('startScroller()',speed)
  }
}

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)
