
  ///////////////////////////// Data for drawing chromosome
  function _chromosome() {
    var o=new Object(); 

    o.width=parseInt((cvs2.width/2-20)*(_getChr(Objects.Chr).size/_getChr(max).size));
    o.centromere=parseInt(o.width*(_getChr(Objects.Chr).centromere/_getChr(Objects.Chr).size).toFixed(2));

    o.pos=parseInt(o.width*((Objects.Pos*_cvtZoom(Objects.Zoom))/_getChr(Objects.Chr).size));
    o.area=parseInt(o.width*(Objects.Block*_cvtZoom(Objects.Zoom))/_getChr(Objects.Chr).size);

    if(o.pos<1)o.pos=1;
    if(o.area<1)o.area=1;

    // Start position of details meter
    var h=cvs2.width/2;

    // Draw meter
    var max=_last(Objects.Zoom)/Objects.Block;
    o.s1=parseInt((Objects.Pos-1)/Objects.Block*h/5);
    o.s2=parseInt((max-(Objects.Pos-1)/Objects.Block)*h/5-h/5);
    if(o.s1>h/5*2)o.s1=h/5*2;
    if(o.s2>h/5*2)o.s2=h/5*2;

    return o;
  }

  ///////////////////////////// Get maximum SNP gradation
  function _getMaxGraDP() {
    var en=new Object(JSON.parse(localStorage.getItem("_Env")));
    return en.depth_max;
  }
 
  ///////////////////////////// Get maximum SNP gradation
  function _getMaxGraSNP() {
    var en=new Object(JSON.parse(localStorage.getItem("_Env")));
    switch (Objects.Zoom) {
      case "100k":
        return en.snp_100k;
        break;
      case "10k":
        return en.snp_10k;
        break;
      case "1k":
        return en.snp_1k;
        break;
      case "500b":
        return en.snp_500b;
        break;
      case "200b":
        return en.snp_200b;
        break;
      case "100b":
        return en.snp_100b;
        break;
      case "10b":
        return en.snp_10b;
        break;
      case "1b":
        return en.snp_1b;
        break;
    }
  }

  ///////////////////////////// Get maximum INDEL gradation
  function _getMaxGraINDEL() {
    var en=new Object(JSON.parse(localStorage.getItem("_Env")));
    switch (Objects.Zoom) {
      case "100k":
        return en.indel_100k;
        break;
      case "10k":
        return en.indel_10k;
        break;
      case "1k":
        return en.indel_1k;
        break;
      case "500b":
        return en.indel_500b;
        break;
      case "200b":
        return en.indel_200b;
        break;
      case "100b":
        return en.indel_100b;
        break;
      case "10b":
        return en.indel_10b;
        break;
      case "1b":
        return en.indel_1b;
        break;
    }
  }

  ///////////////////////////// Convert zoom down
  function _cvtZoomUp(zoom) {
    switch (zoom) {
      case "10k":
        return "100k";
        break;
      case "1k":
        return "10k";
        break;
      case "500b":
        return "1k";
        break;
      case "200b":
        return "500b";
        break;
      case "100b":
        return "200b";
        break;
      case "10b":
        return "100b";
        break;
      case "1b":
        return "10b";
        break;
    }
  }

  ///////////////////////////// Convert zoom down
  function _cvtZoomDown(zoom) {
    switch (zoom) {
      case "100k":
        return "10k";
        break;
      case "10k":
        return "1k";
        break;
      case "1k":
        return "500b";
        break;
      case "500b":
        return "200b";
        break;
      case "200b":
        return "100b";
        break;
      case "100b":
        return "10b";
        break;
      case "10b":
        return "1b";
        break;
    }
  }

  ///////////////////////////// Convert zoom
  function _cvtZoom(zoom) {
    switch (zoom) {
      case "100k":
        return 100000;
        break;
      case "10k":
        return 10000;
        break;
      case "1k":
        return 1000;
        break;
      case "500b":
        return 500;
        break;
      case "200b":
        return 200;
        break;
      case "100b":
        return 100;
        break;
      case "10b":
        return 10;
        break;
      case "1b":
        return 1;
        break;
    }
  }

  ///////////////////////////// Convert length of chromosome as zoom
  function _checkPos(p,z){
    if(p<1) p=1;
    if(p+Objects.Block>_last(z)) p=_last(z)-Objects.Block+1;
    return p;
  }

  ///////////////////////////// Convert length of chromosome as zoom
  function _last(zoom){
    var l = parseInt(_getChr(Objects.Chr).size/_cvtZoom(zoom));
    if (_getChr(Objects.Chr).size%_cvtZoom(zoom)) l=l+1;
    return l;
  }

  ///////////////////////////// Convert length of chromosome as zoom with chromosome
  function _checkPos_Chr(t,z,c){
    var p=parseInt(t/_cvtZoom(z));
    if (t % _cvtZoom(z)) p++;
    if(p<1) p=1;
    if(p+Objects.Block>_last_Chr(z,c)) p=_last_Chr(z,c)-Objects.Block+1;
    return p;
  }

  ///////////////////////////// Convert length of chromosome as zoom with different chromosome
  function _last_Chr(zoom,chr){
    var l = parseInt(_getChr(chr).size/_cvtZoom(zoom));
    if (_getChr(chr).size%_cvtZoom(zoom)) return l=l+1;
    return l;
  }

  ////////////////////////////////////////////////////////
  // Sub functions for gradient
  ////////////////////////////////////////////////////////
  var grad_DP = gra_hexcolor('#f2f2f2','#000000');
  var grad_S = gra_hexcolor('#f0f8ff','#00ffff');
  var grad_I = gra_hexcolor('#ffffff','#008000');
  var grad_INDEL = gra_hexcolor('#FFCB8E','#A52A2A');
  function gra_rgb(b,a){ return gra(b,a,parse_rgb,build_rgb) }
  function gra_hexcolor(b,a){ return gra(b,a,parse_hexcolor,build_hexcolor) }
  function gra(b,a,parser,builder){
    var d = mop.apply(this,[b,a].map(parser));
    return function(i){ return builder([d[0](i),d[1](i),d[2](i)]); }
  }
  function mop(b,a){ return b.map(function(x,v){ var s=a[v]-x; return function(i){return Math.ceil(x+s*i) }}) }
  function parse_rgb(c){ var r=/rgb\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)/(c);
    return r&&r.shift()&&r.map(function(i){return parseInt(i)}) }
  function parse_hexcolor(c){ return [0,1,2].map(function(j){ return parseInt(c[j*2+1]+c[j*2+2],16) }) }
  function build_rgb(c){ return "rgb("+c+")" }
  function build_hexcolor(c){ return "#"+c.map(hex2).join("") }
  function hex2(i){return ((i<16)?"0":"")+i.toString(16)}
  Array.prototype.map=function(f){
    var ret=[];
    for(var i=0;i<this.length;i++) ret.push(f(this[i],i,this));
    return ret;
  }
  function _cvtColorAlpha(str) {
    R = hexToR(str);G = hexToG(str);B = hexToB(str);
    return "rgba("+R+","+G+","+B+","+0.6+")";
  }
  function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
  function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
  function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
  function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}

  ////////////////////////////////////////////////////////
  // Set menu bar
  ////////////////////////////////////////////////////////
  function _setMenu(){

      // Set chromosome on main page
      var o=new Object(JSON.parse(localStorage.getItem("_Chrs")));
      var en=new Object(JSON.parse(localStorage.getItem("_Env")));
      var ld="";
      for (var i=0;i<o.length;i++){
        ld=ld+"<option value='"+o[i].id+"'>"+en.chromosome_name+o[i].id+"</option>";
      }
      $('#prm_chr').find('option').remove().end().append(ld);
      $('#prm_chr').val(localStorage.getItem('_Chr'));
      $('#menu .zoom').text("Block size "+localStorage.getItem('_Zoom'));
      switch(localStorage.getItem('_Mode')){
        case 'dp': m="DEPTH"; break;
        case 'dp+s': m="DEPTH +SNP"; break;
        case 'dp+i': m="DEPTH +INDEL"; break;
        case 'dp+a': m="DEPTH +ALL"; break;
        case 's': m="SNP only"; break;
        case 'i': m="INDEL only"; break;
        case 'si': m="SNP + INDEL"; break;
      }
      $('#menu .mode').text(m);
      if (localStorage.getItem('_Eff') == "off"){
        $('#menu .eff').text("snpEff OFF (e)")
        $('#menu .eff').css("color", "white");
      } else {
        $('#menu .eff').text("snpEff ON (e)")
        $('#menu .eff').css("color", "yellow");
      }
      if (localStorage.getItem('_Snpblock') == "off"){
        $('#menu .snpblock').text("Snpblock OFF (b)")
        $('#menu .snpblock').css("color", "white");
      } else {
        $('#menu .snpblock').text("Snpblock ON (b)")
        $('#menu .snpblock').css("color", "yellow");
      }
      if (localStorage.getItem('_Null') == "true"){
        $('#menu .null').text("Highlight DEPTH=NULL ON (n)")
        $('#menu .null').css("color", "yellow");
      } else {
        $('#menu .null').text("Highlight DEPTH=NULL OFF (n)")
        $('#menu .null').css("color", "white");
      }
      if (localStorage.getItem('_Amino') == "off"){
        $('#menu .amino').text("Amino acids OFF")
        $('#menu .amino').css("color", "white");
      } else {
        $('#menu .amino').text("Amino acids ON")
        $('#menu .amino').css("color", "yellow");
      }
      if (localStorage.getItem('_Indicator') == "off"){
        $('#menu .indicator').text("Indicator OFF (d)")
        $('#menu .indicator').css("color", "white");
      } else {
        $('#menu .indicator').text("Indicator ON (d)")
        $('#menu .indicator').css("color", "yellow");
      }
  }

  ////////////////////////////////////////////////////////
  // Set color
  ////////////////////////////////////////////////////////
  function _setColor(){
    var o=new Object();
    if(localStorage.getItem('_Color')=="0"){
      o.bgcolor="#595454";
      o.tracktitle="khaki";
      o.metercolor="#fff";
      o.meterstring="#fff";
      o.pointercolor="#fff";
      o.pointerstringcolor="#ffffff";
      o.chrcolor="rgb(60,179,113)";
      o.chrborder="#ffffff";
      o.chrstringcolor="rgb(255,252,250)";
      o.chrareacolor="rgba(245,0,0,0.6)";
      o.chrareaselected="rgba(245,222,179,0.4)";
      o.detaillinecolor="rgba(0,150,0,0.5)";
      o.detailarea="rgba(245,0,0,0.6)";
      o.detailareastroke="#fff";
      o.detailstring="#fff";
      o.detailupstring="#000";
      o.detailareaselected="rgba(245,222,179,0.5)";
      o.connectline="yellow";
      o.title="#f8f8ff";
      o.reftitle="#ffff00";
      o.snptitle="khaki";
      o.fasta="rgb(245,245,220)";
      o.amino="rgb(245,245,220)";
      o.fastabold="rgb(245,245,0)";
      o.locusid="rgb(255,255,255)";
      o.highlight="rgba(0,255,0,0.3)";
      o.centeringtrack="rgba(255,255,255,0.3)";
      o.centeringcell="rgba(0,0,0,0.3)";
    } else if(localStorage.getItem('_Color')=="1") {
      o.bgcolor="#778899";
      o.tracktitle="khaki";
      o.metercolor="#bebebe";
      o.meterstring="beige";
      o.pointercolor="#fff";
      o.pointerstringcolor="#ffffff";
      o.chrcolor="rgb(0,150,0)";
      o.chrborder="#ffffff";
      o.chrstringcolor="rgb(255,252,250)";
      o.chrareacolor="rgba(245,0,0,0.6)";
      o.chrareaselected="rgba(245,222,179,0.4)";
      o.detaillinecolor="rgba(0,150,0,0.5)";
      o.detailarea="rgba(245,0,0,0.6)";
      o.detailareastroke="#fff";
      o.detailstring="#fff";
      o.detailupstring="#000";
      o.detailareaselected="rgba(245,222,179,0.5)";
      o.connectline="#000";
      o.title="#ffffff";
      o.reftitle="#ffff00";
      o.snptitle="khaki";
      o.fasta="rgb(245,245,220)";
      o.amino="rgb(245,245,220)";
      o.fastabold="rgb(245,245,0)";
      o.locusid="rgb(255,255,255)";
      o.highlight="rgba(0,255,0,0.3)";
      o.centeringtrack="rgba(255,255,255,0.3)";
      o.centeringcell="rgba(0,0,0,0.3)";
    } else if(localStorage.getItem('_Color')=="2") {
      o.bgcolor="#f8f8ff";
      o.tracktitle="#000000";
      o.metercolor="#000000";
      o.meterstring="#000000";
      o.pointercolor="#000000";
      o.pointerstringcolor="#000000";
      o.chrcolor="rgb(255,140,0)";
      o.chrborder="#000000";
      o.chrstringcolor="rgb(0,0,0)";
      o.chrareacolor="rgba(0,250,0,0.6)";
      o.chrareaselected="rgba(255,255,255,0.5)";
      o.detaillinecolor="rgba(255,140,0,0.9)";
      o.detailarea="rgba(60,179,113,0.6)";
      o.detailareastroke="#000";
      o.detailstring="#000";
      o.detailupstring="rgb(0,0,0)";
      o.detailareaselected="rgba(255,255,255,0.5)";
      o.connectline="#000";
      o.title="#000000";
      o.reftitle="brown";
      o.snptitle="#000000";
      o.fasta="rgb(0,0,0)";
      o.amino="rgb(0,0,0)";
      o.fastabold="brown";
      o.locusid="rgb(0,0,0)";
      o.highlight="rgba(0,255,0,0.4)";
      o.centeringtrack="rgba(255,255,255,0.3)";
      o.centeringcell="rgba(0,0,0,0.3)";
    }
    return o;
  }

  ///////////////////////////// Convert string of amino
  function _get_amino(str) {
    var aminoStr='';
    switch (str.toUpperCase()) {
      case "AAA":aminoStr="K";break;
      case "AAC":aminoStr="N";break;
      case "AAG":aminoStr="K";break;
      case "AAT":aminoStr="N";break;
      case "ACA":aminoStr="T";break;
      case "ACC":aminoStr="T";break;
      case "ACG":aminoStr="T";break;
      case "ACT":aminoStr="T";break;
      case "AGA":aminoStr="R";break;
      case "AGC":aminoStr="S";break;
      case "AGG":aminoStr="R";break;
      case "AGT":aminoStr="S";break;
      case "ATA":aminoStr="I";break;
      case "ATC":aminoStr="I";break;
      case "ATG":aminoStr="M";break;
      case "ATT":aminoStr="I";break;
      case "CAA":aminoStr="Q";break;
      case "CAC":aminoStr="H";break;
      case "CAG":aminoStr="Q";break;
      case "CAT":aminoStr="H";break;
      case "CCA":aminoStr="P";break;
      case "CCC":aminoStr="P";break;
      case "CCG":aminoStr="P";break;
      case "CCT":aminoStr="P";break;
      case "CGA":aminoStr="R";break;
      case "CGC":aminoStr="R";break;
      case "CGG":aminoStr="R";break;
      case "CGT":aminoStr="R";break;
      case "CTA":aminoStr="L";break;
      case "CTC":aminoStr="L";break;
      case "CTG":aminoStr="L";break;
      case "CTT":aminoStr="L";break;
      case "GAA":aminoStr="E";break;
      case "GAC":aminoStr="D";break;
      case "GAG":aminoStr="E";break;
      case "GAT":aminoStr="D";break;
      case "GCA":aminoStr="A";break;
      case "GCC":aminoStr="A";break;
      case "GCG":aminoStr="A";break;
      case "GCT":aminoStr="A";break;
      case "GGA":aminoStr="G";break;
      case "GGC":aminoStr="G";break;
      case "GGG":aminoStr="G";break;
      case "GGT":aminoStr="G";break;
      case "GTA":aminoStr="V";break;
      case "GTC":aminoStr="V";break;
      case "GTG":aminoStr="V";break;
      case "GTT":aminoStr="V";break;
      case "TAA":aminoStr="*";break;
      case "TAC":aminoStr="Y";break;
      case "TAG":aminoStr="*";break;
      case "TAT":aminoStr="Y";break;
      case "TCA":aminoStr="S";break;
      case "TCC":aminoStr="S";break;
      case "TCG":aminoStr="S";break;
      case "TCT":aminoStr="S";break;
      case "TGA":aminoStr="*";break;
      case "TGC":aminoStr="C";break;
      case "TGG":aminoStr="W";break;
      case "TGT":aminoStr="C";break;
      case "TTA":aminoStr="L";break;
      case "TTC":aminoStr="F";break;
      case "TTG":aminoStr="L";break;
      case "TTT":aminoStr="F";break;
      default: aminoStr="-";break;
    }
    return aminoStr;
  }

