// Author: Pierre Dragicevic

//<![CDATA[


function makethumbnails(data, columns) {

	const fields = 5;
	showAllAuthors = false; // Use et al.
	showURL = false; // URL in green, like Google.
	
	document.writeln('<table width=100% cellpadding=3 style=\"table-layout: fixed\">');

	var nmax = data.length / fields;
	var cells;
	if (nmax % columns == 0)
		cells = nmax;
	else
 		cells = nmax + columns - (nmax % columns);

  // -- Force columns of equal width
  for (var n=0; n<columns; n++) {
    document.writeln('<col width=\"' + (100/columns) + '%\" />');
  }
  
	document.writeln('<tr>');

	for (var n=0; n<cells; n++) {

		if (n > 0 && n % columns == 0) {
			document.writeln('</tr>');
			document.writeln('<tr>');
		}

		document.writeln('<td valign=\"top\" style=\"word-wrap:break-word\">');
		if (n < nmax) {
			
			if (data[n*fields+0].indexOf('toggle ') == 0) {
				// --- Thumbnail image
				document.writeln('<br><a href=\"\" onclick=\"toggleVisible(\'' + data[n*fields+0].substring(7) + '\');return false\"><img src=\"img/' + data[n*fields+1] + '\" style=\"border-style: none\"/></a>');
		
				// --- Title
				document.writeln('<br><a href=\"\" onclick=\"toggleVisible(\'' + data[n*fields+0].substring(7) + '\');;return false\"><b>' + data[n*fields+2] + '</b></a><br>');
			}
			else {
				// --- Thumbnail image
				document.writeln('<br><a href=\"' + data[n*fields+0] + '\"><img src=\"img/' + data[n*fields+1] + '\" style=\"border-style: none\"/></a>');
			
				// --- Title
				document.writeln('<br><a href=\"' + data[n*fields+0] + '\"><b>' + data[n*fields+2] + '</b></a><br>');
			}
			
			
			// --- Text
			document.writeln('<small>' + data[n*fields+4] + '</small>');
			
			// --- Authors and date
			if (data[n*fields+3].length > 0) {
				authors = data[n*fields+3].split(', ');
								
				// date
				date = "";
				if (!isNaN(authors[authors.length-1])) {
					date = authors.pop();
				}
				
				// et al
				etal = false;
				if (!showAllAuthors && authors.length > 2) {
					firstauthor = authors.shift();
					authors = new Array(firstauthor, "<acronym title=\"" + authors.join(', ') + "\">al.</acronym>");
					etal = true;
				}
			
				document.write('<br><small><span class=\"a\">(');
				for (var i=0; i<authors.length; i++) {
					document.write(authors[i]);
					if (i < authors.length - 2)
						document.write(', ');
					else if (i == authors.length - 2) {
						if (!etal)
							document.write(' and '); // and
						else
							document.write(' et '); // et
					}
				}
				if (date != "") {
					if (authors.length > 0)
						document.write(', ')
					document.write(date);
				}
				document.writeln(')</span></small> ');
			}
			
			// --- Green URL
			if (showURL) {
				url = data[n*fields+0];
				if (url.indexOf("://") > -1)
					url = url.substring(url.indexOf("://")+3);
				document.writeln('<br><small><span class=\"a\">' + url + '</span></small>');
			}
		}
		document.writeln('</td>');	
	}

	document.writeln('</tr>');
	document.writeln('</table>');
}

function toggleVisible(obj) {
	var el = document.getElementById(obj);
	if (el.style.display != "none")
		el.style.display = 'none';
	else
		el.style.display = '';
}

//]]>

