var diary;
id=0;previd=0;idstart=0;idfinal=0;diarys=new Array();diaryload=false;

window.addEventListener('load',function(){
 var i,j,flag,e,elm,elem,$e,$i;
 elem=document.getElementsByTagName('div');
 for(i=0;i<elem.length;i++){
  if(elem[i].className.match(/group\d*/) && elem[i].parentNode.className.match(/says(\d+)/) && (RegExp.$1==1 || RegExp.$1==2)){
   flag=RegExp.$1-0;
   for(j=0;j<elem[i].childNodes.length;j++)if(!elem[i].childNodes[j].nodeValue){
    e=elem[i].childNodes[j];
    if(e.className=='label'){
     e.onclick=function(){
      for(elm=this.nextSibling;elm;elm=elm.nextSibling){
       if(!elm.nodeValue){
        elm.style.display=elm.style.display=='none'?'block':'none';
       }
      }
     };
    }else e.style.display='none';
   }
  }
 }
 $e=document.getElementById('diary');
 if($e){
  diary=new Diary($e.getAttribute('data-eno')-0,$e);
  $e=document.getElementById('diarybtn');
  $i=document.createElement('input');
  $i.setAttribute('type','button');
  $i.value='古い記録';
  $i.addEventListener('click',function(){diary.move(-1);},false);
  $e.appendChild($i);
  $i=document.createElement('input');
  $i.setAttribute('type','button');
  $i.value='後の記録';
  $i.addEventListener('click',function(){diary.move(1);},false);
  $e.appendChild($i);
 }
 itemtext();
},false);

function itemtext(){
 var i,j;
 var elems,erows;
 elems=document.getElementsByTagName('table');
 for(i=0;i<elems.length;i++){
  if(elems[i].className.match(/\bitem\b/)){
   erows=elems[i].getElementsByTagName('tr');
   for(j=0;j<erows.length;j++){
    if(erows[j].getElementsByTagName('td').length){
     erows[j].getElementsByTagName('td')[0].addEventListener('mouseover',function(e){
      var pxleft,pxtop;
      var elem=this;
      if(!this.getAttribute('data-text')){
       elem=document.getElementById('itemtext');
       if(elem){elem.parentNode.removeChild(elem);}
       return;
      }
      pxleft=elem.offsetWidth;pxtop=0;
      while(elem && elem.nodeName!='body'){pxleft+=elem.offsetLeft;pxtop+=elem.offsetTop;elem=elem.offsetParent;}
      elem=document.getElementById('itemtext');
      if(!elem)elem=document.createElement('div');
      while(elem.firstChild)elem.removeChild(elem.firstChild);
      elem.setAttribute('id','itemtext');
       elem.style.position='absolute';
      elem.style.left=pxleft+'px';elem.style.top=pxtop+'px';
      elem.style.height=elem.style.lineHeight=(this.offsetHeight-this.style.borderBottomWidth)+'px';
      elem.style.minWidth=(this.parentNode.offsetWidth-this.offsetWidth)+'px';
      elem.style.background='#ffffff';
      elem.style.whiteSpace='nowrap';
      elem.appendChild(document.createTextNode(this.getAttribute('data-text')));
      document.body.appendChild(elem);
     },false);
    }else{
     erows[j].addEventListener('mouseover',function(e){
      var elem=document.getElementById('itemtext');
      if(elem)elem.parentNode.removeChild(elem);
     },false);
    }
   }
  }
 }
}

function Diary(eno,$root){
 this.idnow=0;
 this.idstart=0;
 this.idfinal=0;
 this.pages=new Array();
 this.loaded=false;
 this.eno=eno;
 this.$count=$root.getElementsByTagName('span')[0];
 this.$content=$root.getElementsByTagName('div')[1];
 this.idnow=this.$count.firstChild.nodeValue-0;
 this.move=function(val){
  var i,id,$e;
  if(!this.loaded){
   this.load();
   document.head.lastChild.setAttribute('data-move',val);
   return;
  }
  id=this.idnow+val;
  if(id<this.idstart){alert('これより前はありません。');return;}
  if(id>this.idfinal){alert('これより後はありません。');return;}
  this.idnow=id;
  while(this.$count.firstChild)this.$count.removeChild(this.$count.firstChild);
  this.$count.appendChild(document.createTextNode(id));
  while(this.$content.firstChild)this.$content.removeChild(this.$content.firstChild);
  $e=parseHTML(this.pages[id-this.idstart].replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&'));
  while($e.firstChild){
   this.$content.appendChild($e.firstChild);
  }
 };
 this.load=function(){
  var $e;
  $e=document.createElement('script');
  $e.setAttribute('src','../diary.cgi?eno='+this.eno);
  document.head.appendChild($e);
  $e.addEventListener('load',function(e){document.body.style.removeProperty('cursor');});
  $e.addEventListener('error',function(e){e.target.dispatchEvent(new UIEvent('load'));});
  document.body.style.cursor='wait';
 };
 this.onload=function(idstart,idfinal,pages){
  this.loaded=true;
  this.idstart=idstart;this.idfinal=idfinal;this.pages=pages;
  this.move(document.head.lastChild.getAttribute('data-move')-0);
 };
};
parseHTML=function(txt){
 var i,j,k,temp,atts,kvs,csss,stls,$e,$root,$now;
 $root=document.createElement('div');
 $now=$root;
 i=0;
 while(txt.length){
  i=txt.indexOf('<');
  temp=i<0?txt:txt.substr(0,i);
  txt=i<0?'':txt.substr(i+1);
  $now.appendChild(document.createTextNode(temp));
  if(i<0)continue;
  if(txt.charAt(0)=='/'){$now=$now.parentNode;txt=txt.substr(txt.indexOf('>')+1);continue;}
  i=txt.indexOf('>');
  temp=txt.substr(0,i);
  txt=txt.substr(i+1);
  atts=temp.split(/ /);
  $e=document.createElement(atts.shift());
  for(j=0;j<atts.length;j++){
   let k=atts[j].indexOf('=');
   kvs=k<0?[atts[j],atts[j]]:[atts[j].substr(0,k),atts[j].substr(k+1)];
   if(kvs[0]=='style'){
    csss=kvs[1].replace(/^([\x22\x27])(.*)\1$/,"$2").split(/;/);
    for(k=0;k<csss.length;k++){
     stls=csss[k].split(/:/,2);
     $e.style.setProperty(stls[0],stls[1]);
    }
   }else{$e.setAttribute(kvs[0],kvs[1].replace(/^([\x22\x27])(.*)\1$/,"$2"));}
  }
  $now.appendChild($e);
  if($e.nodeName.toLowerCase()!='br' && $e.nodeName.toLowerCase()!='img')$now=$e;
 }
 return $root;
}
function loaddiary(ret){
 diary.onload(ret.start,ret.final,ret.diary);
}
