// ------------------------------------
// フロートメニューライブラリ Ver.1.05 
//                              By k.r 
// ------------------------------------

var timerID  = null;
var timerID2 = null;
var displayMenuID  = -1;
var displayMenuID2 = -1;
var parentFmAncObj;

function FmDisp(obj,index)
{
	var i;
	var fm;
	var html = "";
	var left,top;
	var width,height;
	var clientWidth,scrollLeft;
	var fullurl;
	var linkdir  = eval("FMenuLinkDir"  + index);
	var linktext = eval("FMenuLinkText" + index);
	var linkhref = eval("FMenuLinkHref" + index);
	var parenttbl= eval("FMenuParentTbl"+ index);
	
	if( !linktext || linktext[0] == null )
		return;
	
	if( !obj || !document.getElementById )
		return;
	
	if( !(fm = document.getElementById("FloatMenu")) ){
		fm = document.createElement('span');
		fm.id = "FloatMenu";
		document.getElementById("FloatMenuOut").appendChild(fm);
	}
	
	if( timerID ){
		clearTimeout(timerID);
		timerID = null;
	}
	if( displayMenuID == index )
		return;
	displayMenuID = index;
	
	if( timerID2 )	clearTimeout(timerID2);
	FmHideChild(1);
	
	html += '<table border=0 bgcolor=#808080 cellspacing=1 cellpadding=1 onmouseover="FmDisp(1,' + index + ');" onmouseout="FmHide();"><tr>\n';
	html += '<td bgcolor=#ffffff>\n';
	html += '<table border=0 cellspacing=3 cellpadding=1>\n';
	
	for( i = 0; linktext[i] != null; i++ ){
		if( linkhref[i].substr(0,5) == "http:" )	fullurl = linkhref[i];
		else										fullurl = MainURL + linkdir + "/" + linkhref[i];
		
		if( parenttbl[i] != -1 )	continue;
		if( location.href == fullurl ){
			if( CheckChild(index,i) ){
				html += '<tr><td width=100% nowrap class=FMenuTD2 onmouseover="FmDispChild(this,' + index + ',' + i + ');" onmouseout="FmHideChild();">\n';
				if( FmChildAllow )	html += '<img src="' + FmChildAllow + '" width=6 height=12 align=right border=0 hspace=0 vspace=0>';
				html += '<b>' + linktext[i] + '</b>\n';
				html += '</td></tr>\n';
			}
			else{
				html += '<tr><td width=100% nowrap class=FMenuTD2>\n';
				html += '<b>' + linktext[i] + '</b>\n';
				html += '</td></tr>\n';
			}
		}
		else{
			if( CheckChild(index,i) ){
				html += '<tr><td width=100% nowrap class=FMenuTD onmouseover="FmOver(this);FmDispChild(this,' + index + ',' + i + ');" onmouseout="FmOut(this);FmHideChild();" onmousedown="FmDown(this)" onclick="FmClick(this)">\n';
				if( FmChildAllow )	html += '<img src="' + FmChildAllow + '" width=6 height=12 align=right border=0 hspace=0 vspace=0>';
				html += '<a href="' + fullurl + '" class=FMenuLink><b>' + linktext[i] + '</b></a>\n';
				html += '</td></tr>\n';
			}
			else{
				html += '<tr><td width=100% nowrap class=FMenuTD onmouseover="FmOver(this);" onmouseout="FmOut(this);" onmousedown="FmDown(this)" onclick="FmClick(this)">\n';
				html += '<a href="' + fullurl + '" class=FMenuLink><b>' + linktext[i] + '</b></a>\n';
				html += '</td></tr>\n';
			}
		}
	}
	html += '</table>\n';
	html += '</td>\n';
	html += '</tr></table>\n';
	
	fm.style.position = "absolute";
	fm.style.zIndex   = "3";
	fm.innerHTML = html;
	width = fm.offsetWidth;
	height= fm.offsetHeight;
	
	left = GetAbsPosLeft(obj);
	top  = GetAbsPosTop(obj);
	top += obj.offsetHeight;
	
	clientWidth = document.body.clientWidth || window.innerWidth;
	scrollLeft = document.body.scrollLeft || window.pageXOffset;
	
	if( left+width > clientWidth+scrollLeft )
		left = clientWidth+scrollLeft - width;
	if( left-scrollLeft < 0 )
		left = scrollLeft;
	
	fm.style.left = left;
	fm.style.top  = top;
}

function FmHide(flag)
{
	if( displayMenuID == -1 )
		return;
	
	if( flag ){
		document.getElementById("FloatMenu").innerHTML = "";
		timerID = null;
		displayMenuID = -1;
		return;
	}
	
	timerID = setTimeout("FmHide(1);",500);
}

