// JavaScript to create a cloud and frequency distribution display 
// of provided text
// Written 2006-10-22 by Chris Zagar <chris.zagar@emcmail.maricopa.edu>

function tag_transform(mysrc, mydst, myfunc)
{
  if (myfunc == 'clear') {
    document.getElementById(mysrc).value = "";
    document.getElementById(mydst).innerHTML = "";
    return false;
  }
  
  var cloud = myfunc == 'cloud';
  var freq  = myfunc == 'freq';
  
  if (! cloud && ! freq) {
    alert("tags_transform unknown function: " + myfunc);
    return false;
  }

  var distinctWordCount = 0;
  var totalWordCount = 0;
  var aWords = new Array();
  var words = new Object();
  var wordCount = new Object();
  var omit = { a:1, an:1, the:1, and:1 };
  var max = 0;
  var globs = document.getElementById(mysrc).value.split(/[^a-zA-Z']/);
  for (i = 0; i < globs.length; i++) {
    word = globs[i].replace(/[^a-zA-Z']/g, "");
    if (word != "") {
      lowerWord = word.toLowerCase();
      if (lowerWord in words) {
        wordCount[lowerWord]++;
      } else {
        words[lowerWord] = new Object();
        wordCount[lowerWord] = 1;
        aWords.push(lowerWord);
      }
      if (word in words[lowerWord]) {
        words[lowerWord][word]++;
      } else {
        words[lowerWord][word] = 1;
      }
      if (lowerWord in omit)
        continue;
      if (wordCount[lowerWord] > max) {
        max = wordCount[lowerWord];
      }
    }
  }
  var feedback = "";
  var fontSize;
  var fontColor;
  var wordFrequency;
  aWords = aWords.sort();
  for (j = max; j > 0 && (j == max || freq); j--) {
    for (i = 0; i < aWords.length; i++) {
      lowerWord = aWords[i];
      if (freq && wordCount[lowerWord] != j)
        continue;
      distinctWordCount++;
      totalWordCount += wordCount[lowerWord];
      if (lowerWord in omit)
  	  continue;
      // Find most frequently appearing capitalization version
      // In a tie on frequency, if one is the all lower-case version, favor it
      // so if a common word appears once capitalized and once not, assume
      // it shouldn't be capitalized
      wordFrequency = 0;
      for (wordVariation in words[lowerWord]) {
        if (wordFrequency < words[lowerWord][wordVariation] ||
            (wordFrequency == words[lowerWord][wordVariation] &&
             wordVariation.toLowerCase() == wordVariation)) {
          word = wordVariation;
          wordFrequency = words[lowerWord][wordVariation];
        }
      }
      if (freq) {
        wordPct = Math.round(200 * wordCount[lowerWord] / max);
        feedback += "<tr><td>" + word + "</td><td width=\"*\"><div class=\"bar\" style=\"width: " + wordPct +
          ";\">" + wordCount[lowerWord] + "</div></td></tr>";
  
        continue;
      }
      fontSize = Math.round(40 * wordCount[lowerWord] / max) + 8;
      fontColor = (128 - Math.round(127 * wordCount[lowerWord] / max)).toString(16);
      if (fontColor.length == 1) {
        fontColor = "0" + fontColor;
      }
      feedback += "<a style=\"font-size: " + fontSize + "px; " +
        "color: #" + fontColor + fontColor + fontColor + "; margin-right: 6px;\"" +
        "target=\"_blank\" href=\"http://www.onelook.com/?w=" + word + "&ls=a\"" +
        "title=\"" + wordCount[lowerWord] + "\">" +
        word + "</a> \n";
    }
  }
  mydstelem = document.getElementById(mydst);
  if (feedback != "") {
    summary = "<p>Distinct words: " + distinctWordCount + "<br>" +
        "Total words: " + totalWordCount + "</p>";
    if (freq) {
      feedback = "<table class=\"graph\">" + feedback + "</table>" + summary;
    } else {
      feedback = "<div class=\"tagslink\">" +
        "<p>" + feedback + "</p>" +
        summary +
        "</div>";
    }
    mydstelem.innerHTML = feedback;
  } else {
    mydstelem.innerHTML = "";
  }
  return false;
}
