function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if (node == null) node = document;
    if (tag == null) tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
	if (pattern.test(els[i].className) ) {
	    classElements[j] = els[i];
	    j++; } }
    return classElements; }

function showpublications(c,y) {
    var pubclass = ['book', 'journal', 'conference', 
		    'dissertation', 'techrep' ]; 
    for (var i = 0; i < pubclass.length; i++) {
	var p = getElementsByClass( pubclass[i] );
	for (var j = 0; j < p.length; j++)
	    p[j].style.display = 'none';
	var q = new Array();
	if (y != '*') {
	    q.push(p[p.length - 1]);
	    for (var j = 0; j < p.length; j++ ) {
		var s = new RegExp("(^|\\s)" + y + "(\\s|$)");
		if ( s.test(p[j].className) && 
		     (c == '*' || c == pubclass[i] ) ) q.push(p[j]); } }
	else if (c == '*' || c == pubclass[i]) { 
	    q = p; q.pop(); }
	else q.push(p[p.length - 1]);
	for (var j = 0; j < q.length; j++)
	    q[j].style.display = 'list-item'; }
    return null; }

function showlistoftags(t,l) {
    var p = new Array();
    for (var i = 0; i < l.length; i++)
	p = p.concat( getElementsByClass( l[i] ) );
    if (t != '*') {
	var s = new RegExp("(^|\\s)" + t + "(\\s|$)");
	for (var i = 0; i < p.length; i++)
	    p[i].style.display = (s.test(p[i].className)) ? 
		'list-item': 'none'; 
	p = getElementsByClass( "ellipsis" ); }
    else { 
	q = getElementsByClass( "ellipsis" );
	for (var i = 0; i < q.length; i++)
	    q[i].style.display = 'none'; }
    for (var i = 0; i < p.length; i++)
	p[i].style.display = 'list-item';
    return null; }

function showprojects(_,t) {
    return showlistoftags(t, [ "year\\S+" ]); }

function showtheses(_,t) {
    return showlistoftags(t, [ "thesis", "proposal", "doing" ]); }

function showcourses(_,t) {
    return showlistoftags(t, [ "corso" ]); }

function toggleOn(x) {
    var p = document.getElementById(x);
    p.style.display = 'block';
    p = document.getElementById(x + "-P");
    p.style.display = 'none';
    p = document.getElementById(x + "-M");
    p.style.display = 'inline';
    return null;
}

function toggleOff(x) {
    var p = document.getElementById(x);
    p.style.display = 'none';
    p = document.getElementById(x + "-P");
    p.style.display = 'inline';
    p = document.getElementById(x + "-M");
    p.style.display = 'none';
    return null;
}

function yearToString(s) {return s.replace(/-/g,"/"); }

function linksToYear(t,m) {
    var p = getElementsByClass("year\\S+");
    var tag = new RegExp("(^|\\s)" + t + "(\\s|$)");
    var year = /year(\S+)/g;
    var s1 = '<a href="#" onclick="show' + m + '(\'';
    var s2 ='\',\'year';
    var s3 = s1 + ((t == "")?'*':t) + s2;
    var s4 = '\');">';
    var s5 = '</a> ';
    var r;
    var acc = [];
    var l = "<ul><li>" + s1 +  ((t == "")?'*':t) + '\',\'*' 
	+ s4 + 'All' + s5 + "</li></ul><p>";
    for (var i = 0; i < p.length; i++)
	if (t == "" || tag.test(p[i].className))
	    while ((r = year.exec(p[i].className)) != null) acc.push(r[1]);
    acc.sort(); acc.reverse();
    for (var i = 0, x = null; i < acc.length; i++)
	if (acc[i] != x) {
	    l += s3 + acc[i] + s4 + yearToString(acc[i]) + s5;
	    x = acc[i];	}
    return l + "</p>"; }

function locToString(s) {
    var r = s.replace(/_/g," ");
    return r.replace(/-(.*)$/," ($1)"); }

function linksToLocation(m) {
    var p = getElementsByClass("loc-\\S+");
    var loc = /loc-(\S+)/g;
    var s1 = '<li><a href="#" onclick="show' + m + '(\'\',\'loc-';
    var s2 = '\');">';
    var s3 = '</a></li>';
    var r;
    var acc = [];
    var l = "<ul>";
    for (var i = 0; i < p.length; i++)
	while ((r = loc.exec(p[i].className)) != null) acc.push(r[1]);
    acc.sort();
    for (var i = 0, x = null; i < acc.length; i++)
	if (acc[i] != x) {
	    l += s1 + acc[i] + s2 + locToString(acc[i]) + s3;
	    x = acc[i];	}
    return l + "</ul>"; }
