/************** global variables/constants **********************************/

var explorer = (navigator.appName != "Netscape" && parseInt(navigator.appVersion) >= 4);
var page_width = 1600;
var page_margin = 6;
var mouse_x = 0, mouse_y = 0;

/************** window opener ***********************************************/

function popup(url,name,width,height) {
	var features = [
		['toolbar',		0],
		['directories',	0],
		['menubar',		0],
		['status',		0],
		['resizable',	1],
		['location',	0],
		['scrollbars',	0],
		['copyhistory',	0]
		];
	for (var nr = 4; nr < popup.arguments.length; nr++) {
		var param = popup.arguments[nr].split('=');
		for (var i = 0; i < features.length; i++)
			if (features[i][0] == param[0]) {
				features[i][1] = param[1];
				break;
				}
		if (i == features.length)
			features[i] = param;
		}
	var feature_string = 'width=' + width + ',height=' + height;
	for (var i = 0; i < features.length; i++)
		feature_string += ',' + features[i][0] + '=' + features[i][1];
	window.open(url,name,feature_string);
	}

/************** onmouseover/onmouseout event handlers ***********************/

var setimg_id=null, setimg_src=null;

function restore_img() {
	if (setimg_id)
		document[setimg_id].src = setimg_src;
	setimg_id = null;
	}

function set_img(id,src) {
	if (setimg_id && setimg_id != id)
		restore_img();
	setimg_id		= id;
	setimg_src		= document[id].src;
	document[id].src= src;
	}

/************** CSH *********************************************************/

var CSH_id=0, CSH_text, CSH_event, CSH_event_x, CSH_event_y;

function CSH_popup(id) {
	if (CSH_id != id) return false;

	if (!(document.all || (document.layers && document.CSH))) {
		window.status=CSH_text;
		return true;
		}

	var contents='<table border=0><tr><td bgcolor="#FFFF80">' + CSH_text + '</td></tr></table>';

	if (explorer)  {	// IE4+
		document.all['CSH'].style.left=CSH_event_x + 5;
		document.all['CSH'].style.top=CSH_event_y + 5;
		document.all['CSH'].innerHTML=contents;
		document.all['CSH'].style.visibility="visible";
		}
	else {				// NN4+
		document.CSH.document.open();
		document.CSH.document.write(contents);
		document.CSH.document.close();

		document.CSH.left=CSH_event.pageX + 5;
		document.CSH.top =CSH_event.pageY + 5;
		document.CSH.visibility="visible";
		}

	return false;
	}

function CSH_off() 
{ 
	CSH_id++;

	if (!(document.all || (document.layers && document.CSH))) {
		window.status='';
		return true;
		}

	if (explorer)
		document.all['CSH'].style.visibility="hidden";	// IE4+
	else
		document.CSH.visibility="hidden";	// NN4+
	return false;
	}

function CSH_on(text,event_obj)
{
	CSH_text=text;
	CSH_event=event_obj;
	if (explorer) {
		CSH_event_x=event_obj.clientX + document.body.scrollLeft;
		CSH_event_y=event_obj.clientY + document.body.scrollTop;
		}
	CSH_id++;

	setTimeout("CSH_popup(" + CSH_id + ")",1000);
	}

/************** menu template ***********************************************/

var menufwk_top =
  '<table cellpadding=0 cellspacing=0 border=0 bgcolor=white>' +
  '  <tr>' +
  '    <td bgcolor="#4F607F" rowspan=3><tpix height=1 width=1></td>' +
  '    <td bgcolor="#4F607F"><tpix height=1 width=1></td>' +
  '    <td bgcolor="#4F607F" rowspan=3><tpix height=1 width=1></td>' +
  '    <td><tpix height=1 width=1></td>' +
  '  </tr>' +
  '  <tr>' +
  '    <td>' +
  '      <table cellpadding=0 cellspacing=0 border=0>' +
  '        <tr>' +
  '          <td><tpix height=2 width=2></td>' +
  '          <td></td>' +
  '          <td><tpix width=2></td>' +
  '        </tr>' +
  '        <tr>' +
  '          <td></td>' +
  '          <td>';

