
    var cvs0;
    var ctx0;
    var cvs1;
    var ctx1;
    var cvs2;
    var ctx2;
    var cvs3;
    var ctx3;
    var cvs4;
    var ctx4;
    var cvs5;
    var ctx5;
    var cvs6;
    var ctx6;
	//
	var cvs7;
	var ctx7;
	var cvs8;
	var ctx8;
	//
    var frameCell;
    var frameCellwoI;
    var frameTitle;
    var frameNav;
    var frameChr;
    var frameTrack;
    var frameTrackwR;
    var frameSNP;
    var ONSEARCH=false;
    var Objects=new Object();

    function render() {

      // Checking HTML5
      var canvasSupported = !!window.HTMLCanvasElement;
      if (!canvasSupported) document.location="./bin/error.php?prm_error_mode=HTML5";

      cvs0=document.getElementById("cvs_track_list");
      ctx0=cvs0.getContext("2d");
      cvs1=document.getElementById("cvs_tracks");
      ctx1=cvs1.getContext("2d");
      cvs2=document.getElementById("cvs_nav");
      ctx2=cvs2.getContext("2d");
      cvs3=document.getElementById("cvs_list");
      ctx3=cvs3.getContext("2d");
      cvs4=document.getElementById("cvs_view");
      ctx4=cvs4.getContext("2d");
      cvs5=document.getElementById("cvs_snpt");
      ctx5=cvs5.getContext("2d");
      cvs6=document.getElementById("cvs_snpg");
      ctx6=cvs6.getContext("2d");
	  //
	  cvs7=document.getElementById("cvs_ind");
      ctx7=cvs7.getContext("2d");
	  cvs8=document.getElementById("cvs_ind_blk");
      ctx8=cvs8.getContext("2d");
	 //
	  
      // Clear localStorage when url is sub directory on same domain
      if ( localStorage.getItem('_Url') != location.href ) localStorage.clear();

      _showLoading();
      _initCache();
      _execCanvas();
      //_dialog_go();

      // When first time
      if ( localStorage.getItem('_Count') == "1" ) {
      //  _dialog_manual();
      //  _dialog_citation();
      //  _dialog_about();
        localStorage.setItem('_Count',"2");
      }

      $("#tasuke").css("background-color",_setColor().bgcolor);
    }

    ///////////////////////////////////////////////////////////
    // Initialize localStorage
    ///////////////////////////////////////////////////////////
    function _initCache() {
      if(_isEmpty(localStorage.getItem("_Url"))){
        localStorage.setItem("_Url",location.href);
      }
      if(_isEmpty(localStorage.getItem("_Ref"))){
        localStorage.setItem("_Ref","Reference");
      }
      if(_isEmpty(localStorage.getItem("_Zoom"))){
        localStorage.setItem("_Zoom","1k");
      }
      if(_isEmpty(localStorage.getItem("_Pos"))){
        localStorage.setItem("_Pos","1");
      }
      if(_isEmpty(localStorage.getItem("_Mode"))){
        localStorage.setItem("_Mode","s");
      }
      if(_isEmpty(localStorage.getItem("_Eff"))){
        localStorage.setItem("_Eff","off");
      }
      if(_isEmpty(localStorage.getItem("_Snpblock"))){
        localStorage.setItem("_Snpblock","off");
      }
      if(_isEmpty(localStorage.getItem("_QUAL"))){
        localStorage.setItem("_QUAL","20");
      }
      if(_isEmpty(localStorage.getItem("_DP"))){
        localStorage.setItem("_DP","2");
      }
      if(_isEmpty(localStorage.getItem("_DP4"))){
        localStorage.setItem("_DP4","2");
      }
      if(_isEmpty(localStorage.getItem("_Name"))){
        localStorage.setItem("_Name","id");
      }
      if(_isEmpty(localStorage.getItem("_NameSub"))){
        localStorage.setItem("_NameSub","subvariety");
      }
      if(_isEmpty(localStorage.getItem("_TitleW"))){
        localStorage.setItem("_TitleW","120");
      }
      if(_isEmpty(localStorage.getItem("_BlockW"))){
        localStorage.setItem("_BlockW","10");
      }
      if(_isEmpty(localStorage.getItem("_BlockH"))){
        localStorage.setItem("_BlockH","12");
      }
      if(_isEmpty(localStorage.getItem("_Block"))){
        localStorage.setItem("_Block","90");
      }
      if(_isEmpty(localStorage.getItem("_Indicator"))){
        localStorage.setItem("_Indicator","on");
      }
      if(_isEmpty(localStorage.getItem("_Amino"))){
        localStorage.setItem("_Amino","on");
      }
      if(_isEmpty(localStorage.getItem("_DirRef"))){
        localStorage.setItem("_DirRef","+");
      }
      if(_isEmpty(localStorage.getItem("_Null"))){
        localStorage.setItem("_Null","true");
      }
      if(_isEmpty(localStorage.getItem("_Keyword"))){
        localStorage.setItem("_Keyword","");
      }
      if(_isEmpty(localStorage.getItem("_History"))){
        localStorage.setItem("_History","[]");
      }
      if(_isEmpty(localStorage.getItem("_Color"))){
        localStorage.setItem("_Color","0");
      }
      if(_isEmpty(localStorage.getItem("_Count"))){
        localStorage.setItem("_Count","1");
      }
      if(_isEmpty(localStorage.getItem("_ListsDeleted"))){
        localStorage.setItem("_ListsDeleted","[]");
      }
      if(_isEmpty(localStorage.getItem("_Env"))){
        _execEnv();
      }
      if(_isEmpty(localStorage.getItem("_Lists"))){
        _execLists();
      }
      if(_isEmpty(localStorage.getItem("_Chr"))){
        _execChr();
        localStorage.setItem("_Chr",_getChr().id);
      }
   }

    ///////////////////////////////////////////// Cheking empty
    function _isEmpty(value){
      return (value == null || value.length == 0);
    }

    ///////////////////////////////////////////////////////////
    // Ajax: get environment settings
    ///////////////////////////////////////////////////////////
    function _execEnv() {
      $.ajax({
        url: "./bin/env.php",
        cache: false,
        async: false,
        success: function(data) {
          localStorage.setItem("_Env", data);
        }
      });
    }

    ///////////////////////////////////////////////////////////
    // Ajax: get chromosome
    ///////////////////////////////////////////////////////////
    function _execChr() {
      $.ajax({
        url: "./bin/chr.php",
        cache: false,
        async: false,
        success: function(data) {
          localStorage.setItem("_Chrs", data);
        }
      });
    }

    ///////////////////////////////////////////////////////////
    // Ajax: get snpblock
    ///////////////////////////////////////////////////////////
    function _execSnpblock(keyword) {
      $.ajax({
        url: "./bin/snpblock.php",
        cache: false,
        async: false,
        data: {
          "prm_accession": keyword
        },
        success: function(data) {
          localStorage.setItem("_Snpblock", data.replace(/^[ 　\t\r\n]+|[ 　\t\r\n]+$/g, ""));
        }
      });
    }

    ///////////////////////////////////////////////////////////
    // Ajax: get list of accessions & tracks
    ///////////////////////////////////////////////////////////
    function _execLists() {
      $.ajax({
        url: "./bin/list.php",
        cache: false,
        async: false,
        success: function(data) {
          var a=data.split("|");
          localStorage.setItem("_Lists", a[0]);
          localStorage.setItem("_Lists-origin", a[0]);
          localStorage.setItem("_Tracks", a[1]);
        }
      });
    }

    ///////////////////////////////////////////////////////////
    // Ajax: Load information of cell
    ///////////////////////////////////////////////////////////
    function _execInfoCell(x,y) {

      var o=new Object(JSON.parse(localStorage.getItem('_Lists')));
      var id=o[parseInt(y/Objects.BlockH)].db;
      var b=Math.abs(parseInt((x/Objects.BlockW)));
      var p=Objects.Pos+b;

      if ( _cvtZoom(Objects.Zoom) > 1000 ) { // When over 1k
        _drawInfoCell(id,b,y,"[]");
      } else {
        $.ajax({
          url: "./bin/info.php",
          type: "GET",
          cache: false,
          data: {
            "prm_Zoom":Objects.Zoom,
            "prm_Chr":Objects.Chr,
            "prm_Mode":Objects.Mode,
            "prm_Pos":p,
            "prm_QUAL":localStorage.getItem("_QUAL"),
            "prm_DP":localStorage.getItem("_DP"),
            "prm_DP4":localStorage.getItem("_DP4"),
            "prm_Acc":id
          },
          success: function(datas) {
            _drawInfoCell(id,b,y,datas);
          }
        });
      }

    }

    ///////////////////////////////////////////////////////////
    // Ajax: Search ID
    //       1K zoom will setted
    ///////////////////////////////////////////////////////////
    function _execSearch(keyword) {
      $.ajax({
        url: "./bin/search.php",
        type: "GET",
        cache: false,
        data: {
          "prm_chr":localStorage.getItem("_Chr"),
          "prm_zoom":"1k",
          "prm_keyword":keyword
        },
        success: function(datas) {
           var a=datas.split("|");
           var p=_checkPos(a[1]-parseInt(Objects.Block/2), "1k");
		   var c =a[2];
		   c = c.replace( /\r\n/g , "\n" );	
		   c = c.replace( /^(\n+)|(\n+)$/g , "" );
           if(a[1]>0){
             _setHistory("search,"+keyword);
             localStorage.setItem("_Keyword", keyword);
             localStorage.setItem("_Zoom", "1k");
             localStorage.setItem("_Pos", p);
			 localStorage.setItem("_Chr", c);
             _changeChromosomeSearch(keyword);
			 //_showLoading();
             //_messageOnScreen("Searching "+keyword);
             //_execCanvas();
           } else {
             localStorage.setItem("_Chr", Objects.Chr);
             _messageOnScreen("Not found ("+keyword+")");
           }
        }
      });
    }

    ///////////////////////////////////////////////////////////
    // Ajax: Get track&depth&variant and drawing
    ///////////////////////////////////////////////////////////
    function _execCanvas(p,b,s) {

      // Track
      $.ajax({
          url: "./bin/track.php",
          type: "GET",
          cache: false,
          async: false,
          data: {
              "prm_Zoom":localStorage.getItem("_Zoom"),
              "prm_Chr":localStorage.getItem("_Chr"),
              "prm_Pos":localStorage.getItem("_Pos"),
              "prm_Block":localStorage.getItem("_Block"),
              "prm_Tracks":new Object(_getTrack("list"))
          },
          success: function(datas) {
            var n=datas.split("|");
            for (i=0; i<_getTrack("list").length;i++){
              localStorage.setItem("_Track_"+i, n[i]);
            }
            if ( localStorage.getItem("_Zoom") == '1b' ) {
              localStorage.setItem("_FASTA", n[n.length-1]);
            }
          }
      });

      // Depth&Variants
      if (_isEmpty(p)) {
          p=localStorage.getItem("_Pos"); 
          b=localStorage.getItem("_Block"); 
      }
      $.ajax({
          url: "./bin/data.php",
          type: "POST",
          cache: false,
          data: {
            "prm_Ref":localStorage.getItem("_Ref"),
            "prm_Zoom":localStorage.getItem("_Zoom"),
            "prm_Chr":localStorage.getItem("_Chr"),
            "prm_Pos":p,
            "prm_QUAL":localStorage.getItem("_QUAL"),
            "prm_DP":localStorage.getItem("_DP"),
            "prm_DP4":localStorage.getItem("_DP4"),
            "prm_Block":b,
            "prm_Lists":new Object(_getList("db"))
          },
          success: function(datas) {
            var d=datas.replace(/^\s+|\s+$/g, "");
            if (_isEmpty(s)) {
              _setData(d);
            } else {
              _setData_p(d,s,b);
            }
            _drawCanvas();
          }
      });
    }

    ///////////////////////////////////////////////////////////
    // Double click for zoom in
    ///////////////////////////////////////////////////////////
    function _execDblClick(x) {
      if(_cvtZoom(Objects.Zoom)>1){
        var t=parseInt(x/Objects.BlockW);
        if (t==0) t=1;
        var b=(parseInt(Objects.Pos)+t)*_cvtZoom(Objects.Zoom);
        var p=_checkPos(parseInt(b/_cvtZoom(_cvtZoomDown(Objects.Zoom)))-(Objects.Block/2)+1,_cvtZoomDown(Objects.Zoom));
        localStorage.setItem("_Pos", new String(p));
        localStorage.setItem("_Zoom", _cvtZoomDown(Objects.Zoom));
        _showLoading();
        _drawMessageOnScreen("zoomInPos",x);
        _messageOnScreen("Zoom in "+Objects.Zoom+" to "+_cvtZoomDown(Objects.Zoom)+" per block");
        _execCanvas();
      } else {
        _messageOnScreen("Minimum zoom scale");
      }
      // Unlink cell information
      $("#dialog_infocell").dialog("close");
    }

    ///////////////////////////////////////////// Get chromosome
    function _getChr(id) {
      var a=new Object();
      var o=new Object(JSON.parse(localStorage.getItem("_Chrs")));
      if (_isEmpty(id)) {
        a.id=o[0].id;
        a.size=o[0].size;
        a.centromere=o[0].centromere;
      } else if (id == "max") {
        a.size=0;
        for(i=0;i<o.length;i++){
          if(o[i].size>a.size){
            a.size=o[i].size;
          }
        }
      } else {
        for(i=0;i<o.length;i++){
          if(o[i].id==id){
            a.id=o[i].id;
            a.size=o[i].size;
            a.centromere=o[i].centromere;
            break;
          }
        }
      }
      return a;
    }
    ///////////////////////////////////////////// Get list of accession by mode
    function _getList(str) {
      var a=new Array();
      var o=new Object(JSON.parse(localStorage.getItem("_Lists")));
      for(var i=0;i<o.length;i++){
        if(str=="id"){
          a[i]=o[i].id;
        }else if(str=="db"){
          a[i]=o[i].db;
        }else if(str=="name"){
          a[i]=o[i].name;
        }else if(str=="grp"){
          a[i]=o[i].grp;
        }else if(str=="variety"){
          a[i]=o[i].variety;
        }else if(str=="origin"){
          a[i]=o[i].origin;
        }else if(str=="type"){
          a[i]=o[i].type;
        }
      }
      return a;
    }

    ///////////////////////////////////////////// Get list of tracks
    function _getTrack(str) {
      var s=0;
      var a=new Array();
      var o=new Object(JSON.parse(localStorage.getItem("_Tracks")));
      if (str=="list") {
        for(var i=0;i<o.length;i++){
          a[i]=o[i].track;
        }
        return a;
      }
      if ( str=="view" ) {
        for(var i=0;i<o.length;i++){
          if (o[i].p==1){
            a.push(o[i].track);
          }
        }
        return a;
      }
    }

    ///////////////////////////// Set accession data to localStorage
    function _setData(str) {
      var n=str.split(";");
      for (var i=0; i<n.length; i++) {
         var a=n[i].split("|");
         localStorage.setItem(a[0], a[1]); // Rewrite if exists
      }
    }

    ///////////////////////////// Hide loading 
    function _hideLoading() {
      document.body.style.cursor = "default";
      _initEvents();
    }

    ///////////////////////////// Show loading 
    function _showLoading() {
      document.body.style.cursor = "wait";
      _removeEvents();
    }
 
    ///////////////////////////// Moving head or tail
    function _moveEdge(pos) {
      if ( pos == "s" ) {
        localStorage.setItem("_Pos", "1");
        msg="head";
      } else { 
        localStorage.setItem("_Pos",new String(_last(Objects.Zoom)-Objects.Block+1));
        msg="last";
      } 
      _showLoading();
      _messageOnScreen("Moving "+msg+" of "+Objects.Chr);
      _execCanvas();
    }

    ///////////////////////////// Moving page
    function _movePage(str) {
      var p=Objects.Pos;
      var b=Objects.Block;
      var msg;
      if (str == "+") {
        var c=0;
        if ((p+b+b-1)>_last(Objects.Zoom)) {
          localStorage.setItem("_Pos",new String(_last(Objects.Zoom)-b+1));
        } else {
          localStorage.setItem("_Pos", new String(p+b));
        }
        msg="right";
      } else {
        if (p-b<1) {
          localStorage.setItem("_Pos", new String(1));
        } else {
          localStorage.setItem("_Pos", new String(p-b));
        }
        msg="left";
      }

      _setHistory("position,"+Objects.Chr+","+p+","+Objects.Zoom);
      _drawMessageOnScreen(msg);
      _showLoading();
      _execCanvas();
    }

    ///////////////////////////// Initialize canvas with size
    function _initCanvas() {

      cvs0.width=Objects.titleWidth;
      cvs0.height=Objects.TrackH;
      ctx0.fillRect(0,0,cvs0.width, cvs0.height);
      ctx0.setTransform(1, 0, 0, 1, 0, 0);
      ctx1.font="11px 'Courier'";

      cvs1.width=Objects.canvasWidth;
      cvs1.height=Objects.TrackH;
      ctx1.fillRect(0,0,cvs1.width, cvs1.height);
      ctx1.setTransform(1, 0, 0, 1, 0, 0);
      ctx1.font="11px 'Courier'";

      cvs2.width=Objects.canvasWidth;
      cvs2.height=Objects.NavH;
      ctx2.fillRect(0,0,cvs2.width, cvs2.height);
      ctx2.setTransform(1, 0, 0, 1, 0, 0);
      ctx2.font="11px 'Century'";

      cvs3.width=Objects.titleWidth;
      cvs3.height=Objects.canvasHeight;
      ctx3.fillRect(0,0,cvs3.width,cvs3.height);
      ctx3.setTransform(1, 0, 0, 1, 0, 0);
      ctx3.font="11px 'Courier'";

      cvs4.width=Objects.canvasWidth;
      cvs4.height=Objects.canvasHeight;
      ctx4.fillStyle="#ffffff";
      ctx4.fillRect(0,0,cvs4.width,cvs4.height);
      ctx4.setTransform(1, 0, 0, 1, 0, 0);
      ctx4.font="10px 'Courier'";

      cvs5.width=Objects.titleWidth;
      cvs5.height=20;
      ctx5.fillStyle=_setColor().bgcolor;
      ctx5.fillRect(0,0,cvs5.width,cvs5.height);
      ctx5.setTransform(1, 0, 0, 1, 0, 0);
      ctx5.font="12px 'Courier'";

      cvs6.width=Objects.canvasWidth;
      cvs6.height=20;
      ctx6.fillStyle=_setColor().bgcolor;
      ctx6.fillRect(0,0,cvs6.width,cvs6.height);
      ctx6.setTransform(1, 0, 0, 1, 0, 0);
      ctx6.font="11px 'Courier'";
//
	  //cvs7.width=Objects.canvasWidth;
	  cvs7.width=150;
      cvs7.height=165;
      //ctx7.fillStyle="#ffffff";
	  ctx7.fillStyle='rgba(0,0,0,0)';
      ctx7.fillRect(0,0,cvs7.width,cvs7.height);
	  ctx7.fillStyle='rgba(255,255,255,0.6)';
      ctx7.fillRect(4,cvs7.height-58,cvs7.width-8,23);
	  ctx7.strokeStyle="#acacb3";
      ctx7.lineWidth=1;
      ctx7.strokeRect(4,cvs7.height-58,cvs7.width-8,23);
      ctx7.setTransform(1, 0, 0, 1, 0, 0);
      ctx7.font="10px 'Courier'";
	  
	  cvs8.width=Objects.canvasWidth;
	  //cvs8.width=170;
      //cvs8.height=Objects.canvasHeight;
	  cvs8.height=1;
      //ctx8.fillStyle="#ffffff";
	  ctx8.fillStyle='rgba(0,0,0,0)';
      ctx8.fillRect(0,0,cvs8.width,1);
      ctx8.setTransform(1, 0, 0, 1, 0, 0);
      ctx8.font="10px 'Courier'";
	  
//	  
	  
      // Set to main canvas height
      var offset=20;
      var divHeight=window.innerHeight-$('#content').offset().top-$('#content2').height()-$('#bottom').height()-offset;
      if ( Objects.canvasHeight < divHeight ) {
        divHeight=Objects.canvasHeight+10;
      }
      $('#content').css("height", divHeight);

      // Changing height during resize
      $(window).bind("resize", resizeWindow);
      function resizeWindow( e ) {
        divHeight=$(window).height()-$('#content').offset().top-$('#content2').height()-$('#bottom').height()-offset;
        if ( Objects.canvasHeight < divHeight ) {
          divHeight=Objects.canvasHeight+10;
        } $('#content').css("height", divHeight);
      }

   }

    ///////////////////////////// Print area
    function _printArea(area) {
      t=area*_cvtZoom(Objects.Zoom);
      if (t<1000) {
        return String(t)+" bp";
      } else if (t>=1000 && t<1000000) {
        return String(t/1000)+" kbp";
      } else if (t>=1000000) {
        return String(t/1000000)+" mbp";
      }
    }

    ///////////////////////////// Print pos by zoom
    function _printPos(pos) {
      z=_cvtZoom(Objects.Zoom);
      t=parseInt(pos*z);
      if (z<1000) {
        return String(t)+" b";
      } else if (z>=1000 && z<1000000) {
        return String((t/1000))+" k";
      } else if (z>=1000000) {
        return String((t/1000000))+" m";
      }
    }

    ///////////////////////////// Drawing Snpblock: Estimation of SNP "blocks"
    function _getSnpblock(str) {
      var o=new Object(JSON.parse(localStorage.getItem('_Snpblock')));
      for (var x=0; x<o.length; x++) {
        var pattern=new RegExp(str);
        if(o[x].id.match(pattern)) {
          return o[x].rate;
        }
      }
    }
 
    /////////////////////////////////////////////// Stop drag when mouse up
    function _getData_p(d) {
        var s;
        var t=0;
        var p=0
        var b=Math.abs(parseInt(d/Objects.BlockW));
        var setP;
        if (d>0){
            s="right";
            t=Objects.BlockW-d%Objects.BlockW;
            n=d+t;
            b=parseInt(n/Objects.BlockW);
            if(Objects.Pos-b<1){
              //n=parseInt(localStorage.getItem("_Pos"))*parseInt(Objects.BlockW)-6;
              n=0;
              b=Objects.Pos-1;
              p=1;
            } else {
              p=Objects.Pos-b;
            }
            setP=p;
        } else {
            s="left";
            t=Objects.BlockW+d%Objects.BlockW;
            n=d-t;
            b=Math.abs(n/Objects.BlockW);
            max=_last(Objects.Zoom);
            p=Objects.Pos+Objects.Block;
            if(p+b-1>max){
              p=max-(max-p);
              n=-((max-p+1)*Objects.BlockW);
              b=(max-p+1);
            }
            setP=p-Objects.Block+b;
        }

        // Set canvas by block width
        ctx2.clearRect(0, 30, cvs2.width, 30);

        ctx4.clearRect(0, 0, Objects.canvasWidth, Objects.canvasHeight);
        ctx4.putImageData(frameCellwoI, n, 0);
        ctx1.clearRect(0, 0, cvs1.width, cvs1.height);
        ctx1.putImageData(frameTrack, n, 0);
        ctx0.clearRect(0, 0, cvs0.width, cvs0.height);
        ctx0.putImageData(frameTrackTitle, 0, 0);
        ctx6.putImageData(frameSNP, n, 0);
        _drawMeter(setP,null);
        _drawRules();

        if (Objects.Indicator=="on") {
          _drawIndicator();
          _drawDirection();
        }

        // If occured gap
        if (b>0) {
          _showLoading();
          localStorage.setItem("_Pos", setP);  // need load, first position
          _setHistory("position,"+Objects.Chr+","+setP+","+Objects.Zoom);
          _execCanvas(p,b,s);   // Load part of datas
        }
    }

    ///////////////////////////// Stringfy of array data
    function _mergeObject(o){
      var str;
      str="{\"dp\":\"";
      if(o.dp == null){
        str=str+"";
      } else {
        str=str+o.dp;
      }
      str=str+"\"";
      if(o.s != undefined){
        str=str+",\"s\":\""+o.s+"\"";
      }
      if(o.i != undefined){
        str=str+",\"i\":\""+o.i+"\"";
      }
      if(o.d != undefined){
        str=str+",\"d\":\""+o.d+"\"";
      }
      str=str+"}";
      return str; 
    }

    ///////////////////////////// Set part of data
    function _setData_p(datas,s,b) {
      var n=datas.split(";");
      for (var i=0; i<n.length; i++) {
        var a=n[i].split("|");
        var o=JSON.parse(localStorage.getItem(a[0]));
        var j=new Object(JSON.parse(a[1]));
        var t="[";
        if(s=="left"){
        for(var k=0; k<o.length-b; k++) {
        t=t+_mergeObject(o[k+b])+",";
        }
        for(var k=0; k<b; k++) {
        t=t+_mergeObject(j[k]);
        if(k<b-1){t=t+",";}
        }
        } else {
        for(var k=0; k<b; k++) {
        t=t+_mergeObject(j[k])+",";
        }
        for(var k=0; k<o.length-b; k++) {
        t=t+_mergeObject(o[k]);
        if(k<o.length-b-1){t=t+",";}
        }
        }
        var t=t+"]";
        localStorage.setItem(a[0], t); // Rewrite if exists
      }
    }

    Array.prototype.remove = function(from, to) {
      var rest = this.slice((to || from) + 1 || this.length);
      this.length = from < 0 ? this.length + from : from;
      return this.push.apply(this, rest);
    };

    /////////////////////////////////////////////// Change display mode
    function _changeMode(str){
      if ( Objects.Snpblock == "off" ) {
        localStorage.setItem("_Mode", str);
        _drawCanvas();
      } else {
        _messageOnScreen("Mode is fixed reason why 'Snpblock' is on.");
      }
    }
	
	/////////////////////////////////////////////// Change display mode(switch)
    function _changeModeSW(str){
      if ( Objects.Snpblock == "off" ) {
		if (localStorage.getItem("_Mode") == str){
			localStorage.setItem("_Mode", "dp");
		} else {
			localStorage.setItem("_Mode", str);
		}
		_drawCanvas();
      } else {
        _messageOnScreen("Mode is fixed reason why 'Snpblock' is on.");
      }
    }
	

    /////////////////////////////////////////////// Change direction of reference
    function _changeDirRef(){
      if ( localStorage.getItem("_DirRef") == "+" ) {
        localStorage.setItem("_DirRef", "-");
      } else {
        localStorage.setItem("_DirRef", "+");
      }
    }

    /////////////////////////////////////////////// Change select chromosome for search ID
    function _changeChromosomeSearch(kwd){
      var chr=$('#prm_chr').val();
      $('#prm_chr').blur(); // focus out from select box
      _showLoading();
      _messageOnScreen("Searching "+kwd);
      _execCanvas();
	  
      // Set chromosome option on dialog 'Search'
      $('#prm_position_chr').val(chr);
    }
	
	/////////////////////////////////////////////// Change select chromosome 
	function _changeChromosome(){
      var chr=$('#prm_chr').val();
      localStorage.setItem('_Chr', chr);
      localStorage.setItem('_Pos', '1');
      $('#prm_chr').blur(); // focus out from select box

      _showLoading();
      _messageOnScreen("Loading "+chr);
      _execCanvas();

      // Set chromosome option on dialog 'Search'
      $('#prm_position_chr').val(chr);
    }

    /////////////////////////////////////////////// Change zoom
    function _changeZoomS(zoom){

      var msg;
      var flag;
      var cZoom=_cvtZoom(Objects.Zoom);
      var tZoom=_cvtZoom(zoom);

      // Zoom in when over 1b
      if(zoom=='+') { 
        if ( _cvtZoom(Objects.Zoom) > 1 ){
          var b=(parseInt(Objects.Pos)+(Objects.Block/2))*_cvtZoom(Objects.Zoom);
          var p=_checkPos(parseInt(b/_cvtZoom(_cvtZoomDown(Objects.Zoom)))-(Objects.Block/2)+1,_cvtZoomDown(Objects.Zoom));
          localStorage.setItem("_Pos", new String(p));
          localStorage.setItem("_Zoom", _cvtZoomDown(Objects.Zoom));
          msg="zoomIn";
          flag=true;
        } else {
          msg="Minimum zoom scale";
          flag=false;
        }
      }

      // Zoom out when under 100k
      if(zoom=='-') {
        if (_cvtZoom(Objects.Zoom) < 100000){
          var b=(parseInt(Objects.Pos)+(Objects.Block/2))*_cvtZoom(Objects.Zoom);
          var p=_checkPos(parseInt(b/_cvtZoom(_cvtZoomUp(Objects.Zoom)))-(Objects.Block/2)+1,_cvtZoomUp(Objects.Zoom));
          localStorage.setItem("_Pos", new String(p));
          localStorage.setItem("_Zoom", _cvtZoomUp(Objects.Zoom));
          flag=true;
          msg="zoomOut";
        } else {
          msg="Maximum zoom scale";
          flag=false;
        }
      }

      if(msg=="zoomIn"){
        _drawMessageOnScreen(msg);
        _messageOnScreen("Zoom in "+Objects.Zoom+" to "+_cvtZoomDown(Objects.Zoom)+" per block");
      } else if ( msg=="zoomOut") {
        _messageOnScreen("Zoom out "+Objects.Zoom+" to "+_cvtZoomUp(Objects.Zoom)+" per block");
      } else {
        _messageOnScreen(msg);
      }

      if(flag){
        _showLoading();
        _execCanvas();
      }

    }

    /////////////////////////////////////////////// Change zoom
    function _changeZoom(zoom){

      var msg;
      var flag;
      var cZoom=_cvtZoom(Objects.Zoom);
      var tZoom=_cvtZoom(zoom);

      if(zoom != Objects.Zoom){
        var b=(parseInt(Objects.Pos)+(Objects.Block/2))*_cvtZoom(Objects.Zoom);
        var p=_checkPos(parseInt(b/_cvtZoom(zoom))-(Objects.Block/2)+1,zoom);
        localStorage.setItem('_Pos', new String(p));
        localStorage.setItem('_Zoom', zoom);
        if ( cZoom > tZoom ) { msg="zoomInC" }
        else { msg="zoomOut"; }
        flag=true;
      } else {
        msg="Currently zoom scale is "+Objects.Zoom+" per block";
        flag=false;
      }

      if(msg=="zoomInC"){
        _drawMessageOnScreen(msg,tZoom,cZoom);
        _messageOnScreen("Zoom in "+Objects.Zoom+" to "+zoom+" per block");
      } else if ( msg=="zoomOut" ) {
        _messageOnScreen("Zoom out "+Objects.Zoom+" to "+zoom+" per block");
      } else {
       _messageOnScreen(msg);
      }

      if(flag){
        _showLoading();
        _execCanvas();
      }

      // Set zoom option on dialog 'Search'
      $('#prm_position_zoom').val(zoom);

    }

    /////////////////////////////////////////////// Details accession
    function _changeRef(){
	  var i=document.getElementById("prm_chref").value;
      var o=new Object(JSON.parse(localStorage.getItem('_Lists')));
      if ( localStorage.getItem("_Ref") != o[i].db ) {
        _changeEff('off')
        localStorage.setItem("_Ref", o[i].db);
        msg="Changing reference "+o[i].id;
      } else {
        localStorage.setItem("_Ref", "Reference");
        msg="Reset reference";
      }
      // Estimation of SNP 'blocks' in changing reference
      if (localStorage.getItem("_Snpblock") != "off") {
        _execSnpblock(localStorage.getItem("_Ref"));
      }

      _changeNull();
	  _changeNull();//require
      _showLoading();
      _messageOnScreen(msg);
      _execCanvas();
	  _dlg_chref_cancel()
    }

    /////////////////////////////////////////////// Message on screen
    function _messageOnScreen(str){
      ctx4.textAlign="left";
      ctx4.fillStyle="#ffffff";
      ctx4.fillRect(200,150,350,60);
      ctx4.strokeStyle="#b0c4de";
      ctx4.lineWidth=7;
      ctx4.strokeRect(200,150,350,60);

      ctx4.font="Bold 13px 'Century'";
      ctx4.fillStyle="#000000";
      ctx4.fillText(str,220,185);
    }

    /////////////////////////////////////////////// Change variant Effect
    function _changeEff(eff){
      if ( localStorage.getItem("_Ref") == "Reference") {
          if (localStorage.getItem('_Eff')=="on" || eff == "off") {
            localStorage.setItem('_Eff', "off");
          } else {
            localStorage.setItem('_Eff', "on");
          }
          if ( localStorage.getItem("_Snpblock") != "off" && localStorage.getItem('_Eff') == "on" ) {
            localStorage.setItem('_Snpblock', "off");
          }
      }
      _drawCanvas();
    }

    /////////////////////////////////////////////// Change snpblock
    function _changeSnpblock(){
      if ( localStorage.getItem("_Snpblock")=="off" ) {
        localStorage.setItem('_Eff', "off");
        //localStorage.setItem('_Mode', "dp+s");
		localStorage.setItem('_Mode', localStorage.getItem("_Mode"));
        _execSnpblock(localStorage.getItem("_Ref"));
      } else {
        localStorage.setItem("_Snpblock", "off");
      }
      _drawCanvas();
      if ( localStorage.getItem('_Snpblock') == "[]" ) {
        _messageOnScreen("Snpblock data is not found");
      }
    }

    /////////////////////////////////////////////// Change NULL on/off
    function _changeNull(){
      if (localStorage.getItem("_Null")=="false"){
        localStorage.setItem("_Null", "true");
      } else {
        localStorage.setItem("_Null", "false");
      }
      _drawCanvas();
    }


