/*
 * Javascript functions that manage
 * communication with AstexViewer.
 */

function av_execute(command){
  document.av.execute(command);
  window.status = command;
}

function av_color(color, from, to, state){
  var i=0;
  var residues= '';
  for (i=from;i<=to;i++){
    residues = residues+" "+i;
  }
  av_execute("color "+color+" residue "+residues+" ;");
  av_execute("schematic -name ribbon all;");
}

function av_color_bs(color, from, to, state){
  var i=0;
  var residues= '';
  for (i=from;i<=to;i++){
    residues = residues+" "+i;
  }
  av_execute("color "+color+" residue "+residues+" ;");
  av_execute("schematic -name ribbon all;");
  if(state == 'on'){
    av_execute("color "+color+" residue "+residues+" ;");
    av_execute('display sticks on residue '+residues+';');
  }
}

function av_show_selection(){
  var isSelected = 0;
  av_clear_color();
  av_execute('display sticks off all;');
  for (i=0; i<document.viewer_features.feature_check.length; i++){
    if (document.viewer_features.feature_check[i].checked==true){
      isSelected=1;
      var residues='';
      var parameters = document.viewer_features.feature_check[i].value.split(" ");
      var rangelist = parameters[0].split("+");
      for(j=0; j<rangelist.length; j++){
        var ranges = rangelist[j].split("-");
        if(ranges.length == 2){
          for(k=ranges[0];k<=ranges[1]; k++){
            residues = residues+" "+k;
          }
        }else{
          residues = residues+" "+rangelist[j];
        }
      }
      var color = hsvToRgb(i/(document.viewer_features.feature_check.length+5), 1, 1)
      color = Math.round(color[0])+","+Math.round(color[1])+","+Math.round(color[2]);
      if(parameters[1]=='bs'){
        av_execute("color '"+color+"' residue "+residues+" ;");
        av_execute("schematic -name ribbon all;");
        av_execute('display sticks on residue '+residues+';');
      }else{
        av_execute("color '"+color+"' residue "+residues+" ;");
        //av_execute("color blue residue "+residues+" ;");
        av_execute("schematic -name ribbon all;");
      }
    }
  }

  if(isSelected == 0){
    av_execute("color_by_rainbow aminoacid;");
    av_execute("secstruc all;");
    av_execute("schematic -name ribbon all;");
  }
}

function av_clear_color(){
    av_execute("color white aminoacid;");
    av_execute("schematic -name ribbon all;");

}

/**
 * Converts an HSV color value to RGB. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
 * Assumes h, s, and v are contained in the set [0, 1] and
 * returns r, g, and b in the set [0, 255].
 *
 * @param   Number  h       The hue
 * @param   Number  s       The saturation
 * @param   Number  v       The value
 * @return  Array           The RGB representation
 */
function hsvToRgb(h, s, v){
    var r, g, b;

    var i = Math.floor(h * 6);
    var f = h * 6 - i;
    var p = v * (1 - s);
    var q = v * (1 - f * s);
    var t = v * (1 - (1 - f) * s);

    switch(i % 6){
        case 0: r = v, g = t, b = p; break;
        case 1: r = q, g = v, b = p; break;
        case 2: r = p, g = v, b = t; break;
        case 3: r = p, g = q, b = v; break;
        case 4: r = t, g = p, b = v; break;
        case 5: r = v, g = p, b = q; break;
    }

    return [r * 255, g * 255, b * 255];
}


