// banner2.js

onload = iniDelay;

function showMessage(n, show) {
  var whichEl = (NS4) ? eval("message" + n) :
                        eval("message" + n + ".style");
  whichEl.visibility = (show) ? ((NS4) ? "show" : "visible") :
                                ((NS4) ? "hide" : "hidden");
}
//
//-----------------------------------------------------------
// nextMessage
//
function nextMessage(){
	scrollBanner();
}
//
//-----------------------------------------------------------
// moveUp
//
function moveUp() {
	moveItems(currentLeadingObjIdx, objElement.length);
	if (currentLeadingObjIdx > 0) {
		moveItems(0, currentLeadingObjIdx);
	}
	//moveUpVikar();
}
//
//-----------------------------------------------------------
// moveItems
//
function moveItems(firstIdx, lastIdx){
	var pP;
	var pE;
	var bMovedRecently = 0;
	for (i = firstIdx; i < lastIdx; i++) {
		pE = objElement[i];		
		if(bMovedRecently == 0){
			if (i == firstIdx){
				pE.style.pixelTop -= increment;
			}
			else {
				pE.style.pixelTop = pP.style.pixelTop + pP.clientHeight + topPadding;
			}
			pP = pE;
			if (pE.style.pixelTop + pE.clientHeight < 0) {  //har nådd toppen, og er på vei ut av listen
				curNumPauseMsg += 1;
				objElement[currentLeadingObjIdx].style.pixelTop = objElement[objElement.length - 1].style.pixelTop + objHeight;
				currentLeadingObjIdx += 1; //øker pekeren til hvilket element som er øverst	
								
				if(currentLeadingObjIdx >= objElement.length - 1){
					currentLeadingObjIdx = 0; //tilbakestiller pekeren
				}
				if(curNumPauseMsg >= maxNumPauseMsg){
					
					curNumPauseMsg = 0;
					clearInterval(intervalID);
					timeoutID = setTimeout("nextMessage()", pause);
				}
				bMovedRecently = 1;		
			}		
		}
		else{
			pE.style.pixelTop -= increment;
		}
	}
}

function scrollBanner() {
	intervalID = setInterval("moveUp()", interval);
}

function makeIE() {
  // assign the necessary code to a variable
  var text = '<DIV ID="banner" STYLE="position:absolute;z-index:105">';
  for (var i = ar.length - 1; i >= 0; i--) {
    text += '<DIV ID="message' + i + 
            '" STYLE="position:absolute"></DIV>';
  }
  text += '</DIV>';

  // insert the code before the end of the document
  document.body.insertAdjacentHTML("BeforeEnd", 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;
  }
currentObjTop = 2; //bannerTop + bannerHeight - objHeight;

  // define the child elements' properties
  for (i = 0; i < ar.length; i++) {
    with (eval("message" + i + ".style")) {
      pixelLeft = leftPadding;
      pixelTop = currentObjTop;
      width = bannerWidth - leftPadding;
      backgroundColor = bannerColor;
    }
  }
  
}

function fillBanner() {
  var whichEl;
    for (var i = 0; i < ar.length; i++) {
      whichEl = eval("message" + i);
      whichEl.innerHTML = ar[i];
      if( i > 0){ 
			whichEl.style.pixelTop = objElement[i-1].style.pixelTop + objElement[i-1].clientHeight + topPadding;
	  }
	  else{
			whichEl.style.pixelTop = topPadding;
	  }
      objElement[i] = whichEl;
    }
}
function iniDelay(){
	makeIE();
	fillBanner();
	current = 0;
	timeoutID = setTimeout("startBanner()", lIniDelay);
}
function startBanner() {
  moveItems(0, objElement.length - 1);
  scrollBanner();
}
