//#############################################################################
// 
// All rights on this script are reserved to
// Nihon Enganshigen Kenkyusyo (The Japan Coastal Resouce Research Institude).
// URL: http://umizukan.com
// Coresponder: iwase@umizukan.com
// Version: 1.0
// 
//#############################################################################

// split get value ############################################################
var aurlval_temp=Array();
var aurlval=Array();
var aurlval_org=location.href.split('?');
var i=0;
var listtype="";
var def_spt=0;
var def_id="";

if(aurlval_org.length>1){
  aurlval_temp=aurlval_org[1].split('&');
  for(i=0;i<aurlval_temp.length;i++){
    aurlval[i]=aurlval_temp[i].split("=");
    // find listtype
    if(aurlval[i][0]=="listtype") listtype=aurlval[i][1];
    // find def_spt
    if(aurlval[i][0]=="spt") def_spt=aurlval[i][1];
    // find id
    if(aurlval[i][0]=="id") def_id=aurlval[i][1];
  }
}


// create object ##############################################################
function createXMLHTTP(){
  if(window.XMLHttpRequest){
    return new XMLHttpRequest();
  }
  else if(window.ActiveXObject){
    try{
      return new ActiveXObject("MSXML2.XMLHTTP");
    }
    catch(e){
      try{
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e){
        return null;
      }
    }
  }
  return null;
}
var reqspts=createXMLHTTP();
var reqindv=createXMLHTTP();


