/*
* tools.js
* http://tasuke.dna.affrc.go.jp/
*
* Author: Jungsok Kim, Ryutaro Itoh
*
* Copyright (c) 2013 National Institute of Agrobiological Sciences   
*
* This software is released under the MIT License.
*
* http://opensource.org/licenses/mit-license.php
*/

///////////////////////////// Data for drawing chromosome
function _chromosome() {
  var o=new Object(); 
  o.width=parseInt((cvs2.width/2-20)*(_getChr(Objects.Chr).size/_getChr("max").size));
  if(o.width < 72){
    o.width=72;
  }
  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;
}

////////////////////////////// sort by key of object
function _sortByKey(a,b) {
  var keys = Array();
  var ret = new Object;
  
  for (var prop in a){keys.push(prop);}
  keys.sort();
  for (var key in keys){if(keys[key] != "remove"){ret[key]=keys[key];}}
  return ret;
}

///////////////////////////// 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 "90k":
      return en.snp_90k;
      break;
    case "80k":
      return en.snp_80k;
      break;
    case "70k":
      return en.snp_70k;
      break;
    case "60k":
      return en.snp_60k;
      break;
    case "50k":
      return en.snp_50k;
      break;
    case "40k":
      return en.snp_40k;
      break;
    case "30k":
      return en.snp_30k;
      break;
    case "20k":
      return en.snp_20k;
      break;
    case "10k":
      return en.snp_10k;
      break;
    case "9k":
      return en.snp_9k;
      break;
    case "8k":
      return en.snp_8k;
      break;
    case "7k":
      return en.snp_7k;
      break;
    case "6k":
      return en.snp_6k;
      break;
    case "5k":
      return en.snp_5k;
      break;
    case "4k":
      return en.snp_4k;
      break;
    case "3k":
      return en.snp_3k;
      break;
    case "2k":
      return en.snp_2k;
      break;
    case "1k":
      return en.snp_1k;
      break;
    case "900b":
      return en.snp_900b;
      break;
    case "800b":
      return en.snp_800b;
      break;
    case "700b":
      return en.snp_700b;
      break;
    case "600b":
      return en.snp_600b;
      break;
    case "500b":
      return en.snp_500b;
      break;
    case "400b":
      return en.snp_400b;
      break;
    case "300b":
      return en.snp_300b;
      break;
    case "200b":
      return en.snp_200b;
      break;
    case "100b":
      return en.snp_100b;
      break;
    case "90b":
      return en.snp_90b;
      break;
    case "80b":
      return en.snp_80b;
      break;
    case "70b":
      return en.snp_70b;
      break;
    case "60b":
      return en.snp_60b;
      break;
    case "50b":
      return en.snp_50b;
      break;
    case "40b":
      return en.snp_40b;
      break;
    case "30b":
      return en.snp_30b;
      break;
    case "20b":
      return en.snp_20b;
      break;
    case "10b":
      return en.snp_10b;
      break;
    case "9b":
      return en.snp_9b;
      break;
    case "8b":
      return en.snp_8b;
      break;
    case "7b":
      return en.snp_7b;
      break;
    case "6b":
      return en.snp_6b;
      break;
    case "5b":
      return en.snp_5b;
      break;
    case "4b":
      return en.snp_4b;
      break;
    case "3b":
      return en.snp_3b;
      break;
    case "2b":
      return en.snp_2b;
      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 "90k":
      return en.indel_90k;
      break;
    case "80k":
      return en.indel_80k;
      break;
    case "70k":
      return en.indel_70k;
      break;
    case "60k":
      return en.indel_60k;
      break;
    case "50k":
      return en.indel_50k;
      break;
    case "40k":
      return en.indel_40k;
      break;
    case "30k":
      return en.indel_30k;
      break;
    case "20k":
      return en.indel_20k;
      break;
    case "10k":
      return en.indel_10k;
      break;
    case "9k":
      return en.indel_9k;
      break;
    case "8k":
      return en.indel_8k;
      break;
    case "7k":
      return en.indel_7k;
      break;
    case "6k":
      return en.indel_6k;
      break;
    case "5k":
      return en.indel_5k;
      break;
    case "4k":
      return en.indel_4k;
      break;
    case "3k":
      return en.indel_3k;
      break;
    case "2k":
      return en.indel_2k;
      break;
    case "1k":
      return en.indel_1k;
      break;
    case "900b":
      return en.indel_900b;
      break;
    case "800b":
      return en.indel_800b;
      break;
    case "700b":
      return en.indel_700b;
      break;
    case "600b":
      return en.indel_600b;
      break;
    case "500b":
      return en.indel_500b;
      break;
    case "400b":
      return en.indel_400b;
      break;
    case "300b":
      return en.indel_300b;
      break;
    case "200b":
      return en.indel_200b;
      break;
    case "100b":
      return en.indel_100b;
      break;
    case "90b":
      return en.indel_90b;
      break;
    case "80b":
      return en.indel_80b;
      break;
    case "70b":
      return en.indel_70b;
      break;
    case "60b":
      return en.indel_60b;
      break;
    case "50b":
      return en.indel_50b;
      break;
    case "40b":
      return en.indel_40b;
      break;
    case "30b":
      return en.indel_30b;
      break;
    case "20b":
      return en.indel_20b;
      break;
    case "10b":
      return en.indel_10b;
      break;
    case "9b":
      return en.indel_9b;
      break;
    case "8b":
      return en.indel_8b;
      break;
    case "7b":
      return en.indel_7b;
      break;
    case "6b":
      return en.indel_6b;
      break;
    case "5b":
      return en.indel_5b;
      break;
    case "4b":
      return en.indel_4b;
      break;
    case "3b":
      return en.indel_3b;
      break;
    case "2b":
      return en.indel_2b;
      break;
    case "1b":
      return en.indel_1b;
      break;
  }
}