function FmDispChild(obj,index,prnt)
{
	var i;
	var fm;
	var html = "";
	var left,top;
	var width,height;
	var clientWidth,scrollLeft;
	var fullurl;
	var linkdir  = eval("FMenuLinkDir"  + index);
	var linktext = eval("FMenuLinkText" + index);
	var linkhref = eval("FMenuLinkHref" + index);
	var parenttbl= eval("FMenuParentTbl"+ index);
	
	if( !linktext || linktext[0] == null )
		return;
	
	if( !obj || !document.getElementById )
		return;
	
	if( !(fm = document.getElementById("FloatMenu2")) ){
		fm = document.createElement('span');
		fm.id = "FloatMenu2";
		document.getElementById("FloatMenuOut").appendChild(fm);
	}
	
	if( timerID2 ){
		clearTimeout(timerID2);
		timerID2 = null;
	}
	if( displayMenuID2 == prnt )
		return;
	displayMenuID2 = prnt;
	
	if( obj.getElementsByTagName("a")[0] )	parentFmAncObj = obj;
	else									parentFmAncObj = 0;
	
	html += '<table border=0 bgcolor=#808080 cellspacing=1 cellpadding=1 onmouseover="FmDisp(1,' + index + ');FmOver(parentFmAncObj);FmDispChild(1,' + index + ',' + prnt + ');" onmouseout="FmHide();FmOut(parentFmAncObj);FmHideChild();"><tr>\n';
	html += '<td bgcolor=#ffffff>\n';
	html += '<table border=0 cellspacing=3 cellpadding=1>\n';
	
	for( i = 0; linktext[i] != null; i++ ){
		if( linkhref[i].substr(0,5) == "http:" )	fullurl = linkhref[i];
		else										fullurl = MainURL + linkdir + "/" + linkhref[i];
		
		if( parenttbl[i] != prnt )		continue;
		if( location.href == fullurl ){
			html += '<tr><td width=100% nowrap class=FMenuTD2>\n';
			html += '<b>' + linktext[i] + '</b>\n';
			html += '</td></tr>\n';
		}
		else{
			html += '<tr><td width=100% nowrap class=FMenuTD onmouseover="FmOver(this)" onmouseout="FmOut(this)" onmousedown="FmDown(this)" onclick="FmClick(this)">\n';
			html += '<a href="' + fullurl + '" class=FMenuLink><b>' + linktext[i] + '</b></a>\n';
			html += '</td></tr>\n';
		}
	}
	html += '</table>\n';
	html += '</td>\n';
	html += '</tr></table>\n';
	
	fm.style.position = "absolute";
	fm.innerHTML = html;
	width = fm.offsetWidth;
	height= fm.offsetHeight;
	
	left = GetAbsPosLeft(obj);
	top  = GetAbsPosTop (obj);
	left += obj.offsetWidth;
	
	clientWidth = document.body.clientWidth || window.innerWidth;
	scrollLeft = document.body.scrollLeft || window.pageXOffset;
	
	if( left+width > clientWidth+scrollLeft )
		left = clientWidth+scrollLeft - width;
	if( left-scrollLeft < 0 )
		left = scrollLeft;
	
	fm.style.left = left;
	fm.style.top  = top;
}

function FmHideChild(flag)
{
	if( displayMenuID2 == -1 )
		return;
	
	if( flag ){
		document.getElementById("FloatMenu2").innerHTML = "";
		timerID2 = null;
		displayMenuID2 = -1;
		return;
	}
	
	timerID2 = setTimeout("FmHideChild(1);",500);
}

function CheckChild(index,prnt)
{
	var i;
	var parenttbl= eval("FMenuParentTbl" + index);
	
	for( i = 0; parenttbl[i] != null; i++ ){
		if( parenttbl[i] == prnt )
			return 1;
	}
	
	return 0;
}

function CheckChildHref(index,prnt,href2)
{
	var i;
	var linkhref = eval("FMenuLinkHref" + index);
	var parenttbl= eval("FMenuParentTbl"+ index);
	
	if( location.href == href2 )
		return 1;
	
	for( i = 0; parenttbl[i] != null; i++ ){
		if( parenttbl[i] == prnt  && linkhref[i] == location.href )
			return 1;
	}
	
	return 0;
}


function GetAbsPosLeft(obj)
{
	var x = 0;
	
	do{
		x += obj.offsetLeft;
	}while( obj = (obj.offsetParent || obj.parentElement) );
	
	return( x );
}

function GetAbsPosTop(obj)
{
	var y = 0;
	
	do{
		y += obj.offsetTop;
	}while( obj = (obj.offsetParent || obj.parentElement) );
	
	return( y );
}


function FmOver(obj)
{
	if( !obj )		return;
	obj.style.background = "#e0e0ff";
	obj.getElementsByTagName("a")[0].style.color = "#505050";
}

function FmOut(obj)
{
	if( !obj )		return;
	obj.style.background = "#ffffff";
	obj.getElementsByTagName("a")[0].style.color = "#505050";
}

function FmDown(obj)
{
	if( !obj )		return;
	obj.style.background = "#505080";
	obj.getElementsByTagName("a")[0].style.color = "#ffffff";
}

function FmClick(obj)
{
	if( !obj )		return;
	location.href = obj.getElementsByTagName("a")[0].href;
}

