LayerWidth = 351;
LayerHeight = 200; //これは後から消す
PopupFontSize =13;
LetterPerLine = Math.floor(LayerWidth / PopupFontSize); //概算の一行あたりの文字数
PixelPerLine = 18; //一行の縦のピクセル数

IsPopupShow = false;
PastLength = 0; // 履歴の数
PastCount = 0; // 現在表示されている履歴の番号
PastStr = new Array(); // 履歴を格納する配列
LayerMoveFlag = false; //レイヤー移動フラグ
LayerDontMoveFlag = false; //リンクをクリックしたときもレイヤーが移動するのを防ぐ。
LayerMoveX = 0; //レイヤー移動開始時のマウスX,Y座標
LayerMoveY = 0;

// Netscapeではレイヤーの大きさを得ることが出来ないため指定することにしました。
// フレームサイズ固定なので、指定してあげられると思うので...
if( IsNN() ){
	// ここを変更する必要あり。
	if (!IsNN3()) {
		NnFrameHeight = window.innerHeight - 30;
		NnFrameWidth = window.innerWidth;
	}
}

// ポップアップウィンドウを描画する。
function DrawPopup(index)
{
	if (IsNN3()) {
		// Netscape 3.0 はポップアップウィンドウをサポートしていない。
		return;
	}
	a = PopupStrings[index];
	b = a.length / LetterPerLine;
	LayerHeight = (Math.floor(b) + 7 ) * PixelPerLine;
//	alert("LayerHeight:"+LayerHeight.toString());
	if( !IsPopupShow ){ //ポップアップが表示されていない場合

		if( WindowY > GetFrameHeight()-LayerHeight-15 ){ // マウスが下すぎるとき
			popup_y = DocumentY - LayerHeight - 15;
		}else{
			popup_y = DocumentY + 15;
		}
		if( WindowX > GetFrameWidth()-LayerWidth-15 ){
			popup_x = DocumentX - LayerWidth - 15;
		}else{
			popup_x = DocumentX + 15;
		}
		MoveToLayer("popup",popup_x,popup_y); //背景レイヤの位置
		ResizeToLayer("popup",LayerWidth,LayerHeight); //背景レイヤの大きさ
		BgcolorLayer("popup","#ffff66");
		//BgimageLayer("popup","back.gif"); //背景レイヤの背景画像を設定
		ResizeToLayer("popup_str",LayerWidth-4,LayerHeight-4); //文字列レイヤの大きさ
		MoveToLayer("popup_str",popup_x+3,popup_y+2); //文字列レイヤの位置
		
		BgcolorLayer("popup_str","#ffffcc"); //文字列レイヤの背景色

		ZindexLayer("popup_str",2);
//	alert("LayerHeight:"+LayerHeight.toString());
		IsPopupShow = true;
	}

	// 用語集文字列を付加
	WordStr = "<BR><BR>";
	words = new Array();
	words = PopupWords[index].split(",");
	for( i=0; i<words.length; i++){
		if(!PopupName[words[i]]) continue;
		WordStr += "<A href='javascript:NullLink()' onClick=\"ShowPopup('";
		WordStr += words[i];
		WordStr += "')\">";
		WordStr += PopupName[words[i]];
		WordStr += "</A> ";
	}
	WordStr += "<BR>";
	
	RewriteLayer("popup_str",BeforeStr+PopupStrings[index]+WordStr+AfterStr);
	ShowLayer("popup");
	ShowLayer("popup_str");
}

function ShowPopup(index)
{
	//IEで、リンクをクリックしたときもレイヤーが移動してしまうため。
	if( IsPopupShow ){
		LayerDontMoveFlag = true;
	}
	
	DrawPopup(index);
	PastCount++;
	PastLength = PastCount;
	PastStr[PastCount-1] = index;
}

function ClosePopup()
{
	HideLayer("popup_str");
	HideLayer("popup");
	IsPopupShow = false;
	PastCount = 0;
	delete PastStr;
	PastStr = new Array();

	//IEで、リンクをクリックしたときもレイヤーが移動してしまうため。
	LayerDontMoveFlag = true; 
}

