/* data.js - part of the dna-rna-amino package
 * Licensed under the MIT license found in the file LICENSE
 * If the text of the license is not included, refer to this
 * website for the terms of the license:
 *   http://www.opensource.org/licenses/mit-license.php
 */

/* Functions and objects for displaying data. Also includes
 * functions for getting data from HTML/user-interface elements.
 * Uses jQuery 1.2.6 in some spots.
 */

function view_dna_conversion () {
  var dna = getDNASequence();
  displayRNASequence(transcribeDNA(dna));
  if (isSequenceCorrectLength(dna))
    $("#dna-sequence div").removeClass("invalid");
  else
    $("#dna-sequence div").addClass("invalid");
}

function view_rna_conversion () {
  var rna = getRNASequence();
  displayDNASequence(reverseTranscribe(rna));
  if (isSequenceCorrectLength(rna))
    $("#rna-sequence div").removeClass("invalid");
  else
    $("#rna-sequence div").addClass("invalid");
}

function _tr (s) { return "<tr>" + s + "</tr>\n"; }
function _td (s) { return "<td>" + s + "</td>"; }

function hide_amino_acids () {
  $("#aminoAcids").hide();
  $("#aminoAcidTotals").hide();
}

function show_amino_acids (n) {
  if (n < 1000) {
    $("#aminoAcids").show(1, function () {
			    $("#aminoAcidTotals").show();
			      });
  }
  else {
    $("#aminoAcids").slideDown(n, function () {
				 $("#aminoAcidTotals").slideDown(1000);
			       });
    }
}

function view_amino_acids (results) {
  hide_amino_acids();
  $("#aminoAcids tbody").empty();
  for (var i = 0; i < results.length; i++) {
    var amino = results[i];
    //console.log("adding amino: %s", amino);
    //$("#aminoAcids").append("<li>" + amino.abbreviation + ", " + amino.molecularWeight + "</li>");
    $("#aminoAcids tbody").append(_tr(_td(amino.abbreviation)
				      + _td(amino.name)
				      + _td(amino.atomicWeightRatio)
				      + _td(amino.molecularWeight)
				      + _td(amino.kiloDaltonWeight)
				      + _td(amino.pK1)
				      + _td(amino.pK2)
				      + _td(amino.pL)
				     ));
  }
  $("#totalMolecularWeight").html(aminoTotal(results, "molecularWeight"));
  $("#totalKiloDaltonWeight").html(aminoTotal(results, "kiloDaltonWeight"));
  $("#totalPL").html(aminoTotal(results, "pL") / results.length);
  show_amino_acids(results.length * 100);
}

function getRNASequence () {
  return _cleanup_sequence($("#rnaString").val(), baseRNA);
}

function getDNASequence () {
  return _cleanup_sequence($("#dnaString").val(), baseDNA);
}

function displayRNASequence (s) {
  $("#rnaString").val(_cleanup_sequence(s, baseRNA));
}

function displayDNASequence (s) {
  $("#dnaString").val(_cleanup_sequence(s, baseDNA));
}

$(document).ready(function () {
  $("#dnaString").keyup(function (e) { view_dna_conversion(); });
  $("#rnaString").keyup(function (e) { view_rna_conversion(); });
  $("h2.load-local").cluetip({ local: true, cursor: "pointer",
			       hideLocal: false, arrows: true,
			       sticky: false,
			       positionBy: "mouse" });
  $("div.tooltip").hide();
  // Colour the rows of the amino acids table
  $("tr > td[class!=amino-grouping]:first-child").css("padding-left", "1.5em");
  hide_amino_acids();
  });
