// config options
var pauseSeconds = 7;
var targetHeight = 230;

// Internal vars
var old = 1;
var scroller;
var scrollDist;
var oldOffset;
var newOffset; 
var xmlHttp;
var tmp;
var countD = 0;
var dontStartWait = 0;
var FF=0;
try {
  xmlHttp=new XMLHttpRequest();
} catch (e) {
  FF=0;
  try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      alert("Your browser does not support AJAX!");
    }
  }
}
getNewContent();
function getNewContent() {
        xmlHttp.onreadystatechange=setNewContent;
        var qs = "page=listing&amp;action=getRandom";
        xmlHttp.open("GET","/cgi-bin/caller.cgi?"+qs,true);
        xmlHttp.send(null);
}
function setNewContent() {
        if(xmlHttp.readyState==4) {
                var ret = xmlHttp.responseText;
		if(old == 1) {
			document.getElementById("newContent").innerHTML = ret;
			oldOffset = 0;
			newOffset = 0; 
			old = 0;
		} else {
			document.getElementById("oldContent").innerHTML = ret;
			oldOffset = targetHeight;
			newOffset = targetHeight*(-1); 
			old = 1;
		}
		scrollDist = 0;
		if(FF) {
			scroller = setInterval("moveUp()", 20);
		} else {
			scroller = setInterval("moveUp()", 40);
		}
		dontStartWait = 0;
	}
}
function moveUp() {
	// Calculate the delta to move our display DIV up by
	// slowly reduce the speed - so it comes to a smooth stop
	if(FF) {
		if(scrollDist >= targetHeight-1) {
			l_multi = 0.25;
		} else if(scrollDist >= targetHeight-9) {
			l_multi = 1;
		} else if(scrollDist >= targetHeight-15) {
			l_multi = 3;
		} else if(scrollDist >= targetHeight-40) {
			l_multi = 5;
		} else {
			l_multi = 8;
		}
	} else {
		if(scrollDist >= targetHeight-1) {
			l_multi = 1;
		} else if(scrollDist >= targetHeight-5) {
			l_multi = 2;
		} else if(scrollDist >= targetHeight-40) {
			l_multi = 10;
		} else {
			l_multi = 16;
		}
	}
	scrollDist += l_multi;
	document.getElementById("oldContent").style.top = (oldOffset-scrollDist)+"px";
	document.getElementById("newContent").style.top = (newOffset-scrollDist)+"px";
	// Were finished
	if(scrollDist >= targetHeight) {
		clearInterval(scroller);
		// Make sure the moving out div is clear of of the way
		if(old == 1) {
			document.getElementById("newContent").style.top = (newOffset-scrollDist-10)+"px";
		} else {
			document.getElementById("oldContent").style.top = (oldOffset-scrollDist-10)+"px";
		}
		// Start the countdown to the next page display
		if(dontStartWait == 0) {
			tmp = setTimeout("countDown()", 500);
		}
	}
}
function countDown() {
	tmp = undefined;
	countD++;
	if(countD >= (pauseSeconds*2)) {
		getNewContent();
		countD = 0;
	} else {
		if(dontStartWait == 0) {
			tmp = setTimeout("countDown()", 500);
		}
	}
}
function pauseScroll() {
	clearTimeout(tmp);
	tmp = undefined;
	dontStartWait = 1;
	document.getElementById("dynamicArea").style.border = "1px solid yellow";
}
function restartScroll() {
	dontStartWait = 0;
	if(tmp == undefined) {
		tmp = setTimeout("countDown()", 500);
	}
	document.getElementById("dynamicArea").style.border = "";
}