///////////////////////////// Convert zoom down
function _cvtZoomUp(zoom) {
  switch (zoom) {
    case "90k":
      return "100k";
      break;
    case "80k":
      return "100k";
      break;
    case "70k":
      return "100k";
      break;
    case "60k":
      return "100k";
      break;
    case "50k":
      return "100k";
      break;
    case "40k":
      return "50k";
      break;
    case "30k":
      return "50k";
      break;
    case "20k":
      return "50k";
      break;
    case "10k":
      return "20k";
      break;
    case "9k":
      return "10k";
      break;
    case "8k":
      return "10k";
      break;
    case "7k":
      return "10k";
      break;
    case "6k":
      return "10k";
      break;
    case "5k":
      return "10k";
      break;
    case "4k":
      return "5k";
      break;
    case "3k":
      return "5k";
      break;
    case "2k":
      return "5k";
      break;
    case "1k":
      return "2k";
      break;
    case "900b":
      return "1k";
      break;
    case "800b":
      return "1k";
      break;
    case "700b":
      return "1k";
      break;
    case "600b":
      return "1k";
      break;
    case "500b":
      return "1k";
      break;
    case "400b":
      return "500b";
      break;
    case "300b":
      return "500b";
      break;
    case "200b":
      return "500b";
      break;
    case "100b":
      return "200b";
      break;
    case "90b":
      return "100b";
      break;
    case "80b":
      return "100b";
      break;
    case "70b":
      return "100b";
      break;
    case "60b":
      return "100b";
      break;
    case "50b":
      return "100b";
      break;
    case "40b":
      return "50b";
      break;
    case "30b":
      return "50b";
      break;
    case "20b":
      return "50b";
      break;
    case "10b":
      return "20b";
      break;
    case "9b":
      return "10b";
      break;
    case "8b":
      return "10b";
      break;
    case "7b":
      return "10b";
      break;
    case "6b":
      return "10b";
      break;
    case "5b":
      return "10b";
      break;
    case "4b":
      return "5b";
      break;
    case "3b":
      return "5b";
      break;
    case "2b":
      return "5b";
      break;
    case "1b":
      return "2b";
      break;
  }
}