function BackPopup()
{
	//IEで、リンクをクリックしたときもレイヤーが移動してしまうため。
	LayerDontMoveFlag = true; 

	if( PastCount <= 1 ){
		ClosePopup();
		return;
	}
	PastCount--;
	DrawPopup( PastStr[PastCount-1] );
}

function ForwardPopup()
{
	//IEで、リンクをクリックしたときもレイヤーが移動してしまうため。
	LayerDontMoveFlag = true; 

	if( PastCount >= PastLength ){
		return;
	}
	PastCount++;
	DrawPopup( PastStr[PastCount-1] );
}

function LayerOnClick()
{
	if( LayerDontMoveFlag){
		LayerDontMoveFlag = false;
		LayerMoveFlag = false;
		return;
	}
	LayerMoveFlag = !LayerMoveFlag;
	if( LayerMoveFlag ){
		LayerMoveX = DocumentX;
		LayerMoveY = DocumentY;
		PopupFirstLeft = GetLeftLayer("popup");
		PopupFirstTop = GetTopLayer("popup");
		PopupstrFirstLeft = GetLeftLayer("popup_str");
		PopupstrFirstTop = GetTopLayer("popup_str");
	}
}

function LayerOnMouseDown()
{
	LayerMoveFlag = true;
	LayerMoveX = DocumentX;
	LayerMoveY = DocumentY;
	PopupFirstLeft = GetLeftLayer("popup");
	PopupFirstTop = GetTopLayer("popup");
	PopupstrFirstLeft = GetLeftLayer("popup_str");
	PopupstrFirstTop = GetTopLayer("popup_str");
}

function LayerOnMouseUp()
{
	LayerMoveFlag = false;
}

function MakeLayer()
{
//	alert(LetterPerLine);
	if(IsNN()){
		document.write("<LAYER name='popup' visibility='hide'></LAYER>");
		document.write("<LAYER name='popup_str' visibility='hide'></LAYER>");
	}
	if(IsIE()){
		document.write("<DIV style='position:absolute; visibility:hidden;' id='popup' onClick='LayerOnClick()'></DIV>");
		//document.write("<DIV style='position:absolute; visibility:hidden; font-size:"+PopupFontSize.toString()+";' onClick='LayerOnClick()' onmousedown='LayerOnMouseDown()' onmouseup='LayerOnMouseUp()' id='popup_str' >b</DIV>");
		document.write("<DIV style='position:absolute; visibility:hidden; font-size:"+PopupFontSize.toString()+";' onClick='LayerOnClick()' id='popup_str' >b</DIV>");
	}
}


// ----- 以下、クロスブラウザ用関数群 -----
// WindowX,Y:ウィンドウ内座標 , DocumentX,Y:ドキュメントの座標(スクロール関係あり）
var WindowX=0,WindowY=0,DocumentX=0,DocumentY=0;
var debag;

function IsNN3(){
	return navigator.appVersion.substring(0,3) < 4.0;
}

function IsNN(){
	return navigator.appName.charAt(0) == "N";
}

function IsIE(){
	return navigator.appName.charAt(0) == "M";
}

function NullLink()
{
	if( IsNN() ){
//		return void(0);
	}
	if( IsIE() ){;
	}
}

function OnLoad()
{
	if( IsNN() ){
		document.onmousemove = OnMouseMove;
		if (!IsNN3()) {
			document.captureEvents(Event.MOUSEMOVE);
		}
	}
	if( IsIE() ){
		document.onmousemove = OnMouseMove;
	}
}

function OnMouseMove(Event)
{
	if( IsNN() ){
		WindowX = Event.x;
		WindowY = Event.y;
		DocumentX = Event.pageX;
		DocumentY = Event.pageY;
		//document.Form1.Edit1.value = "Y:"+(Event.y).toString()+" PageY:" + (Event.pageY).toString() + " ScreenY:" + (Event.screenY).toString();
	}
	
	if( IsIE() ){
		WindowX = event.clientX;
		WindowY = event.clientY;
		DocumentX = event.x;
		DocumentY = event.y;
	}
	
	// レイヤー移動フラグが立っているときはレイヤーを移動させる。
	if( LayerMoveFlag ){
		dx = DocumentX - LayerMoveX;
		dy = DocumentY - LayerMoveY;
		MoveToLayer("popup",PopupFirstLeft+dx,PopupFirstTop+dy);
		MoveToLayer("popup_str",PopupstrFirstLeft+dx,PopupstrFirstTop+dy);
	}
}

