var primo = 1;
var altezza = 210;
var pausaTraDueNotizie = 70;
var velocita = 35;
var stato = 5 - pausaTraDueNotizie;
var i = 0;
var timeoutID;
var fermati = 0;

var has_alpha = document.all && navigator.appVersion.indexOf("Mac") == -1;

if (document.layers)
{
  // Funzioni finte
  roll_to_next = function () { }
  start = function () { }
  stop = function () { }
  anim = function () { }
  do_anim = function () { }
}
else
{
  xbSetInnerHTML = function (element, str) 
  { 
    var txt = str + '<br><img src="/common/images/img_homepage/spacer.gif" width="150">';
    if (typeof(element.innerHTML) != 'undefined') 
      element.innerHTML = txt; 
  };
  
  xbSetOpacityOrTop = function (element, opacity, top)
  {
    if (has_alpha)
      element.filters.alpha.Opacity = opacity > 0 ? opacity : 0;
    else
      element.style.top = top + 'px';
  };

  xbSetTop = function (element, top)
  {
    element.style.top = top + 'px';
    element.style.height = (altezza - top) + 'px';
  };

  if (!document.getElementById)
    document.getElementById = function (id) { 
      var elm = windowRef.document.all[id]; 
      return elm ? elm : null;
    };

  roll_to_next = function () {
    return !fermati && num_notizie > 1;
  }

  start = function () {
    if (num_notizie != 0)
      timeoutID = setTimeout ('anim()', roll_to_next () || stato > 0 ? velocita : 500);
  }

  stop = function () {
    clearTimeout(timeoutID);
  }

  anim = function () {
    stop();
    do_anim();
    start();
  }

  do_anim = function () {
    old_dom = document.getElementById('old');
    new_dom = document.getElementById('new');

    if (stato-- == 0 && !roll_to_next ()) {
      // Ferma la rotazione se ho solo una notizia o se il mouse sta sopra la notizia
      stato = 0;
    }

    else if (stato <= -pausaTraDueNotizie) {
      // Passa alla notizia successiva, scambiando i due oggetti DIV: old_dom
      // acquisisce il testo della vecchia notizia, new_dom quello della nuova.
      // Poi, resettando STATO a 50, old_dom diventa opaco e new_dom e' pronto
      // a scorrere di nuovo dal fondo.
      if (!primo)
        {
          xbSetInnerHTML(old_dom, notizie[i]);
          i = (i + 1) % num_notizie;
        }

      xbSetInnerHTML(new_dom, notizie[i]);
      stato = 50;
      primo = 0;
    }

    if (stato >= 0) {
      new_top = (stato * stato + 34 * stato) / 20;
      xbSetOpacityOrTop(old_dom, (stato - 40) * 10, new_top - altezza);
      xbSetTop(new_dom, new_top);
    }
  }
}