var menufwk_bot =
  '          </td>' +
  '          <td></td>' +
  '        </tr>' +
  '        <tr>' +
  '          <td><tpix height=2></td>' +
  '          <td></td>' +
  '          <td></td>' +
  '        </tr>' +
  '      </table>' +
  '    </td>' +
  '    <td bgcolor="#3F506F"><tpix height=1 width=1></td>' +
  '  </tr>' +
  '  <tr>' +
  '    <td bgcolor="#C0C0C0"><tpix height=1 width=1></td>' +
  '    <td bgcolor="#3F506F" rowspan=2><tpix height=1 width=1></td>' +
  '  </tr>' +
  '  <tr>' +
  '    <td><tpix height=2 width=1></td>' +
  '    <td bgcolor="#3F506F" colspan=2><tpix height=2 width=1></td>' +
  '  </tr>' +
  '</table>';

/************** menus *******************************************************/

var openmenu = null, timerID = null;

function menu(entries) {
	// assign default arguments: x, y, is_relative, mask
	var argv = menu.arguments;
	var argc = argv.length;
	var x = (argc > 1) ? argv[1] : 0;
	var y = (argc > 2) ? argv[2] : 0;
	var is_relative = (argc > 3) ? argv[3] : false;
	var mask = (argc > 4) ? argv[4] : '';
	// assemble menuentries string
	var menuentries = '';
	for (var i = 0; entries && i < entries.length; i++) {
		var nameurl = entries[i].split('@');
		if (nameurl[0] == 'autoexec') {
			window.location = nameurl[1];
			return;
			}
		if (mask.charAt(i) != '0')
			menuentries += '<a href="' + nameurl[1] + '" onclick="menu(null)">' + nameurl[0] + '</a><br>';
		  else
			menuentries += nameurl[0].fontcolor('a0a0a0') + '<br>';
		}
	// apply parameters to menuentires
	for (var i = 5; i < argc; i++) {
		var param = argv[i].split('=');
		re = new RegExp(param[0], 'g')
		menuentries = menuentries.replace(re, param[1]);
		}
	// calculate correct position
	if (is_relative) {
		x += mouse_x;
		y += mouse_y;
		}
	  else {
		var window_width = explorer ? document.body.offsetWidth : window.innerWidth;
		if (window_width > page_width)
			x += (window_width-page_width)/2-10;
		x += page_margin;
		}
	// add menu framework
	var menucontent = menufwk_top + menuentries.replace(/`/g, "'") + menufwk_bot;
	menucontent = menucontent.replace(/tpix/g, 'img src="' + tpix_src +'" alt=""');
	// display menu
	var newmenu = menuentries;
	if (explorer)  {	// IE4+
		if (entries && newmenu != openmenu) {
			document.all['menulayer'].innerHTML = menucontent;
			document.all['menulayer'].style.posLeft = x;
			document.all['menulayer'].style.posTop = y;
			document.all['menulayer'].style.visibility = "visible";
			openmenu = newmenu;
			}
		  else {
			document.all['menulayer'].style.visibility = "hidden";
			openmenu = null;
			}
		}
	  else {	// NN4+
		if (entries && newmenu != openmenu) {
			document.menulayer.document.open();
			document.menulayer.document.write(menucontent);
			document.menulayer.document.close();
			document.menulayer.left = x;
			document.menulayer.top = y;
			document.menulayer.visibility = "show";
			openmenu = newmenu;
			}
		  else {
			document.menulayer.visibility = "hide";
			openmenu = null;
			}
		}
	}

function delayed_closemenu(activate) {
	// if activate==true activate timer that closes open menu in 300ms
	// if activate==false deactivate that timer
	if (activate)
		timerID = setTimeout(menu,300,null);
	  else {
		if (timerID)
			clearTimeout(timerID);
		timerID = null;
		}
	}

/************** event handlers **********************************************/

// mousedown event handler
function mousedown_handler(e) {
	if (explorer) {
		mouse_x = event.clientX + document.body.scrollLeft;
		mouse_y = event.clientY + document.body.scrollTop;
		}
	  else {
		mouse_x = e.pageX;
		mouse_y = e.pageY;
		}
	}

// setup mousedown event handler
if (!explorer)
	document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = mousedown_handler;

// onresize event handler
function do_resize() {
	if (explorer)
		menu(null,0);
	  else
		if (document.x.do_resize.algw != window.innerWidth || document.x.do_resize.algh != window.innerHeight)
			document.location = document.location;
	}

// setup onresize event handler for netscape
if (!explorer)  {
	if (typeof document.x == 'undefined')
		document.x = new Object;
	if (typeof document.x.x_scaleFont == 'undefined') {
		document.x.do_resize = new Object;			
		document.x.do_resize.algw = window.innerWidth;
		document.x.do_resize.algh = window.innerHeight;
		}				
	window.onresize = do_resize();
	}

