

var n=2*1;
function changeme(choice){
		n=1*choice;
		document.getElementById("role").innerHTML=contents[n].role;
		document.getElementById("author").innerHTML=contents[n].author;
		document.getElementById("title").innerHTML=contents[n].title;
		document.getElementById("text").innerHTML=contents[n].text;
		document.getElementById("biog").innerHTML=contents[n].biog;
  	document.getElementById("sources").innerHTML= generateSourceLinks(n);
}


function findWorkIndexByTitle(title)
{
  for (var i = 0; i < contents.length; i++)
  {
    var c = contents[i];
    if (c.title == title)
    {
      return i;
    }
  }
  return null;
}

function generateSourceLinks(choiceNum)
{
  var sourceLinks = "";
  var choice = contents[choiceNum];
  if (choice != null && choice.sources != null)
  {
    for (var i = 0; i < choice.sources.length; i++)
    {
			if (i > 0)
      {
        sourceLinks += ", ";
      }
      var sourceWorkIndex = findWorkIndexByTitle(choice.sources[i]);
      var sourceWork = contents[sourceWorkIndex];
      if (sourceWork != null)
      {
			  sourceLinks += "<a href='javascript:changeme(" + sourceWorkIndex + ")'>" + sourceWork.title + "</a>";
      }
      else
      {
        sourceLinks += choice.sources[i];
      }
    }
  }
  return sourceLinks;
}


document.write("<form name='front_page'>");

for (var i = 0; i < contents.length; i++) {
while (contents[i].sources == "" && i < (contents.length-1)){
	         i++;
  } 
  document.write("<input name='radios' type='radio' value='" + i + "' onClick='changeme(this.value)'>" + contents[i].title+ " by " + contents[i].author + "</input>"+ " &nbsp;&nbsp;&nbsp;<br/>");
}
document.write("</form>");
document.write( "<p id='author'>"+ contents[n].author  + "</p><p id='role'>" + contents[n].role + "</p><p id='title'>" + contents[n].title + "</p><p id='text'>" + contents[n].text + "</p>Bio: <p id='biog'>" + contents[n].biog + "</p>Sources (if any): <p id='sources'>" + generateSourceLinks(n) + "</p>");