///////////////////////////// Convert zoom down
function _cvtZoomDown(zoom) {
  switch (zoom) {
    case "100k":
      return "50k";
      break;
    case "90k":
      return "50k";
      break;
    case "80k":
      return "50k";
      break;
    case "70k":
      return "50k";
      break;
    case "60k":
      return "50k";
      break;
    case "50k":
      return "20k";
      break;
    case "40k":
      return "20k";
      break;
    case "30k":
      return "20k";
      break;
    case "20k":
      return "10k";
      break;
    case "10k":
      return "5k";
      break;
    case "9k":
      return "5k";
      break;
    case "8k":
      return "5k";
      break;
    case "7k":
      return "5k";
      break;
    case "6k":
      return "5k";
      break;
    case "5k":
      return "2k";
      break;
    case "4k":
      return "2k";
      break;
    case "3k":
      return "2k";
      break;
    case "2k":
      return "1k";
      break;
    case "1k":
      return "500b";
      break;
    case "900b":
      return "500b";
      break;
    case "800b":
      return "500b";
      break;
    case "700b":
      return "500b";
      break;
    case "600b":
      return "500b";
      break;
    case "500b":
      return "200b";
      break;
    case "400b":
      return "200b";
      break;
    case "300b":
      return "200b";
      break;
    case "200b":
      return "100b";
      break;
    case "100b":
      return "50b";
      break;
    case "90b":
      return "50b";
      break;
    case "80b":
      return "50b";
      break;
    case "70b":
      return "50b";
      break;
    case "60b":
      return "50b";
      break;
    case "50b":
      return "20b";
      break;
    case "40b":
      return "20b";
      break;
    case "30b":
      return "20b";
      break;
    case "20b":
      return "10b";
      break;
    case "10b":
      return "5b";
      break;
    case "9b":
      return "5b";
      break;
    case "8b":
      return "5b";
      break;
    case "7b":
      return "5b";
      break;
    case "6b":
      return "5b";
      break;
    case "5b":
      return "2b";
      break;
    case "4b":
      return "2b";
      break;
    case "3b":
      return "2b";
      break;
    case "2b":
      return "1b";
      break;
  }
}

