var going = 0;
var altezza = 210;
var larghezza = 590;
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';
};

xbSetOpacityOrLeft = function (element, opacity, left)
{
  if (is_ie_win)
    {
      element.filters.alpha.enabled = 1;
      element.filters.alpha.Opacity = opacity > 0 ? opacity : 0;
    }
  else
    element.style.left = left + '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';
};

xbSetLeft = function (element, left)
{
  if (is_ie_win)
    element.filters.alpha.enabled = 0;
  element.style.left = left + 'px';
  element.style.width = (larghezza - left) + '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 ('animTop()', velocita);
}

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

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

  start();
}

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

  start();
}


do_animTop = 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;
}

do_animLeft = 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_left = (stato * stato + 34 * stato) / 20;
    xbSetOpacityOrLeft(old_dom, (stato - 40) * 10, new_left - larghezza);
    xbSetLeft(new_dom, new_left);
  }

  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 () { }
      animTop = function () { }
      do_animTop = function () { }
      //animLeft = function () { }
      //do_animLeft = 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 () { }
    animTop = function () { }
    do_animTop = function () { }
    //animLeft = function () { }
    //do_animLeft = function () { }
    documents.getElementById ('notizia0').style.display = 'none';
    documents.getElementById ('vuoto').style.display = 'none';
  }

