// AJAX components for content.ci.pomona.ca.us
// kudos to tutorials by IBM

// initialize the request XMLHttpRequest variable
var request = false;
var theInterval = "";

function createRequest() {
	try {
		request = new XMLHttpRequest();
	} catch(e) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(failed) {
				request = false;
			}
		}
	}

	if (!request) {
		alert("Error initializing XMLHttpRequest");
	}
}

function getRandomImage() {

	// IE caching workaraound:
	// since IE caches same requests, we must create a timestamp
	// in order for it to think it's different every time.
	var daDate = new Date();
	var daTime = daDate.getTime();

	var url = "/ajax/random.php?&time=" + daTime;
	request.open("GET", url, true);
	request.onreadystatechange = updateImage;
	request.send(null);
}

function updateImage() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var response = request.responseText;
			// expecting collection|alias|DMRECORD|TITLE
			dataArray = response.split("|");			

			var newHTML = '<div align="center"><strong>From the &quot;' + dataArray[0] + '&quot;</strong></div>';
			newHTML += '<div align="center">';
			newHTML += '<a href="/cdm4/item_viewer.php?CISOROOT=' + dataArray[1] + '&CISOPTR=' + dataArray[2] + '">';
			newHTML += dataArray[3] + '</a></div>';
			newHTML += '<div align="center"><a href="/cdm4/item_viewer.php?CISOROOT=' + dataArray[1] + '&CISOPTR=' + dataArray[2] + '">';
			newHTML += '<img border="0" src="/cgi-bin/thumbnail.exe?CISOROOT=' + dataArray[1] + '&CISOPTR=' + dataArray[2] + '">';
			newHTML += '</a></div>';

			document.getElementById("RotatingImage").innerHTML = newHTML;
		} else {
			alert(request.status);
		}
	}
}

function StartParade() {
	// call the first one, then set an interval to get the parade going.
	createRequest();
	getRandomImage();
	theInterval = window.setInterval("getRandomImage()", 30000);
}