///////////////////////////// Convert zoom
function _cvtZoom(zoom) {
  switch (zoom) {
    case "100k":
      return 100000;
      break;
    case "90k":
      return 90000;
      break;
    case "80k":
      return 80000;
      break;
    case "70k":
      return 70000;
      break;
    case "60k":
      return 60000;
      break;
    case "50k":
      return 50000;
      break;
    case "40k":
      return 40000;
      break;
    case "30k":
      return 30000;
      break;
    case "20k":
      return 20000;
      break;
    case "10k":
      return 10000;
      break;
    case "9k":
      return 9000;
      break;
    case "8k":
      return 8000;
      break;
    case "7k":
      return 7000;
      break;
    case "6k":
      return 6000;
      break;
    case "5k":
      return 5000;
      break;
    case "4k":
      return 4000;
      break;
    case "3k":
      return 3000;
      break;
    case "2k":
      return 2000;
      break;
    case "1k":
      return 1000;
      break;
    case "900b":
      return 900;
      break;
    case "800b":
      return 800;
      break;
    case "700b":
      return 700;
      break;
    case "600b":
      return 600;
      break;
    case "500b":
      return 500;
      break;
    case "400b":
      return 400;
      break;
    case "300b":
      return 300;
      break;
    case "200b":
      return 200;
      break;
    case "100b":
      return 100;
      break;
    case "90b":
      return 90;
      break;
    case "80b":
      return 80;
      break;
    case "70b":
      return 70;
      break;
    case "60b":
      return 60;
      break;
    case "50b":
      return 50;
      break;
    case "40b":
      return 40;
      break;
    case "30b":
      return 30;
      break;
    case "20b":
      return 20;
      break;
    case "10b":
      return 10;
      break;
    case "9b":
      return 9;
      break;
    case "8b":
      return 8;
      break;
    case "7b":
      return 7;
      break;
    case "6b":
      return 6;
      break;
    case "5b":
      return 5;
      break;
    case "4b":
      return 4;
      break;
    case "3b":
      return 3;
      break;
    case "2b":
      return 2;
      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_INDEL = gra_hexcolor('#FFCB8E','#A52A2A');
var grad_CSTM_array= new Array();
grad_CSTM_array[0] = gra_hexcolor("#f5e9f7","#a229b8");
grad_CSTM_array[1] = gra_hexcolor("#e6edf1", "#094c78");
grad_CSTM_array[2] = gra_hexcolor("#f7f3e5", "#b88b04");
grad_CSTM_array[3] = gra_hexcolor("#eaf0ea", "#326b2d");
function gra_rgb(b,a){ return gra(b,a,parse_rgb,build_rgb) }
function gra_hexcolor(b,a){
  var ret;
  try{
    ret = gra(b,a,parse_hexcolor,build_hexcolor);
  }catch(e){
    _dlg_reset_confirm();
  }
  return ret;
}
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="";
  var mode_name = "";
  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'));
  if(!_isUndefined(Objects.Conditions)){
    if(Objects.Conditions.length > 1){
      var condArray = localStorage.getItem("_CrtCondition").split("|");
      var cond = parseInt(condArray[0]);
      mode_name = en.cstm_name.substr(0, 10)+" : "+Objects.Conditions[cond].id.substr(0, 10);
      //mode_name = $.format("%30s",mode_name);
    }else{
      mode_name = en.cstm_name;
    }
  }else{
    mode_name = en.cstm_name;
  }
/*
  var headicon = "<i class=\"fa fa-sliders\" aria-hidden=\"true\" style=\"display:none;\"></i><i class=\"fa fa-bar-chart\" aria-hidden=\"true\"></i><i class=\"fa fa-check-square\" aria-hidden=\"true\" style=\"display:none;\"></i> ";
  var tailicon = " <i class=\"fa fa-caret-down\" aria-hidden=\"true\"></i>";
  switch(localStorage.getItem('_Mode')){
    case 'dp': 
      if(localStorage.getItem('_CSTM')=="on"){
        m=_escapeHTML(mode_name);
      }else {
        m="DEPTH";
      }
      break;
    case 'dp+s': 
      if(localStorage.getItem('_CSTM')=="on"){
        m=_escapeHTML(mode_name)+" + SNP";
      }else{
        m="DEPTH +SNP";
      }
      break;
    case 'dp+i': 
      if(localStorage.getItem('_CSTM')=="on"){
        m=_escapeHTML(mode_name)+" + INDEL";
      }else{
        m="DEPTH +INDEL";
      }
      break;
    case 'dp+a': 
      if(localStorage.getItem('_CSTM')=="on"){
        m=_escapeHTML(mode_name)+" + ALL";
      }else{
        m="DEPTH +ALL";
      }
      break;
    case 's': m="SNP only"; break;
    case 'i': m="INDEL only"; break;
    case 'si': m="SNP + INDEL"; break;
    case 'hs': m="exp"; break;
    case 'hi': m="exp"; break;
    case 'hd': m="exp"; break;
  }
  $('#menu .mode').empty();
  $('#menu .mode').append(headicon+m+tailicon);
  $('#menu .mode').css("width","140");
*/

  ///Available within 4k///
  /*
  var zoom = _cvtZoom(localStorage.getItem("_Zoom"));
  //alert(zoom);
  if(zoom > 4999) {
	 $('#menu .one').css("color", "rgb(128, 128, 128)");
  } else {
	$('#menu .one').css("color", "black");
  }
  */
  $('#menu .one').css("color", "black");

  var onchk = "<i class=\"fa fa-circle\" aria-hidden=\"true\"></i> ";
  var offchk = "<i class=\"fa fa-circle-o\" aria-hidden=\"true\"></i> ";

  if (localStorage.getItem('_sFlg') == "1"){
    $('#menu .menuLisSnp').empty()
    $('#menu .menuLisSnp').append(onchk+"<i id=\"effbox\" style=\"background-color:#00ffff;width:10px;height:12px;display:inline-block;\"></i>SNP : ON (s)")
    $('#menu .menuLisSnp').css("color", "black");
  } else {
    $('#menu .menuLisSnp').empty()
    $('#menu .menuLisSnp').append(offchk+" <i id=\"effbox\" style=\"background-color:#00ffff;width:10px;height:12px;display:inline-block;\"></i>SNP : OFF (s)")
    $('#menu .menuLisSnp').css("color", "black");
  }
  if (localStorage.getItem('_iFlg') == "1"){
    $('#menu .menuLisIndel').empty()
    $('#menu .menuLisIndel').append(onchk+" <i id=\"effbox\" style=\"background-color:#A52A2A;width:10px;height:12px;display:inline-block;\"></i>INDEL : ON (i)")
    $('#menu .menuLisIndel').css("color", "black");
  } else {
    $('#menu .menuLisIndel').empty()
    $('#menu .menuLisIndel').append(offchk+" <i id=\"effbox\" style=\"background-color:#A52A2A;width:10px;height:12px;display:inline-block;\"></i>INDEL : OFF (i)")
    $('#menu .menuLisIndel').css("color", "black");
  }
  if (localStorage.getItem('_dpFlg') == "1"){
    $('#menu .menuLisDP').empty()
    $('#menu .menuLisDP').append(onchk+" <i id=\"effbox\" style=\"background-color:#b2b2b2;width:10px;height:12px;display:inline-block;\"></i>DEPTH : ON (p)")
    $('#menu .menuLisDP').css("color", "black");
  } else {
    $('#menu .menuLisDP').empty()
    $('#menu .menuLisDP').append(offchk+" <i id=\"effbox\" style=\"background-color:#b2b2b2;width:10px;height:12px;display:inline-block;\"></i>DEPTH : OFF (p)")
    $('#menu .menuLisDP').css("color", "black");
  }

  var offEff = "<table CELLSPACING=0 CELLPADDING=0 style=\"border-collapse: collapse;border-spacing: 0;\"><tr><td><i class=\"fa fa-circle-o\" aria-hidden=\"true\"></i>&nbsp;</td><td><table CELLSPACING=0 CELLPADDING=0 style=\"border-collapse: collapse;border-spacing: 0;\"><tr><td><i style=\"background-color:#00BFFF;width:5px;height:6px;display:block;\"></i></td><td><i style=\"background-color:#FFFF00;width:5px;height:6px;display:block;\"></i></td></tr><tr><td><i style=\"background-color:#EE9A00;width:5px;height:6px;display:block;\"></i></td><td><i style=\"background-color:red;width:5px;height:6px;display:block;\"></i></td></tr></table></td><td>snpEff : OFF (e)</td></tr></table>";
  var onEff = "<table CELLSPACING=0 CELLPADDING=0 style=\"border-collapse: collapse;border-spacing: 0;\"><tr><td><i class=\"fa fa-circle\" aria-hidden=\"true\"></i>&nbsp;</td><td><table CELLSPACING=0 CELLPADDING=0 style=\"border-collapse: collapse;border-spacing: 0;\"><tr><td><i style=\"background-color:#00BFFF;width:5px;height:6px;display:block;\"></i></td><td><i style=\"background-color:#FFFF00;width:5px;height:6px;display:block;\"></i></td></tr><tr><td><i style=\"background-color:#EE9A00;width:5px;height:6px;display:block;\"></i></td><td><i style=\"background-color:red;width:5px;height:6px;display:block;\"></i></td></tr></table></td><td>snpEff : ON (e)</td></tr></table>";

  if (localStorage.getItem('_Eff') == "off"){
    if(localStorage.getItem("_Ref") == "Reference"){
      //$('#menu .eff').text("snpEff : OFF (e)")
      $('#menu .eff').empty();
      //$('#menu .eff').append(offchk+fourblock+"snpEff : OFF (e)")
      $('#menu .eff').append(offEff);
      $('#menu .eff').css("color", "black");
    }else{
      //$('#menu .eff').text("snpEff OFF (e)")
      //$('#menu .eff').css("color", "gray");
      $('#menu .eff').empty();
      //$('#menu .eff').append(offchk+fourblock+"snpEff : OFF (e)")
      $('#menu .eff').append(offEff)
      $('#menu .eff').css("color", "black");
    }
  } else {
    //$('#menu .eff').text("snpEff ON (e)")
    //$('#menu .eff').css("color", "yellow");
    $('#menu .eff').empty();
    //$('#menu .eff').append(onchk+fourblock+"snpEff : ON (e)")
    $('#menu .eff').append(onEff)
    $('#menu .eff').css("color", "black");
  }
  if (localStorage.getItem('_Snpblock') == "off"){
    //$('#menu .snpblock').text("Snpblock OFF (b)")
    $('#menu .snpblock').empty()
    $('#menu .snpblock').append(offchk+"Snpblock : OFF (b)")
    //$('#menu .snpblock').css("color", "white");
    $('#menu .snpblock').css("color", "black");
  } else {
    //$('#menu .snpblock').text("Snpblock ON (b)")
    $('#menu .snpblock').empty()
    $('#menu .snpblock').append(onchk+"Snpblock : ON (b)")
    //$('#menu .snpblock').css("color", "yellow");
    $('#menu .snpblock').css("color", "black");
  }
  if (localStorage.getItem('_Null') == "true"){
    //$('#menu .null').text("Highlight DEPTH=NA ON (n)")
    $('#menu .null').empty()
    $('#menu .null').append(onchk+"Highlight DEPTH=NA : ON (n)")
    //$('#menu .null').css("color", "yellow");
    $('#menu .null').css("color", "black");
  } else {
    //$('#menu .null').text("Highlight DEPTH=NA OFF (n)")
    $('#menu .null').empty()
    $('#menu .null').append(offchk+"Highlight DEPTH=NA : OFF (n)")
    //$('#menu .null').css("color", "white");
    $('#menu .null').css("color", "black");
  }
  if (localStorage.getItem('_Amino') == "off"){
    //$('#menu .amino').text("Amino acids OFF")
    $('#menu .amino').empty()
    $('#menu .amino').append(offchk+"Amino acids : OFF")
    //$('#menu .amino').css("color", "white");
    $('#menu .amino').css("color", "black");
  } else {
    //$('#menu .amino').text("Amino acids ON")
    $('#menu .amino').empty()
    $('#menu .amino').append(onchk+"Amino acids : ON")
    //$('#menu .amino').css("color", "yellow");
    $('#menu .amino').css("color", "black");
  }
  if (localStorage.getItem('_Indicator') == "off"){
    //$('#menu .indicator').text("Indicator : OFF (d)")
    $('#menu .indicator').empty()
    $('#menu .indicator').append(offchk+"Indicator : OFF (d)")
    //$('#menu .indicator').css("color", "white");
    $('#menu .indicator').css("color", "black");
  } else {
    //$('#menu .indicator').text("Indicator : ON (d)")
    $('#menu .indicator').empty()
    $('#menu .indicator').append(onchk+"Indicator : ON (d)")
    //$('#menu .indicator').css("color", "yellow");
    $('#menu .indicator').css("color", "black");
  }
  if (localStorage.getItem('_GWAS') == "off"){
    //$('#menu .GWAS').text("Show GWAS : OFF (g)")
    $('#menu .GWAS').empty()
    $('#menu .GWAS').append(offchk+"Show GWAS : OFF (g)")
    //$('#menu .GWAS').css("color", "white");
    $('#menu .GWAS').css("color", "black");
    localStorage.getItem('_GWAS') == "on"
  } else {
    //$('#menu .GWAS').text("Show GWAS ON (g)")
    $('#menu .GWAS').empty()
    $('#menu .GWAS').append(onchk+"Show GWAS : ON (g)")
    //$('#menu .GWAS').css("color", "yellow");
    $('#menu .GWAS').css("color", "black");
    localStorage.getItem('_GWAS') == "off"
  }
  if (localStorage.getItem('_Onebase') == "off"){
    if(localStorage.getItem("_Ref") == "Reference"){
      //$('#menu .one').text("Frequency mode : ON (f)")
      $('#menu .one').empty()
      //$('#menu .one').append(onchk+"Frequency mode : ON (f)")
      $('#menu .one').append(offchk+"Absolute Position : OFF (f)")
      //$('#menu .one').css("color", "yellow");
      //$('#menu .one').css("color", "black");
    }else{
      //$('#menu .one').text("Frequency mode : ON (f)")
      $('#menu .one').empty()
      //$('#menu .one').append(onchk+"Frequency mode : ON (f)")
      $('#menu .one').append(offchk+"Absolute Position : OFF (f)")
      //$('#menu .one').css("color", "gray");
    }
  } else {
    if(localStorage.getItem("_Ref") == "Reference"){
      //$('#menu .one').text("Frequency mode : OFF (f)")
      $('#menu .one').empty()
      //$('#menu .one').append(offchk+"Frequency mode : OFF (f)")
      $('#menu .one').append(onchk+"Absolute Position : ON (f)")
      //$('#menu .one').css("color", "white");
      //$('#menu .one').css("color", "black");
    }else{
      //$('#menu .one').text("Frequency mode : OFF (f)")
      $('#menu .one').empty()
      //$('#menu .one').append(offchk+"Frequency mode : OFF (f)")
      $('#menu .one').append(onchk+"Absolute Position : ON (f)")
      //$('#menu .one').css("color", "gray");
    }
  }
  if (localStorage.getItem('_CSTM') == "on"){
    $('#menu .cstm').text("View "+en.cstm_name+" : ON (c)");
    $('#menu .cstm').css("color", "yellow");
    if(Objects.Conditions.length > 1){
      $('#menu .condSelect').css("color", "white");
      $("#menu .condSelect").attr({href: "javascript:_dialog_cond()"});
    }else{
      $('#menu .condSelect').css("color", "gray");
      $("#menu .condSelect").attr({href: "javascript:void(0)"});
    }
    $('#menu .color_cstm').text("Color manager");
    //$('#menu .color_cstm').css("color", "white");
    $('#menu .color_cstm').css("color", "black");
/*
    $('#menu .menuLisDP').html("<font color=\"#797a7a\">&#9619;</font> "+_escapeHTML(en.cstm_name)+" (p) ");
    $('#menu .menuLisDpSnp').html("<font color=\"#797a7a\">&#9619;</font> "+_escapeHTML(en.cstm_name)+" <font color=\"#00ffff\">&#9632;</font> SNP ");
    $('#menu .menuLisDpIndel').html("<font color=\"#797a7a\">&#9619;</font> "+_escapeHTML(en.cstm_name)+" <font color=\"#A52A2A\">&#9632;</font> INDEL ");
    $('#menu .menuLisDpAll').html("<font color=\"#797a7a\">&#9619;</font> "+_escapeHTML(en.cstm_name)+" <font color=\"#00ffff\">&#9632;</font><font color=\"#A52A2A\">&#9632;</font> ALL ");
  }else if(localStorage.getItem('_CSTM') == "disabled"){
    $('#menu .cstm').text("View "+en.cstm_name+" : Disabled");
    $('#menu .cstm').css("color", "gray");
    $('#menu .condSelect').css("color", "gray");
    $("#menu .condSelect").attr({href: "javascript:void(0)"});
    $('#menu .color_cstm').text("Color manager");
    //$('#menu .color_cstm').css("color", "white");
    $('#menu .color_cstm').css("color", "black");
    //$('#menu .menuLisDP').html("<i class=\"fa fa-circle-o\" aria-hidden=\"true\"></i> <font color=\"#797a7a\">&#9619;</font> DEPTH (p)");
    $('#menu .menuLisDpSnp').html("<font color=\"#797a7a\">&#9619;</font> DEPTH <font color=\"#00ffff\">&#9632;</font> SNP ");
    $('#menu .menuLisDpIndel').html("<font color=\"#797a7a\">&#9619;</font> DEPTH <font color=\"#A52A2A\">&#9632;</font> INDEL ");
    $('#menu .menuLisDpAll').html("<font color=\"#797a7a\">&#9619;</font> DEPTH <font color=\"#00ffff\">&#9632;</font><font color=\"#A52A2A\">&#9632;</font> ALL ");
*/
  } else {
/*
    $('#menu .cstm').text("View "+en.cstm_name+" : Off (c)");
    $('#menu .cstm').css("color", "white");
    $('#menu .condSelect').css("color", "gray");
    $("#menu .condSelect").attr({href: "javascript:void(0)"});
    $('#menu .color_cstm').text("Color manager");
    //$('#menu .color_cstm').css("color", "white");
    $('#menu .color_cstm').css("color", "black");
    //$('#menu .menuLisDP').html("<font color=\"#797a7a\">&#9619;</font> DEPTH (p)");
    $('#menu .menuLisDpSnp').html("<font color=\"#797a7a\">&#9619;</font> DEPTH <font color=\"#00ffff\">&#9632;</font> SNP ");
    $('#menu .menuLisDpIndel').html("<font color=\"#797a7a\">&#9619;</font> DEPTH <font color=\"#A52A2A\">&#9632;</font> INDEL ");
    $('#menu .menuLisDpAll').html("<font color=\"#797a7a\">&#9619;</font> DEPTH <font color=\"#00ffff\">&#9632;</font><font color=\"#A52A2A\">&#9632;</font> ALL ");
*/
  }
  if(_trackConflict()){
    $('#menu .viewObjId').text("Feature IDs : ON (t)")
    $('#menu .viewObjId').css("color", "gray");
  }else if (localStorage.getItem('_TrackID') == "on"){
    $('#menu .viewObjId').text("Feature IDs : ON (t)");
    $('#menu .viewObjId').css("color", "yellow");
  } else {
    $('#menu .viewObjId').text("Feature IDs : Off (t)");
    $('#menu .viewObjId').css("color", "white");
  }
}

////////////////////////////////////////////////////////
// Set color
////////////////////////////////////////////////////////
function _setColor(){
  var o=new Object();
  if(localStorage.getItem('_Color')=="0"){
    //o.bgcolor="#595454";
    o.bgcolor="#fff";
    //o.tracktitle="khaki";
    o.tracktitle="#000";
    //o.metercolor="#fff";
    o.metercolor="#000";
    o.meterstring="#fff";
    o.pointercolor="#000";
    //o.pointercolor="#fff";
    //o.pointerstringcolor="#ffffff";
    o.pointerstringcolor="#000";
    o.chrcolor="rgb(60,179,113)";
    o.chrborder="#A4A4A4";
    //o.chrborder="#ffffff";
    //o.chrstringcolor="rgb(255,252,250)";
    o.chrstringcolor="#000";
    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.detailstring="#000";
    o.detailupstring="#000";
    o.detailareaselected="rgba(245,222,179,0.5)";
    o.connectline="yellow";
    //o.title="#f8f8ff";
    o.title="#000";
    //o.reftitle="#ffff00";
    o.reftitle="#000";
    //o.snptitle="khaki";
    o.snptitle="#000";
    //o.fasta="rgb(245,245,220)";
    o.fasta="black";
    //o.amino="rgb(245,245,220)";
    o.amino="black";
    //o.fastabold="rgb(245,245,0)";
    o.fastabold="brown";
    //o.locusid="rgb(255,255,255)";
    o.locusid="black";
    o.highlight="rgba(0,255,0,0.3)";
    o.centeringtrack="rgba(255,255,255,0.3)";
    o.centeringcell="rgba(0,0,0,0.3)";
    o.zoombar="#C0C0C0";
  } else if(localStorage.getItem('_Color')=="1") {
    o.bgcolor="#778899";
    //o.tracktitle="khaki";
    o.tracktitle="#000";
    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="#000";
    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)";
    o.zoombar="#ffffff";
  } else if(localStorage.getItem('_Color')=="2") {
    o.bgcolor="#f8f8ff";
    o.tracktitle="#000000";
    o.metercolor="#000000";
    o.meterstring="#000000";
    o.pointercolor="#000000";
    o.pointerst000lor="#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)";
    o.zoombar="#f8f8ff";
  }
  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;
}

