// イベントハンドラ：onMouseover
// 外部関数：addAnchor,getDTfromAnc
// 特徴：DIV#popupBase下に「フラットでリニア」にDL#p\dを作成する。
// 要注意：LABELNUMBERのないブラウザによるHTML上不適正な出力は、ブラウザが強制的に解釈しなおし、メモリ上で変更するらしい。
// 　　　　すなわち、<dt>の前に出力された<a name=""></a>は、その一つ前のDDの中に存在するものと解釈されてしまう（DDの終了タグが後ろにずれ込む解釈）。ここでレス番号がずれることになる。
// 　　　　結果、souceIndexでは、[a,dt,dd],[a,dt,dd]...と並ぶが、ノードツリーでは(a),[dt,dd,a],[dt,dd...]と並ぶようである。最初の(a)だけはDLの子ノードとして扱われる。
//==========CSSの出力
var nCSS=''
nCSS+='#popupBase{font-size:90%;}'; //フォントサイズの指定
nCSS+='#popupBase{position:absolute;width:100%;height:100%;z-index:7}';
nCSS+='#popupBase dl{position:absolute; background-color:window; border:outset 1px infobackground; overflow-y:auto; z-index:5; border-top:none}';
nCSS+='#popupBase dt span{float:none; margin-left:1em;}';
nCSS+='#popupBase dd{margin:auto 1em}';
document.write('<style type="text/css">'+nCSS+'</style>\n');
//=========ナンバーな名前のポップアップ
function namePopup(e,before,num,after){
 var hnum=tohan(num);
 if(before){if(hnum==3 || hnum==774 || hnum==4){return}}
 e.outerHTML = "<b>"+before+"</b><b>"+addAnchor(hnum,num)+"</b><b>"+after+"</b>";
}
//=========多段ポップアップ
//・ポップアップの作成
function makePopContent(obj){
 //事前準備
 var num=event.srcElement.innerText.replace(/[>＞]/g,"");
 var number=tohan(num);
 if(!obj.rel){obj.rel=obj.href;}
 obj.href="decoy:";
 //調査
 if(document.anchors.length==1){return} //レス数が1なら終了
 var startRes=parseInt(document.anchors[1].name.replace(/\D/g,""));
 if(number.match(/(\d*)\D+(\d*)/)){var start=parseInt(RegExp.$1); var end=parseInt(RegExp.$2);}
 else                             {var start=end=parseInt(number);}
 if(startRes>end && end!=1){obj.href=obj.rel;return}//存在していなければ終了
 //中身の切り出し
 var targetString="";
 if(start==end){targetString=returnString(start)}
 else          {for(var i=0;i<=end-start;i++){targetString+=returnString(start+i)}}
 //対象が存在したらrelに退避してポップアップ
 if(targetString){popup(targetString);}else{obj.href=obj.rel;}
}
//・中身の切り出し
function returnString(num){
 var obj=getDTfromAnc(num);
 if(!obj){return("")} //対象が透明あぼ〜んなら終了
 var dt,dd,dtOuter,ddOuter;
 dt=obj.cloneNode(true);
  dtOuter=dt.outerHTML.replace(/name=.*?>/,">"); // LABELNUMBER対応のものに係るリンクアンカーの除去
 dd=obj.nextSibling.cloneNode(true);
  while(dd.lastChild.name){dd.lastChild.removeNode(true)} // LABELNUMBER非対応のものに係るリンクアンカーの除去
  if(dd.lastChild.tagName=="DL"){dd.lastChild.removeNode(true)} // 逆参照の除去
  ddOuter=dd.outerHTML;
 return(dtOuter+ddOuter);
}
//・ポップアップ
function popup(inner){
 if(!document.getElementById("popupBase")){document.body.insertAdjacentHTML('afterBegin','<div id="popupBase"></div>')}
 //要素の作成
 var aNum=event.srcElement.sourceIndex;
 if(document.getElementById('p'+aNum)){return} // ポップアップしてたら終了
 var nHTML='<dl id="p'+aNum+'">'+inner+'</dl>';
 document.getElementById("popupBase").insertAdjacentHTML("beforeEnd",nHTML);
 //要素の配置
 var p=document.getElementById('p'+aNum);
 // y軸調整
 var pos=Math.min(event.y,document.body.clientHeight-event.y);
 var scTop=document.body.scrollTop+event.y;
 if(pos==event.y){var y=scTop-30;}  // 下
 else            {var y=scTop+10-p.clientHeight;}  // 上
 if(y<0){y=0}
 if(event.srcElement.parentElement.id=="foundResult"){y+=25}
 p.style.pixelTop=y;
 // x軸調整
 var pos=Math.min(event.x,document.body.clientWidth-event.x);
 var scLeft=document.body.scrollLeft+event.x;
 if(pos==event.x){var x=scLeft-2}  // 右
 else            {var x=scLeft-4-p.clientWidth;}  // 左
 if(x<0){x=0}
 p.style.pixelLeft=x;
 // 高さ調整（scrollBarを要す場合と要しない場合がある）→y軸調整
 if(p.clientHeight>Math.max(event.y,document.body.clientHeight-event.y)){
  if(p.clientHeight>document.body.clientHeight){p.style.pixelHeight=document.body.clientHeight-2;}
  p.style.pixelTop=document.body.scrollTop;
 }
}
//・ポップアップ消去
function removePopup(popid){
 if(document.getElementById("popupBase")){document.getElementById("popupBase").removeNode(true)}
}
//数値変換（to半角）
function tohan(num){
 var zen="０１２３４５６７８９";
 var han="0123456789";
 var hnum="";
 if(zen.indexOf(num.charAt(0))!=-1){
  for(var i=0;i<num.length;i++){
   var at=zen.indexOf(num.charAt(i));
   hnum+=han.charAt(at);
  }
 }else{hnum=num}
 return hnum;
}
