/*
Embedded appositives (Annotated data)
Christopher Potts and Jesse Aron Harris, UMass Amherst
Created: 2009-03-29

This work is licensed under the Creative Commons
Attribution-Noncommercial-Share Alike 3.0 Unported License. To view a
copy of this license, visit
http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
Creative Commons, 171 Second Street, Suite 300, San Francisco,
California, 94105, USA.
*/

/*===================================================================*/
// General match function. Gathers user input and calls the
// appropriate search function.
function match(){
  // Clear any results that are currently displayed.
  document.getElementById('results').innerHTML = "";
  var hits = "";
  var userEmbeddings = getUserEmbeddings();
  var userPreds = getUserPreds();
  hits_and_hitCount = getMatches(userEmbeddings, userPreds);
  hits = hits_and_hitCount[0];
  hitCount = hits_and_hitCount[1];
  document.getElementById('results').innerHTML = hits;
  document.getElementById('resultsHeader').innerHTML = "Results (" + hitCount + " hits)";
}

/*===================================================================*/
// Gets the user's embedding choices.
function getUserEmbeddings(){
  var userEmbeddings = new Array();
  for(i=0; i < document.searchCriteria.embeddings.length; i++){
    if(document.searchCriteria.embeddings[i].checked){
      userEmbeddings.push(document.searchCriteria.embeddings[i].value);
    }
  }
  return(userEmbeddings)
}

/*===================================================================*/
// Gets the user's predicate choices.
function getUserPreds(){
  var userPreds = new Array();
  for(i=0; i < document.searchCriteria.preds.length; i++){
    if(document.searchCriteria.preds[i].checked){
      userPreds.push(document.searchCriteria.preds[i].value);
    }
  }
  return(userPreds)
}

/*===================================================================*/
// Gathers formatted hits, and keeps a running count of hits.
function getMatches(userEmbeddings, userPreds){  
  var hits = "";
  var hitCount = 0;
  for (i=1; i < corpus.length; i++){
    thisExample = corpus[i];
    thisExamplePreds = getPreds(thisExample);
    thisExampleEmbedding = getEmbedding(thisExample);
    if (checkEmbeddingMatch(userEmbeddings, thisExampleEmbedding) && checkPredMatch(userPreds, thisExamplePreds)){
      hits += formatExample(thisExample, i);
      hitCount += 1;
    }
  }
  return([hits, hitCount])
}

function getPreds(thisExample){
  return thisExample[0];
}

function getEmbedding(thisExample){
  var authorJudgment = thisExample[4][0];
  var judgment = authorJudgment;
  if(thisExample[5].length == 0){
    judgment = "DISPARITY";
  }
  else {
    var authorStatus = thisExample[4][2];
    var assessorOneJudgment = thisExample[5][0][0];
    var assessorOneStatus = thisExample[5][0][1];
    var assessorTwoJudgment = thisExample[5][1][0];
    var assessorTwoStatus = thisExample[5][1][1];
    if(authorStatus != "Good" || assessorOneStatus != "Good" || assessorTwoStatus != "Good"){    
      judgment = "DISPARITY";
    }
    if (authorJudgment != assessorOneJudgment || assessorOneJudgment != assessorTwoJudgment){
      judgment = "DISPARITY";
    }
  }
  return judgment;
}

/*===================================================================*/
// Returns true if there are no requested embedding restrictions or
// the example matches at least one of the chosen restrictions, else
// returns false.
function checkEmbeddingMatch(userEmbeddings, thisExampleEmbedding){
  var value = false;
  if(userEmbeddings.length == 0){
    value = true;
  }
  else {
    for (j=0; j < userEmbeddings.length; j++){
      if (thisExampleEmbedding == userEmbeddings[j]){
	value = true;
      }
    } 
  }
  return value;
}

/*===================================================================*/
// Returns true if there are no requested predicate restrictions or
// the example matches at least one of the chosen restrictions, else
// returns false.
function checkPredMatch(userPreds, thisExamplePreds){
  var value = false;
  if (userPreds.length == 0){
    value = true;
  }
  else {
    for (j=0; j < userPreds.length; j++){
      for (k=0; k < thisExamplePreds.length; k++){
	if (thisExamplePreds[k] == userPreds[j]){
	  value = true;
	}
      }
    }
  }
  return value;
}

/*===================================================================*/
// Format the example as HTML.
function formatExample(example, index){
  var str = "<dl>\n";
  str += "<dt>" + example[1] + "</dt>\n";
  str += "<dd><strong>Embedding predicate(s):</strong> " + example[0].join("; ") + "</dd>\n";
  str += "<dd><strong>Authors' annotations:</strong> " + formatAuthors(example[4]) + "</dd>\n";
  if (example[5].length > 0){
    str += "<dd><strong>Assessor 1's annotations:</strong> " + formatAssessor(example[5][0]) + "</dd>\n";
    str += "<dd><strong>Assessor 2's annotations:</strong> " + formatAssessor(example[5][1]) + "</dd>\n";  
  }
  str += "<dd class='sourcehead'><a href='javascript:void(0)' onclick=\"hideReveal('ex" + index + "')\">Reveal/hide the textual context</a><br />";
  str += "<div class='source' style='display:none;' id='ex" + index + "'>" + example[2] + "</div></dd>";  
  str += "<dd><em>Source:</em> " + example[3] + "</dd>\n";  
  str += "</dl>\n\n";
  return(str);
}

function formatAuthors(authors){

    var str = ""; 
    str += "<ul>\n";
    if (authors[2] != ""){
      str += "<li><em>Evidence status:</em> "  + authors[2] + "</li>\n";
    }
    str += "<li><em>Interpretation</em>: "   + authors[0] + "</li>\n";
    if (authors[1] != ""){
      str += "<li><em>Evidence:</em> "         + authors[1] + "</li>\n";
    }
    if (authors[3].length > 0){
      str += "<li><em>Evidence type(s):</em> " + authors[3].join("; ") + "</li>\n";
    }
    if (authors[4].length > 0){
      str += "<li><em>Notes:</em> " + authors[4].join("; ") + "</li>\n";
    }
    str += "</ul>";
    return(str);
}

function formatAssessor(assessor){
  var str = ""; 
  str += "<ul>\n";
  str += "<li><em>Evidence for:</em> " + assessor[0] + "</li>\n";
  str += "<li><em>Evidence status:</em> " + assessor[1] + "</li>\n";
  if (assessor[2] != ""){
    str += "<li><em>Notes</em>: " +  assessor[2] + "</li>\n";
  }
  str += "</ul>";
  return(str);
}

/*===================================================================*/
// Format the example as HTML.

function formClear(){
  // Clear all embedding boxes except the first..
  for(i=0; i < document.searchCriteria.embeddings.length; i++){
    document.searchCriteria.embeddings[i].checked = false;
  }
  // Clear all pred boxes.
  for(i=0; i < document.searchCriteria.preds.length; i++){
    document.searchCriteria.preds[i].checked = false;
  }
  // Clear all results.
  document.getElementById('results').innerHTML = "";
  document.getElementById('resultsHeader').innerHTML = "Results";
}

/*===================================================================*/
// Hide/reveal the full context for an example.
function hideReveal(id) {
  if (document.getElementById(id).style.display == "block" ) {
    document.getElementById(id).style.display = "none";
  }
  else {
    document.getElementById(id).style.display = "block";
  }
}
