// banner.js

/*
INPUT:
ar è l'array delle linee di testo
IMG ID=holdspace è il punto su cui piazzare il nuovo DIV absolute

OUTPUT:
DIV ID=banner
timeoute routine
*/

function showMessage(n, show) {
  var whichEl = (NS4) ? eval("message" + n) :
                        fetch_object( "message" + n );
  whichEl.style.visibility = (show) ? ((NS4) ? "show" : "visible") :
                                      ((NS4) ? "hide" : "hidden");
}

function nextMessage() {
  var fromInd = current;
  current = (fromInd == ar.length - 1) ? 0 : fromInd + 1;
  scrollBanner(fromInd, current);
}

function moveUp() {
  if (NS4) {
    fromEl.top -= increment;
    if (toEl.top - increment <= toElTarget) {
      toEl.top = toElTarget;
      clearInterval(intervalID);
      fromEl.visibility = "hide";
      timeoutID = setTimeout("nextMessage()", pause);
    } else {
      toEl.top -= increment;
    }
  } else {
    fromEl.style.pixelTop -= increment;
    if (toEl.style.pixelTop - increment <= toElTarget) {
      toEl.style.pixelTop = toElTarget;
      clearInterval(intervalID);
      fromEl.style.visibility = "hidden";
      timeoutID = setTimeout("nextMessage()", pause);
    } else {
      toEl.style.pixelTop -= increment;
    }
  }
}

function scrollBanner(from, to) {
  if (NS4) {
    fromEl = eval("message" + from);
    toEl = eval("message" + to);
    toEl.top = fromEl.top + bannerHeight;
    toElTarget = fromEl.top;
  } else {
    fromEl = fetch_object( "message" + from );
    toEl = fetch_object( "message" + to );
    toEl.style.pixelTop = fromEl.style.pixelTop + bannerHeight;
    toElTarget = fromEl.style.pixelTop;
  }
  showMessage(to, true); // show the upcoming message
  intervalID = setInterval("moveUp()", interval);
}

function makeIE() {
  // assign the necessary code to a variable
  var text = '';
  for (var i = ar.length - 1; i >= 0; i--) {
    text += '<DIV ID="message' + i +
            '" STYLE="position:absolute"></DIV>';
  }
  // insert the code before the end of the document
  var obj = fetch_object( "banner" );
  obj.innerHTML = text;

  // define the main element's properties
//  with (banner.style) {
//    width = bannerWidth;
//    height = bannerHeight;
//    clip = "rect(0 " + bannerWidth + " " + bannerHeight + " 0)";
//    backgroundColor = bannerColor;
//    pixelLeft = bannerLeft;
//    pixelTop = bannerTop;
//  }

  // define the child elements' properties
  for (i = 0; i < ar.length; i++) {
      obj = fetch_object( "message" + i );
      obj.style.visibility = "hidden";
      obj.style.pixelLeft = leftPadding;
      obj.style.pixelTop = topPadding;
      obj.style.width = bannerWidth - leftPadding;
      obj.style.backgroundColor = bannerColor;
  }
}

function makeNS() {
  // create the main element
  banner = new Layer(bannerWidth);

  // define the main element's properties
  with (banner) {
    clip.right = bannerWidth;
    clip.bottom = bannerHeight;
    document.bgColor = bannerColor;
    left = bannerLeft;
    top = bannerTop;
    visibility = "show";
  }

  // define the child elements' properties
  for (var i = 0; i < ar.length; i++) {
    // create a child element
    eval("message" + i + " = " + 
         "new Layer(bannerWidth - leftPadding, banner)");
    with(eval("message" + i)) {
      visibility = "hide";
      left = leftPadding;
      top = topPadding;
      document.bgColor = bannerColor;
    }
  }
}

function fillBanner() {
  var whichEl;
  if (NS4) {
    for (var i = 0; i < ar.length; i++) {
      whichEl = eval("message" + i);
      whichEl.document.write(ar[i]);
      whichEl.document.close();
    }
  } else {
    for (var i = 0; i < ar.length; i++) {
      var whichEl = fetch_object( "message" + i );
      whichEl.innerHTML = ar[i];
    }
  }
}

function startBanner() {

//  bannerHeight = bannerHeight - 2;
  if (bannerLeft == 0)
      bannerLeft = 359;
  if (bannerTop == 0)
      bannerTop = 0;

  if (NS4)
    makeNS()
  else
    makeIE();
  fillBanner();
  showMessage(0, true);
  current = 0;
  timeoutID = setTimeout("nextMessage()", pause);
}
