

var bv_last_active_menu = null;
var bv_default_menu = null;

function setDropMenuDefault(Id) {
	bv_default_menu = Id;
// this function may be called before the page finishes loading, so stall the result...
	setTimeout('bv_showMenu(document.getElementById("'+Id+'"));',1000);
}



function bv_showMenu(M) { // alert('showing menu '+ M.id);
//document.getElementById('debug').innerHTML+=" | showing "+M.id;
	if (M && M.timer) clearTimeout(M.timer);
	if (bv_last_active_menu && bv_last_active_menu != M.id)
		bv_hideMenuNow(bv_last_active_menu, false);
	bv_last_active_menu = M.id;
	M.style.visibility = "visible";
}

function bv_hideMenu(M) { // alert('hiding menu '+ M.id);
// slight delay to prevent simultaneous off-on triggers
	M.timer = setTimeout('bv_hideMenuNow("'+ M.id +'", true);',25);
}

function bv_hideMenuNow(Id, TimedFlag) { // alert('now hiding menu '+ Id);
	var M = document.getElementById(Id);
	M.style.visibility = "hidden";
	if (M && M.timer) clearTimeout(M.timer);
	bv_last_active_menu = null;
	if (TimedFlag && bv_default_menu) {
		bv_showMenu(document.getElementById(bv_default_menu));
	}
}

function bv_flipMenu(M) {
	if (M.style.visibility == "visible")
		bv_hideMenuNow(M.id, false);
	else bv_showMenu(M);
}




bv_PrepDropMenuTriggers = function(I,Id,clickable) { // alert('prepping drop menu triggers for '+ Id);
	var M = document.getElementById(Id);
	if (M.bv_AfterMenuMouseover) return;
// store existing event handlers so you don't override them
	I.bv_AfterMenuMouseover = (I.onmouseover) ? I.onmouseover : function() {};
	I.bv_AfterMenuMouseout  = (I.onmouseout)  ? I.onmouseout  : function() {};
	I.bv_AfterMenuClick     = (I.onclick)     ? I.onclick     : function() {};
// attach the menu functions to the image events
	I.onmouseover = function () { document.getElementById(Id).show(); I.bv_AfterMenuMouseover(); };
	I.onmouseout  = function () { document.getElementById(Id).hide(); I.bv_AfterMenuMouseout();  };
	if (clickable)
	   I.onclick  = function () { document.getElementById(Id).flip(); I.bv_AfterMenuClick();     };
}

bv_PrepDropMenu = function(Id,clickable) { // alert('prepping drop menu '+ Id);
	var M = document.getElementById(Id);
	if (M.bv_AfterMenuMouseover) return;
	M.timer = false;
	M.show = function () { if (window.bv_showMenu) bv_showMenu(M); }
	M.hide = function () { if (window.bv_hideMenu) bv_hideMenu(M); }
	M.flip = function () { if (window.bv_flipMenu) bv_flipMenu(M); }
// store existing event handlers so you don't override them
	M.bv_AfterMenuMouseover = (M.onmouseover) ? M.onmouseover : function() {};
	M.bv_AfterMenuMouseout  = (M.onmouseout)  ? M.onmouseout  : function() {};
	M.bv_AfterMenuClick     = (M.onclick)     ? M.onclick     : function() {};
// attach the menu functions to the div events
	M.onmouseover = function () { M.show(); M.bv_AfterMenuMouseover(); };
	M.onmouseout  = function () { M.hide(); M.bv_AfterMenuMouseout();  };
	if (clickable)
	   M.onclick  = function () { M.flip(); M.bv_AfterMenuClick();     };
}




bv_enableDropMenu = function() { // alert('enabling drop menus');
	var Imgs = document.getElementsByTagName('img');
	for (var xx=0; xx<Imgs.length; xx++) {
		if ( // if it has a className that starts with bvEnable and has ":menu-" in it:
			Imgs[xx].className &&
			Imgs[xx].className.indexOf('bvEnable') == 0 &&
			/:menu-\w+/.test(Imgs[xx].className)
		) { // then enable the menu triggered by that image
			var parts = /:menu-(\w+)-?(\w+)?/.exec(Imgs[xx].className);
			var clickIt = (parts[2] == "click") ? true : false;
			bv_PrepDropMenuTriggers(Imgs[xx], parts[1], clickIt);
			bv_PrepDropMenu(parts[1], clickIt);
		}
	}
}


/*
if (!window.ToLoad) window.ToLoad = new Array();

window.ToLoad[window.ToLoad.length] = bv_enableDropMenu;
window.onload = function() {
	for (var xx = window.ToLoad.length -1; xx >= 0; xx--) {
		window.ToLoad[xx]();
	}
}
*/
bv_addListener(window, 'docload', bv_enableDropMenu);


