var going = 0;
var altezza = 210;
var pausaTraDueNotizie = 70;
var velocita = 35;
var stato = 5 - pausaTraDueNotizie;
var i = 0;
var timeoutID;
var fermati = 0;
var old_dom;

var is_ie_mac = document.all && !window.print;
var is_ie_win = document.all && window.print;

if (!document.getElementById)
  document.getElementById = function (id) { 
    var elm = windowRef.document.all[id]; 
    return elm ? elm : null;
  };


xbSetOpacityOrTop = function (element, opacity, top)
{
  if (is_ie_win)
    {
      element.filters.alpha.enabled = 1;
      element.filters.alpha.Opacity = opacity > 0 ? opacity : 0;
    }
  else
    element.style.top = top + 'px';
};

xbSetTop = function (element, top)
{
  if (is_ie_win)
    element.filters.alpha.enabled = 0;
  element.style.top = top + 'px';
  element.style.height = (altezza - top) + 'px';
};

roll_to_next = function () {
  /* Aspetta che la prima notizia sia arrivata in cima prima di fermarsi */
  return !fermati || !old_dom;
}

start = function () {
  timeoutID = setTimeout ('anim()', velocita);
}

stop = function () {
  clearTimeout(timeoutID);
}

anim = function () {
  stop();
  if (roll_to_next () || stato >= 0)
    do_anim();
  else
    // Fa passare mezzo secondo da quando lascio il mouse alla prossima notizia
    stato = (500 / velocita) - pausaTraDueNotizie;

  start();
}

do_anim = function () {
  if (--stato <= -pausaTraDueNotizie) {
    // Passa alla notizia successiva, passando ad un altro oggetto DIV: new_dom
    // diventa la vecchia notizia.
    // Poi, resettando STATO a 50, old_dom diventa opaco e new_dom e' pronto
    // a scorrere di nuovo dal fondo.
    if (!going)
      {
	old_dom = document.getElementById ('vuoto');
	i = 0;
      }
    else
      {
	old_dom.style.zIndex = -1;
        old_dom = document.getElementById('notizia'+i);
	i = document.getElementById('notizia' + (i + 1)) ? (i + 1) : 0;
      }

    stato = 50;
    going = 1;
  }

  else if (!going)
    return;

  new_dom = document.getElementById('notizia'+i);

  // Se la notizia in uscita e' la stessa che sta entrando, c'e' una sola
  // notizia: esci subito.
  if (old_dom == new_dom)
    {
      stato = -1;
      return;
    }

  if (stato >= 0) {
    new_top = (stato * stato + 34 * stato) / 20;
    xbSetOpacityOrTop(old_dom, (stato - 40) * 10, new_top - altezza);
    xbSetTop(new_dom, new_top);
  }

  old_dom.style.zIndex = 1;
  new_dom.style.zIndex = 2;
}

onload = function () {
  if (document.layers || !document.getElementById ('notizia0'))
    {
      // Funzioni finte
      roll_to_next = function () { }
      start = function () { }
      stop = function () { }
      anim = function () { }
      do_anim = function () { }
    }
}

// IE/Mac rifiuta di cambiare pagina mentre e' attivo lo script.  Disabilitiamo
// lo script se riceviamo un click, e ripetiamo il click dopo 5 millisecondi.
if (is_ie_mac)
  {
    redoClickSoon = function ()
    {
      stop();
      setTimeout ('location.href = "' + this.href + '"', 5);
    }

    links = document.getElementsByTagName("A");
    for (i = 0; i < links.length; i++)
      links.item (i).onclick = redoClickSoon;
    roll_to_next = function () { }
    start = function () { }
    stop = function () { }
    anim = function () { }
    do_anim = function () { }
    documents.getElementById ('notizia0').style.display = 'none';
    documents.getElementById ('vuoto').style.display = 'none';
  }
