// Accessible Scroller by Mike Foskett (http://www.websemantics.co.uk/). Retain this message and you may use the code freely.

var speed=20;        // speed of scroller
var step=1;          // smoothness of movement
var top="#top";      // name of anchor used as page top when clicking start / stop / show
var x, scroll, sText="";
var startX=800;
var divScrollLength=0;     // lunghezza del div scorrevole

function onclickIE(idAttr,handler,call){
  if ((document.all)&&(document.getElementById)){
    idAttr[handler]=new Function(call);
  }
}

function addLink(id,call,txt,title){
  var e=document.createElement('A');
  e.setAttribute('href',top);
  e.setAttribute('title',title);
  e.setAttribute('onclick',call);
  var linkIMG=document.createElement('IMG');
  linkIMG.setAttribute('alt',title);
  linkIMG.setAttribute('src',txt);
  e.appendChild(linkIMG);
  document.getElementById(id).appendChild(e);
}

function stopScroller(){
  clearTimeout(scroll);
}

function setAction(callvalue,txt, title){
  var html='';
  if (callvalue==1) {
     html="<a href='#top' onclick='javascript:clickAction("+callvalue+");' title='avvia lo scorrimento'><img src='/consiglioweb/img/play.png' alt='avvia lo scorrimento' /></a>"+
    '<a  href="archivio_flash24.php" title="vai all'+"'"+'archivio ora per ora">'+"<img src='/consiglioweb/img/show.png' alt='mostra tutte le notizie' /></a>";
  }else{
     html="<a href='#top' onclick='javascript:clickAction("+callvalue+");' title='ferma lo scorrimento'><img src='/consiglioweb/img/pausa.png' alt='ferma lo scorrimento' /></a>"+
    '<a  href="archivio_flash24.php" title="vai all'+"'"+'archivio ora per ora">'+"<img src='/consiglioweb/img/show.png' alt='mostra tutte le notizie' /></a>";
  }

  $sethtml('controls',html);
}

function clickAction(no){
  switch(no) {
    case 0:
      stopScroller();
      setAction(1,'/consiglioweb/img/play.png', 'avvia lo scorrimento');
      break;
    case 1:
      startScroller();
      setAction(0,'/consiglioweb/img/pausa.png', 'ferma lo scorrimento');
      break;
    case 2:
      stopScroller();
      setAction(1,'/consiglioweb/img/play.png', 'avvia lo scorrimento');
      x=0;
      document.getElementById('flash24scroller').style.whiteSpace='normal';
      document.getElementById('flash24scroller').style.left='0px';
      break;
    case 3:
      startScroller();
      setAction(0,'/consiglioweb/img/pausa.png', 'ferma lo scorrimento');
      x=startX;
      document.getElementById('flash24scroller').style.left=x+'px';
  }
}

function startScroller(){
  document.getElementById('flash24scroller').style.whiteSpace='nowrap';
  var p=document.createElement('P');
  p.id='flash24scroller';
  p.style.fontSize='25%'; //fix for mozilla. multiply by 4 before using
  x-=step;
  if (document.getElementById('flash24scroller').className) {
      p.className=document.getElementById('flash24scroller').className;
  }
  if (divScrollLength==0) {
       divScrollLength=document.getElementById('flash24scroller').offsetWidth;
  }
  if (x<-divScrollLength){
     x=startX;
     //ajaxGetRand("inc/flash24getter.php", showFlash24, "flash24scroller");
     divScrollLength=document.getElementById('flash24scroller').offsetWidth;
  }

  document.getElementById('flash24scroller').style.left=x+'px';
  scroll=setTimeout('startScroller()',speed);
}

function initScroller(){
  //ajaxGetRand("inc/flash24getter.php", showFlash24, "flash24scroller");
  showFlash24("flash24scroller");
  if (document.getElementById && document.createElement && document.body.appendChild) {
    document.getElementById('controls').style.display='block';
    x=startX;
    document.getElementById('flash24scroller').style.position='relative';
    document.getElementById('flash24scroller').style.left=startX+'px';
    var ss=document.getElementById('flash24scroller').childNodes;
    for (i=0;i<ss.length;i++) {
       sText+=ss[i].nodeValue+" ";
    }
    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()
    }
  }
}

/*
versione usata con AJAX

function showFlash24(html, htmlElementId) {
    textLenght=0;
  var links='';

  var newsArray = eval(html);

  for (var currNewsArray in newsArray) {
        links+='<a href="'+newsArray[currNewsArray][1]+'" title="leggi la notizia" onclick="javascript:clearTimeout(scroll);">'+newsArray[currNewsArray][0]+'</a> * ';
        arrayElem =newsArray[currNewsArray][0];
        textLenght+=arrayElem.length;
  }

  $sethtml(htmlElementId,links);
}
*/

function showFlash24(htmlElementId) {
  textLenght=0;
  var links='';

  // newsArray deve essere un vettore di news già esistente.

  for (var currNewsArray in newsArray) {
        links+='<a href="'+newsArray[currNewsArray][1]+'" title="leggi la notizia" onclick="javascript:clearTimeout(scroll);">'+newsArray[currNewsArray][0]+'</a> * ';
        arrayElem =newsArray[currNewsArray][0];
        textLenght+=arrayElem.length;
  }

  $sethtml(htmlElementId,links);
}
addLoadEvent(initScroller)