function GetFrameWidth()
{
	if( IsNN() ){
		return NnFrameWidth;
	}
	if( IsIE() ){
		return self.document.body.clientWidth;
	}
}

function GetFrameHeight()
{
	if( IsNN() ){
		return NnFrameHeight
	}
	if( IsIE() ){
		return self.document.body.clientHeight;
	}
}

function ShowLayer(layer)
{
	if(IsNN()){
		document.layers[layer].visibility = "show";
	}
	if(IsIE()){
		document.all[layer].style.visibility = "visible";
	}
}

function HideLayer(layer)
{
	if(IsNN()){
		document.layers[layer].visibility = "hide";
	}
	if(IsIE()){
		document.all[layer].style.visibility = "hidden";
	}
}

function IsShowLayer(layer)
{
	if(IsNN()){
		return document.layers[layer].visibility == "show";
	}
	if(IsIE()){
		return document.all[layer].style.visibility == "visible";
	}
}

function GetTopLayer(layer)
{
	if(IsNN()){
		return document.layers[layer].top;
	}
	if(IsIE()){
		return document.all[layer].style.pixelTop;
	}
}

function GetLeftLayer(layer)
{
	if(IsNN()){
		return document.layers[layer].left;
	}
	if(IsIE()){
		return document.all[layer].style.pixelLeft;
	}
}

function GetWidthLayer(layer)
{
	if(IsNN()){
		return document.layres[layer].clip.width;
	}
	if(IsIE()){
		return document.all[layer].style.width;
	}
}

function GetHeightLayer(layer)
{
	if(IsNN()){
		return document.layers[layer].height;//clip.height;
	}
	if(IsIE()){
		return document.all[layer].style.height;//.clipHeight;
	}
}

function MoveToLayer(layer,left,top)
{
	if(IsNN()){
		document.layers[layer].left = left;
		document.layers[layer].top = top;
	}
	if(IsIE()){
		document.all[layer].style.posLeft = left;
		document.all[layer].style.posTop = top;
	}
}

function ResizeToLayer(layer,width,height)
{
	if(IsNN()){
		document.layers[layer].resizeTo(width,height);
		document.layers[layer].clip.width = width;
		document.layers[layer].clip.height = height;
	}
	if(IsIE()){
		document.all[layer].style.width = width+"px";
		document.all[layer].style.height = height+"px";
	}
}

function RewriteLayer(layer,source)
{
	if(IsNN()){
		// 改行させるためにTableタグを導入。
		document.layers[layer].document.open();
		document.layers[layer].document.write("<TABLE><TR><TD width=");
		document.layers[layer].document.write(LayerWidth-12);
		document.layers[layer].document.write(">");
		//document.layers[layer].document.write(document.layers[layer].clip.width);
		//document.layers[layer].document.write(">");
		document.layers[layer].document.write(source);
		document.layers[layer].document.write("</TD></TR></TABLE>");
		document.layers[layer].document.close();
	}
	if(IsIE()){
		document.all[layer].innerHTML = source;
	}
}

function BgcolorLayer(layer,color)
{
	if(IsNN()){
		document.layers[layer].bgColor = color;
	}
	if(IsIE()){
		document.all[layer].style.backgroundColor = color;
	}
}

function BgimageLayer(layer,src)
{
	if(IsNN()){
		document.layers[layer].background.src = src;
	}
	if(IsIE()){
		document.all[layer].style.backgroundImage = "url(" + src + ")";
	}
}

function ZindexLayer(layer,z)
{
	if(IsNN()){
		document.layers[layer].zIndex = z;
	}
	if(IsIE()){
		document.all[layer].style.zIndex = z;
	}
}