function _reverseNucleotide(rawseq){
  var seqArray = rawseq.toUpperCase().split("");
  var ret = "";
  for(var i=0; i<seqArray.length; i++){
    switch(seqArray[i]){
      case "A":seqArray[i]="T";break;
      case "G":seqArray[i]="C";break;
      case "C":seqArray[i]="G";break;
      case "T":seqArray[i]="A";break;
      case "M":seqArray[i]="K";break;
      case "K":seqArray[i]="M";break;
      case "R":seqArray[i]="Y";break;
      case "Y":seqArray[i]="R";break;
      default:seqArray[i]=seqArray[i]; break;
    }
  }
  ret = seqArray.join("");
  return ret;
}

function _setTitleColor(){
  var ret = "";
  //var color_list = new Array("#f8f8ff","#ffc125","#df7fb5","#8ad1a9","#56b4e9","#eee8aa","#7fffd4","#FFFF00");
  var color_list = new Array("#fa8f8f","#ffc125","#df7fb5","#8ad1a9","#56b4e9","#eee8aa","#7fffd4","#FFFF00");
  var color_list_count = 0;
  var str_array = new Array();
  var key_reg = new Array();
  var dest_subtitle = localStorage.getItem("_NameSub");
  if(dest_subtitle == "subvariety"){dest_subtitle = "grp";}
  var grps = _getList(dest_subtitle);
  for(var i=0; i<grps.length; i++){
    if(!(grps[i] in key_reg)){
      key_reg[grps[i]] = 1;
      if(color_list_count in color_list){
        str_array.push("\""+grps[i]+"\":\""+color_list[color_list_count]+"\"");
        color_list_count++;
      }else{
        var num = "#";
        for(var a=0; a<6; a++){num += (parseInt(5)+Math.floor(Math.random() *10)).toString(16);}
        str_array.push("\""+grps[i]+"\":\""+num+"\"");
      }
    }
  }
  ret = str_array.join(",");
  ret = "{"+ret+"}";
  localStorage.setItem("_GrpCol",ret);
}
