//	-*- coding: UTF-8 -*-
// Copyright 2004 Mizutori Crafts. All rights reserved.
// Author: Mizutori Tetsuya <support@mizutori.net>
// No part of this program may be reproduced or transmitted in any form or 
// by any means without permission from the author. Mizutori Crafts.
//
// Date: April 10, 2004.
// Revised: April 10, 2004
// Version: 1.00; April 10, 2004.
// Version: 1.01; June 29, 2004. (z-index:255, Alpha(opacity) )

// Usage:
/**
<head>
	<script type="text/javascript" src="../scripts/popmenu.js"></script>
	<script type="text/javascript" src="../scripts/popmenu_data.js"></script>
</head>

	<body>
	<a onmouseover="mouseoverPopmenu(this,event,'menu01');" onmouseout="setTimerPopmenu();" href="frame10/index.html" title="menu_01">[Menu 01]</a>
	<a onmouseover="mouseoverPopmenu(this,event,'menu02');" onmouseout="setTimerPopmenu();" href="#" title="menu_02">[Menu 02]</a>
	<script type="text/javascript">putPopmenuLayers("..");</script>
</body>
*/

// global variables:

var gCurrMenuID = null; // current visible menu layer id
var gMenuTimer = null; // callback function
var gMenuItems = new Array(); // array of cMenu

var version = navigator.appVersion.substring(0,1);
var browser = getBrowserType(); // browser type
var is_ie = ((document.all) ? true:false);
var is_w3c = ((document.getElementById) ? true:false);
var is_ns = ((document.layers) ? true:false);
//if (is_ie) { is_w3c = false };

// object class

function cRect(x,y,w,h) {
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
}

function cMenu(id,fgcolor,bgcolor,width,items) {
	this.id = id;
	this.fgcolor = fgcolor;
	this.bgcolor = bgcolor;
	this.width = width;
	this.items = items;
}

// common functions:

function getBrowserType(){
	if ( document.all ) { return "ie"; } // IE
	else if ( document.getElementById ) { return "w3c"; } // W3C
	else if ( document.layers ) { return "ns"; } // NS
	else { return null; }
}

function getLayer(id) {
	var layer = null;
	if (is_ie) { layer = eval("document.all."+id+".style"); }
	else if (is_w3c) { layer = eval("document.getElementById('"+id+"').style"); }
	else if (is_ns) { layer = eval("document.layers['"+id+"']"); }
	return layer;
}

// function getPosition
//	var rect = getPosition(obj,event);
//	var x = rect.x;

function getPosition(obj,event) {
	var mx,my;
	var x,y,w,h;

	if (is_ie || is_w3c) {
		mx = event.clientX;
		my = event.clientY;
		x = obj.offsetLeft;
		y = obj.offsetTop;
		w = obj.offsetWidth;
		h = obj.offsetHeight;
		var parobj = obj;
		while (parobj.offsetParent) {
			parobj = parobj.offsetParent;
			x += parobj.offsetLeft;
			y += parobj.offsetTop;
		}

	} else if (is_ns) {
		mx = event.pageX;
		my = event.pageY;
		x = obj.x;
		y = obj.y;
		w = 16;
		h = 12;
	}

//	window.alert(obj.name+":("+mx+","+my+")");
//	window.alert(obj.name+":("+x+","+y+"):("+w+","+h+")");

	return new cRect(x,y,w,h);
}

// popmenu functions (alias):
//function setTimer(secs) { setTimerPopmenu(secs); }
//function clearTimer() { clearTimerPopmenu(); }
//function menuOver(obj,event,menuid) { mouseoverPopmenu(obj,event,menuid); }

// popmenu functions:

function initPopmenu() {
	browser = getBrowserType();
}

function setTimerPopmenu(secs) {
	if (secs == undefined) { secs = 500; }
	gMenuTimer = setTimeout("hidePopmenu()",secs);
}

function clearTimerPopmenu() {
	clearTimeout(gMenuTimer);
}

function mouseoverPopmenu(obj,event,menuid) {
	if ((gCurrMenuID != null) && (gCurrMenuID != menuid)) {
		hidePopmenu();
	}
	showPopmenu(obj,event,menuid);
}

function showPopmenu(obj,event,menuid) {
	var rect = getPosition(obj,event);
	var layer = getLayer(menuid);
	// move position
	if (true) {
		layer.left = rect.x + "px";
		layer.top = rect.y + rect.h + "px";
	}
	// show layer
	if (is_ie) {
		layer.visibility = 'visible';
	} else if (is_w3c) {
		layer.visibility = 'visible';
	} else if (is_ns) {
		layer.visibility = 'show';
	}
	clearTimerPopmenu();
	gCurrMenuID = menuid;
}

function hidePopmenu() {
	var menuid = gCurrMenuID;
	var layer = getLayer(menuid);
	// hide layer
	if (is_ie) {
		layer.visibility = 'hidden';
	} else if (is_w3c) {
		layer.visibility = 'hidden';
	} else if (is_ns) {
		layer.visibility = 'hide';
	}
	clearTimerPopmenu();
	gCurrMenuID = null;
}

// function putPopmenuLayers
// gMenuItems = [menu_1,menu_2,...]
// menu_N = new cMenu(id,fore_color,back_color,width,item_list)
// item_list = [[url_1,title_1,label_1],[url_2,title_2,label_2],...]
// the url_N can be "#" for no link.

function putPopmenuLayers(pardir){
	if (pardir == undefined) { pardir = "."; }
	if (pardir == "") { pardir = "."; }

	for (var n = 0; n < gMenuItems.length; n++) {
		var menu = gMenuItems[n];
		if (menu == null) { break; }
		var id = menu.id;
		var fgcolor = menu.fgcolor;
		var bgcolor = menu.bgcolor;
		var width = menu.width;
		var itemList = menu.items;
		var widthAttr = ((width >= 0) ? ' width="'+width+'"' : "");
		document.writeln('<div id="'+id+'" style="position:absolute; top:0px; left:0px; visibility:hidden; z-index:255;" onmouseover="clearTimerPopmenu();" onmouseout="setTimerPopmenu();">');	/*  filter:Alpha(opacity=80); */
		document.writeln('<table border="0" cellspacing="1" cellpadding="1" bgcolor="'+bgcolor+'"'+widthAttr+'>');
		for (var k = 0; k < itemList.length; k++) {
			var item = itemList[k];
			if (item == null) { continue; }
			var url =  item[0];
			var title = item[1];
			var label = item[2];
			var theUrl = pardir + "/" + url;
			var theLabel = '<font color="'+fgcolor+'"><span class="popmenu">'+label+'</span></font>';
			document.writeln('<tr><td xCLASS="submenu" style="border:solid 1px darkgray; padding-left:4px; padding-right:4px;">');
			if (url == "#") {
				document.writeln(theLabel);
			} else {
				document.writeln('<a href="'+theUrl+'" title="'+title+'">'+theLabel+'</a>');
			}
			document.writeln('</td></tr>');
		}
		document.writeln('</table>');
		document.writeln('</div>');
	}
}

// end of javascript