// Page contents ##############################################################
function rewrite_description(idesc,ipic){
  var itxt="";
  if(idesc!=''){
    itxt+='<div style="width:90%;border-color:#dd3;border-style:solid;">';
    itxt+='<div style="background-color:#ddd;">infomation</div>\n';
    itxt+='<div style="padding:10px;line-height:150%;text-align:left;">';
    itxt+=idesc+'</div>\n';
    itxt+='</div>\n';
    document.getElementById("description").innerHTML=itxt;
  }
  else{
    document.getElementById("description").innerHTML='';
  }
  if(ipic!=''){
    document.getElementById("picture").innerHTML='<img src="'+ipic+'" />';
  }
  else{
    document.getElementById("picture").innerHTML='';
  }
}
function rewrite_indv(idnow){
  //document.getElementById("indv").innerHTML='function started';
  var xmlindv    = reqindv.responseXML;
  var node_idv   = xmlindv.getElementsByTagName('indivisual');
  var itxt="";
  var adesc=Array(Array("0","0"));
  var ipos=0;
  var i=0,h=0;
  for(i=0;i<node_idv.length;i++){
    iid=node_idv.item(i).getElementsByTagName("id").item(0).firstChild.nodeValue;
    if(iid==idnow){
      //adesc=Array(Array("0","0"));
      adesc[0][0]=0;
      
      iid=
        node_idv.item(i).getElementsByTagName("id").item(0).firstChild.nodeValue;
      ispt=
        node_idv.item(i).getElementsByTagName("spt").item(0).firstChild.nodeValue;
      iorder=
        node_idv.item(i).getElementsByTagName("tax_order").item(0).firstChild.nodeValue;
      ifamily=
        node_idv.item(i).getElementsByTagName("tax_family").item(0).firstChild.nodeValue;
      igenus=
        node_idv.item(i).getElementsByTagName("tax_genus").item(0).firstChild.nodeValue;
      ilabel=
        node_idv.item(i).getElementsByTagName("label").item(0).firstChild.nodeValue;
      inote=
        node_idv.item(i).getElementsByTagName("note").item(0).firstChild.nodeValue;

      node_desc=node_idv.item(i).getElementsByTagName("description");
      node_desc_label   =node_desc.item(0).getElementsByTagName("label_desc");
      node_desc_contents=node_desc.item(0).getElementsByTagName("contents");
      node_desc_fn      =node_desc.item(0).getElementsByTagName("fn_picture");
      for(h=0;h<node_desc_label.length;h++){
        //decodeURIComponent()
        adesc[++adesc[0][0]]=Array(
        node_desc_label.item(h).firstChild.nodeValue,
        node_desc_contents.item(h).firstChild.nodeValue,
        node_desc_fn.item(h).firstChild.nodeValue      );
      }

      // Write Indivisual data

      itxt+='<center>\n';
      itxt+='<div style="margin-left:auto;margin-right:auto;">\n';
      itxt+='<table class="idvinfotable">\n';
      itxt+='<tr><td>ID</td><td>'+iid+'</td></tr>\n';
      itxt+='<tr><td>目(Order)</td><td>'+iorder+'</td></tr>\n';
      itxt+='<tr><td>科(Family)</td><td>'+ifamily+'</td></tr>\n';
      itxt+='<tr><td>属(Genus)</td><td>'+igenus+'</td></tr>\n';
      itxt+='<tr><td>種名</td><td>'+ilabel+'</td></tr>\n';
      itxt+='</table>\n';
      itxt+='</div>\n';
      itxt+='<div style="height:40px;"> </div>\n';
      itxt+='<div style="margin-left:auto;margin-right:auto;">\n';
      // header for description
      itxt+='<table border="0"><tr>\n';
      itxt+='<td style="background-color:#ccc;color:#ccc;width:10px;">-</td>\n';
      for(h=1;h<=adesc[0][0];h++){
        itxt+='<td><a href="javascript:rewrite_description(\''+adesc[h][1]+'\',\''+adesc[h][2]+'\')">';
        itxt+=adesc[h][0]+'</a></td>\n';
        itxt+='<td style="background-color:#ccc;color:#ccc;width:10px;">-</td>\n';
      }
      itxt+='</tr></table>\n';
      itxt+='</div>\n';
      itxt+='<div id="picture" style="width:500px;margin-left:auto;margin-right:auto;">\n';
      itxt+='waiting for server response...\n';
      itxt+='</div>\n';
      //itxt+='<div id="description" style="width:500px;text-align:left;margin-left:auto;margin-right:auto;">\n';
      itxt+='<div id="description" style="width:500px;margin-left:auto;margin-right:auto;">\n';
      itxt+='waiting for server response...\n';
      itxt+='</div>\n';
      itxt+='</center>\n';

      break;
    }
  }
  document.getElementById("indv").innerHTML=itxt;
  rewrite_description(adesc[1][1],adesc[1][2]);
}
function rewrite_indvlist(sptheadernow){
  //document.getElementById("indv").innerHTML='function started';
  var xmlindv    = reqindv.responseXML;
  var node_idv   = xmlindv.getElementsByTagName('indivisual');
  var itxt="";
  var i=0,h=0;
  var icount=0;
  var iflag_found=false;

  //for(i=0;i<node_idv.length;i++){
  for(i=node_idv.length-1;i>=0;i--){
    iflag_found=false;
    node_iheader=node_idv.item(i).getElementsByTagName("sptheader");
    for(h=0;h<node_iheader.length;h++){
      if(node_iheader.item(h).firstChild.nodeValue.indexOf(sptheadernow,0)==0){
        iflag_found=true;
        break;
      }
    }
    // case header found
    if(iflag_found==true){
      icount++;
      iid=node_idv.item(i).getElementsByTagName("id").item(0).firstChild.nodeValue;
      iauthor=
        node_idv.item(i).getElementsByTagName("author").item(0).firstChild.nodeValue;
      iyear=
        node_idv.item(i).getElementsByTagName("year").item(0).firstChild.nodeValue;
      ititle=
        node_idv.item(i).getElementsByTagName("title").item(0).firstChild.nodeValue;
      ijournal=
        node_idv.item(i).getElementsByTagName("journal").item(0).firstChild.nodeValue;
      ivolume=
        node_idv.item(i).getElementsByTagName("volume").item(0).firstChild.nodeValue;
      ipage=
        node_idv.item(i).getElementsByTagName("page").item(0).firstChild.nodeValue;
      inote=
        node_idv.item(i).getElementsByTagName("note").item(0).firstChild.nodeValue;
      // get sptlabel
      var asptlabel=Array();
      var asptidv=Array();
      node_isptlabel=node_idv.item(i).getElementsByTagName("sptlabel");
      node_isptidv  =node_idv.item(i).getElementsByTagName("spt");
      for(h=0;h<node_isptlabel.length;h++){
        asptlabel[h]=node_isptlabel.item(h).firstChild.nodeValue;
        asptidv[h]  =node_isptidv.item(h).firstChild.nodeValue;
      }

      // line contents

      if(listtype=="simple"){
        itxt+='<p>\n';
        //itxt+='<div style="padding:2px 2px 2px 2px;">\n';
        itxt+=iauthor+" ";
        itxt+="("+iyear+"), ";
        itxt+="<b>"+ititle+"</b>, ";
        itxt+="<i>"+ijournal+"</i>, ";
        itxt+=ivolume+", ";
        itxt+=ipage+"";
        //itxt+='</div>';
        itxt+="<br />\n";
        //itxt+='<div style="padding:2px 2px 2px 50px;">\n';
        itxt+=inote;
        //itxt+='</div>';
        itxt+="<br />\n";
        itxt+='</p>\n';
        itxt+="<br />\n";
        //itxt+='<div style="height:10px;"> </div>\n';
      }
      else{
        itxt+='<p>\n';
        itxt+='[<a id="'+iid+'"><i>ID:'+iid+'</i></a>]<br />\n';
        itxt+='<div style="padding:2px 2px 2px 2px;background-color:#888;color:#fff;">\n';
        itxt+=iauthor+" ";
        if(iyear!="") itxt+="("+iyear+")";
        itxt+=", <b>"+ititle+"</b>";
        if(ijournal!="") itxt+=", <i>"+ijournal+"</i>";
        if(ivolume!="") itxt+=", Vol. "+ivolume+"";
        if(ipage!="") itxt+=", Page "+ipage+"";
        itxt+='</div>';
        itxt+='<div style="padding:2px 2px 2px 50px;background-color:#ccf;">\n';
        itxt+=inote;
        itxt+='</div>';
        if(asptlabel.length>0){
          itxt+='<div style="padding:2px 2px 2px 50px;background-color:#ccf;color:#444;font-size:smaller;">\n';
          itxt+='TAGs:';
          for(h=0;h<asptlabel.length;h++){
            if(h!=0) itxt+=', ';
            itxt+='<a href="index.html?spt='+asptidv[h]+'" style="color:#444;">';
            itxt+=asptlabel[h];
            itxt+='</a>\n';
          }
          itxt+='</div>';
        }
        itxt+='</p>\n';
        itxt+='<div style="height:10px;"> </div>\n';
      }
    }
  }
  //if(itxt!=""){
  //  itxt='<center><table class="idvlist">\n'+itxt+'</table></center>\n';
  //}
  if(icount<8){
    //case no item found
    if(icount==0) itxt='no item found<br>\n';
    //insert spacer
    var iheight=480-60*icount;
    itxt+='<div style="width:100px;height:'+iheight+'px;" />\n';
  }

  document.getElementById("indv").innerHTML=itxt;
}

