// ## Joe's scripts

var menuLife=2000;
var menuTimer;
var menuTrip = false; // activates shide code only when false !! NOT USED

// the following shtuff is the result of my sweet sweaty forehead and hand coded with my fingerprints

	function JBmenuShow() {
		//show/hide absolute elements:
		//with one argument toggle it on and off
		//with many arguments, display the first one, hide the others
		if (arguments.length == 1) { JBshideVisibility(arguments[0], 'toggle') }
		else {
			if (arguments[0]) {JBshideVisibility(arguments[0], 'visible')};
			for (var i = 1; i < arguments.length; i++) {
				n = arguments[i]
				JBshideVisibility(n, 'hidden')
			}
		}
	}

	function JBshideVisibility(elemID, state) {
		//show / hide absolute divs
		//JBresetTimer(menuTimer);
		var LmNt = false;
		if (document.getElementById && document.getElementById(elemID))
			{ LmNt = document.getElementById(elemID) }
		else 
			{ if (document.all && document.all[elemID]) 
				{ LmNt = document.all[elemID] }
			}
		if (LmNt) {
			if (state == 'show') {
				  LmNt.style.visibility = 'visible'} else
			if (state == 'hide') {
				  LmNt.style.visibility = 'hidden' } else
			if (state == 'toggle') {
				 if (LmNt.style.visibility != 'visible') { LmNt.style.visibility = 'visible'}
				 else { LmNt.style.visibility = 'hidden' }
			} else
				if (LmNt.style.visibility != state) { LmNt.style.visibility = state }
			}
	}
	

function JBgetMenuId(y) {
	var x;
	if (!y.offsetParent) {return y }
	else {return y.offsetParent.id}
}

function JBdisplayBlock() {
	//show/hide block elements:
	//with one argument toggle it on and off
	//with many arguments, display the first one, hide the others
	if (arguments.length == 1) { JBshideVisibility(arguments[0], 'toggle') }
	else {
		JBshideVisibility(arguments[0], 'visible');
		if ( (arguments[1] == 'show') || arguments[1] == 'hide') {JBshideVisibility(arguments[0], arguments[1])};
		for (var i = 1; i < arguments.length; i++) {
			JBshideVisibility(arguments[i], 'hidden')
		}
	}
}

function JBshideDisplay(elemID, state) { // create / destroy display blocks
	if (document.all) { LmNt = document.all[elemID] }
	if (document.getElementById) { LmNt = document.getElementById(elemID) }
	if (state == 'toggle') {
		 if (LmNt.style.display != 'block') {LmNt.style.display = 'block'}
		 else { LmNt.style.display = 'none' }
	} else
		if (LmNt.style.display != state) { LmNt.style.display = state } 
}

function JBgetElementId( y ) {
	var x;
	// travels up the DOM tree, stops @ first declared <element id> encountered
	if (y.parentNode) {
		for (x = y; x.id == ""; x = x.parentNode);
		return x.id;
	}
	else {
		return y; //returning attribute assuming it is the actual id!
	}
 }
 
 function JBsetMenuTimer(x) {
 	//packages JBsetTimer to reduce document code
 	//attribute tested to check if it is an element or a string
	JBresetTimer(menuTimer); //reset last timer from global var
	menuTimer=JBsetTimer(JBgetMenuId(x), 'hidden', menuLife);
}

function JBsetTimer(x,v,t) { //set delay to change visibility of element with <id> x
	var JBfn;
	JBfn = "JBshideVisibility('"+x+"', '"+v+"')";
	JBTimer = setTimeout(JBfn,t);
	return JBTimer;
}

function JBresetTimer(widgetTimer) { 
	if (widgetTimer) {
		window.clearTimeout(widgetTimer)
	}
 }
 
//   experimental menu objects
// #############################

function getDepth (href) {
	var y = document.location.href.split(href);
	var z = '';
	var x = y[1].split("/").length-1;
	if (x > 0) {
		for (i = 1; i < x; i++) {z += "../"}
	}
	return z;
}


function menuItem(Title, Link, Desc) {
	this.title = Title;
	this.href = Link;
	this.desc = Desc;
}

function writeMenu() {
	var menuId = this.id;
	var x = '<div id="'+ this.id +'">\n';
	x += '<p class="secHead"><a href="#" onclick="JBmenuShow(\'\',\'' + menuId + '\')" title="'+ this.desc +'">&nbsp;[x]&nbsp;</a>' + this.title + '</p>\n';
	x += '<div class="box">\n';
	if (this.items.length > 0) {
		for (var i = 0; i < this.items.length; i++) 
			{ x += '	<div class="item"><a href="' + this.jump + this.items[i].href + '" onmouseover="JBresetTimer(menuTimer)" onmouseout="JBsetMenuTimer(\''+ menuId +'\')">' + this.items[i].title + '</a></div>\n'}
	}
	x += '</div>\n';
	x += '</div>\n';
	return x;
}

function addItem() {
	for (i=0; i < arguments.length; i++) {
		(arguments[i].title != 'undefined') ? (this.items[this.items.length] = arguments[i]) : ( this.items[this.items.length] = new menuItem('Mismatch!','#','This menu entry is not well formatted: use {menuItem}') )
	}
}

function JBmenu(menuId, menuTitle, menuPosX, menuPosY, pivot) {
	this.id = menuId;
	this.title = menuTitle;
	this.posX = menuPosX;
	this.posY = menuPosY;
	this.pivot = pivot;
	this.jump = getDepth(this.pivot);
	this.item = menuItem;
	this.items = new Array();
	this.code = writeMenu;
	this.addItem = addItem;
}