// randimage.js: display random poster
// Colorado State University Libraries
// Greg Vogl 2007-06-08
function randImage() {
	// get posters data from scripts/posters.js into myarray
	// select a random poster
	i = Math.floor(Math.random()*(myarray.length-1));
	// get poster fields from myarray
	var title = myarray[i][0];
	var id = myarray[i][1];
	var author = myarray[i][2];
	var country = myarray[i][3];
	var filename = myarray[i][4];
	var dir = "http://digital.library.colostate.edu/poster/image/";
	var imgURL = dir + filename;
	var thumbURL = dir + 'icon' + filename.toLowerCase();
	var cdm3URL = "http://digital.library.colostate.edu/cgi-bin/pquery.exe?" 
		+ "CISOROOT=/poster&CISOOP=all&CISOFIELD1=poster&CISOROWS=2&CISOCOLS=5&"
		+ "CISORESTMP=/poster/items.html&CISOVIEWTMP=/poster/item.html&CISOBOX1=" + id;
	var imageHTML = '<a href="' + cdm3URL + '" title="' + title 
	  + '" onmouseover="mouseOverImage()" onmouseout="setTimeouts()">'
	  + '<img src="' + thumbURL + '" border="0" alt="' + title + '" />'
	  + '<span id=\"randimagetext\"><strong>' + title + '</strong><br />'
	  + author + '<br />'
	  + country + '<br />'
	  + id 
	  + '</span></a>';
	return imageHTML;
}
function setOpacity(i) {
	var p = document.getElementById('randimagep').style;
	p.opacity = i/100; // newer Firefox and Mozilla
	p.MozOpacity = i/100; // older Firefox and Mozilla
	p.KhtmlOpacity = i/100; // Safari, Konqueror
	p.filter = "alpha(opacity=" + i + ")"; // IE
}
var timeoutFade;
var timeoutChange;
var timeoutFading;
function mouseOverImage() {
	i = timeoutFading;
	clearTimeouts(); 
	if (i > 0 && i < 100) 
		setTimeout('fadeIn()', 15*i);
}
function setOpacityTimeout(i) {
	setOpacity(i);
	timeoutFading = i;
}
function fadeOut() {
	for (i=100; i>=0; i--)
		setTimeout('setOpacityTimeout(' + i + ')', 15*(100-i));
}
function fadeIn() {
	for (i=0; i<=100; i++)
		setTimeout('setOpacity(' + i + ')', 5*i);
}
function fadeOut2(i) {
	setOpacity(i);
	if (i>0) timeoutFade = setTimeout("fadeOut2(" + (i-1) + ")", 15);
}
function clearTimeouts() {
	clearTimeout(timeoutFade); 
	clearTimeout(timeoutChange);
}
function setTimeouts() {
	timeoutFading = 100;
	timeoutFade = setTimeout("fadeOut()", 8500); 
	timeoutChange = setTimeout("changeImage()", 10000);
}
function changeImage() {
	document.getElementById('randimagep').innerHTML = randImage();
	fadeIn();
	setTimeouts();
}
changeImage();