function get_sptupper(asptupper,sptnow,node_spt){
  for(i=0;i<node_spt.length;i++){
    ispt=node_spt.item(i).getElementsByTagName("spt_key").item(0).firstChild.nodeValue;
    if(ispt==sptnow){
      iroot=node_spt.item(i).getElementsByTagName("root").item(0).firstChild.nodeValue;
      ilabel=node_spt.item(i).getElementsByTagName("label").item(0).firstChild.nodeValue;
      asptupper[++asptupper[0][0]]=Array(ispt,ilabel);
      if(iroot!="0") asptupper=get_sptupper(asptupper,iroot,node_spt);
      break;
    }
  }
  return asptupper;
}
function get_spt1lower(sptnow,node_spt){
  var abranch=array();
  for(i=0;i<node_spt.length;i++){
    ispt=node_spt.item(i).getElementsByTagName("spt_key").item(0).firstChild.nodeValue;
    if(ispt==sptnow){
      node_branch=node_spt.item(i).getElementsByTagName("branch");
      for(h=0;h<node_branch.length;h++){
        abranch[h]=node_branch.item(h).firstChild.nodeValue;
      }
      break;
    }
  }
  return abranch;
}
function write_index_spacer(imax){
  var i=0;
  var itxt="";
  for(i=0;i<=imax;i++) itxt+="-";
  return itxt;
}
function write_treeview(ispt,itxt,iheader,node_spt){
  //list up rooted to ispt
  var i=0;
  for(i=0;i<node_spt.length;i++){
    iroot=node_spt.item(i).getElementsByTagName("root").item(0).firstChild.nodeValue;
    if(iroot==ispt){
      ispt_now=node_spt.item(i).getElementsByTagName("spt_key").item(0).firstChild.nodeValue;
      ilabel=node_spt.item(i).getElementsByTagName("label").item(0).firstChild.nodeValue;
      itxt+=iheader+ilabel+"(SPT:"+ispt_now+")<br>\n";
      // get lower
      itxt=write_treeview(ispt_now,itxt,iheader+"−",node_spt);
    }
  }
  return itxt;
}
function rewrite_treeview(sptnow){
  var xmlspts = reqspts.responseXML;
  var node_spt   = xmlspts.getElementsByTagName('spt');

  //var ispt=0;
  var iheader="";
  var itxt="";
  itxt=write_treeview(sptnow,itxt,iheader,node_spt);
  document.getElementById("indv").innerHTML=itxt;
}
function rewrite_spts(sptnow){
  var xmlspts        = reqspts.responseXML;
  var node_spt       = xmlspts.getElementsByTagName('spt');
  var itxt           ="";
  var itxt_spts      ="";
  var itxt_spts_upper="";

  itxt='<img src="./art-src/reading.gif" />\n';
  document.getElementById("spts_upper").innerHTML=itxt;

  itxt="";
  var asptupper=Array(Array("0","0"));
  asptupper=get_sptupper(asptupper,sptnow,node_spt);
  var sptheadernow="";
  var index_spacer=0;
  if(sptnow=="0"){
    itxt+='<a href="javascript:rewrite_spts(\'0\')">';
    itxt+='<font color="#dd0000">';
    itxt+='[検索トップ]</font></a><br />\n';
  }
  else{
    itxt+='<a href="javascript:rewrite_spts(\'0\')">';
    itxt+='[検索トップ]</a><br />\n';
  }
  for(i=asptupper[0][0];i>0;i--){
    sptheadernow+=asptupper[i][0]+",";
    itxt+=write_index_spacer(index_spacer++);
    if(i==1){
      itxt+='<img src="./art-src/index_blue.gif" width="10"/>';
      itxt+='<a href="javascript:rewrite_spts(\''+asptupper[i][0]+'\')">';
      itxt+='<font color="#dd0000">';
      itxt+=asptupper[i][1]+"</font></a><br />\n";
    }
    else{
      itxt+='<img src="./art-src/index_blue.gif" width="10"/>';
      itxt+='<a href="javascript:rewrite_spts(\''+asptupper[i][0]+'\')">';
      itxt+=asptupper[i][1]+"</a><br />\n";
    }
  }
  //document.getElementById("spts_upper").innerHTML=itxt;
  itxt_spts_upper=itxt;

  itxt="";
  for(i=0;i<node_spt.length;i++){
    iroot=node_spt.item(i).getElementsByTagName("root").item(0).firstChild.nodeValue;
    if(iroot==sptnow){
      ispt=node_spt.item(i).getElementsByTagName("spt_key").item(0).firstChild.nodeValue;
      ilabel=node_spt.item(i).getElementsByTagName("label").item(0).firstChild.nodeValue;
      //isptheader=node_spt.item(i).getElementsByTagName("sptheader").item(0).firstChild.nodeValue;
      inode_branch=node_spt.item(i).getElementsByTagName("branch");
      //itxt+='<a href="javascript:rewrite_spts(\''+ispt+'\',\''+isptheader+'\')">';
      itxt+=write_index_spacer(index_spacer);
      itxt+='<img src="./art-src/index_blue.gif" width="10"/>';
      itxt+='<a href="javascript:rewrite_spts(\''+ispt+'\')">';
      //itxt+=ilabel+'</a><font style="font-size:smaller;">['+ispt+']</font><br />\n';
      itxt+=ilabel+'</a><br />\n';
    }
  }
  itxt+='<div style="height:50px;"> </div>';
  itxt+='Links:<br />\n';
  //
  //itxt+='#<a target="_blank" href="/">';
  //itxt+='</a><br />\n';

  //Umizukan
  itxt+='#<a target="_blank" href="http://umizukan.com">';
  itxt+='Umizukan.com</a><br />\n';
  //Google schoolar
  itxt+='#<a target="_blank" href="http://scholar.google.com/">';
  itxt+='Google Scholar</a><br />\n';
  //Science Direct
  itxt+='#<a target="_blank" href="http://www.sciencedirect.com/">';
  itxt+='Science Direct</a><br />\n';
  //ALC
  itxt+='#<a target="_blank" href="http://www.alc.co.jp/">';
  itxt+='英辞朗</a><br />\n';

  itxt+='<br />\n';

  itxt+='Commands:<br />\n';
  itxt+='#<a href="index.html">';
  itxt+='Top</a><br />\n';
  // List type (Simple)
  itxt+='#<a href="index.html?listtype=simple">';
  itxt+='List Type: Simple</a><br />\n';
  // Tree view link
  itxt+='#<a href="javascript:rewrite_treeview(\''+sptnow+'\')">';
  itxt+='Show lower branches</a><br />\n';

  //document.getElementById("spts").innerHTML=itxt;
  itxt_spts=itxt;

  //rewrite indv window
  rewrite_indvlist(sptheadernow);
  document.getElementById("spts_upper").innerHTML=itxt_spts_upper;
  document.getElementById("spts").innerHTML=itxt_spts;
  if(def_id!=""){
    //window.location.hash=def_id;
	window.scrollTo(0,document.getElementById(def_id).offsetTop);
	def_id=-1;
  }

}

// Request URL ################################################################
function get_reqspts(){
  //document.getElementById("spts").innerHTML='Waiting for server response ...';
  document.getElementById("spts").innerHTML=' ';
  // create object
  if(!reqspts){ return; }
  // excute by the response
  reqspts.onreadystatechange=function(){
    getResponse_spts();
  }
  // set URL
  reqspts.open('GET',"art-spt.xml" ,true);
  // send HTTP request
  reqspts.send(null);
}
function get_reqindv(){
  //document.getElementById("indv").innerHTML='Waiting for server response ...';
  document.getElementById("indv").innerHTML=' ';
  if(!reqindv){ return; }
  // excute by the response
  reqindv.onreadystatechange=function(){
    getResponse_indv();
  }
  // set URL
  reqindv.open('GET',"art-idv.xml" ,true);
  // send HTTP request
  reqindv.send(null);
}
// ivent handler on response ##################################################
function getResponse_spts(){
  if(reqspts.readyState==4){
    // write index for the first contact
    rewrite_spts(def_spt);
  }
}
function getResponse_indv(){
  if(reqindv.readyState==4){
    get_reqspts();
  }
}

