
Object.extend(Event, {
	_domReady : function() {
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		if (this._timer) clearInterval(this._timer);
		this._readyCallbacks.each(function(f) { f() });
		this._readyCallbacks = null;
	},

	onDOMReady : function(f) {
		if (!this._readyCallbacks) {
			var domReady = this._domReady.bind(this);

			if (document.addEventListener) document.addEventListener("DOMContentLoaded", domReady, false);

			/*@cc_on @*/
			/*@if (@_win32)
				document.write("<script id=__ie_onload defer='true' src='javascript:void(0)'><\/script>");
				document.getElementById("__ie_onload").onreadystatechange = function() { if (this.readyState == "complete") domReady(); };
			/*@end @*/

			if (/WebKit/i.test(navigator.userAgent)) this._timer = setInterval(function() {if (/loaded|complete/.test(document.readyState)) domReady(); }, 10);

			Event.observe(window, 'load', domReady);
			Event._readyCallbacks =  [];
		}

		Event._readyCallbacks.push(f);
	}
});


/* ======================================================================== */


var BMJ = function(){

	return {

		/* -- BEGIN: DHTMLMenu ------------------------------------------------ */
		DHTMLMenu : function() {
			
			var CONFIG = {
				menu_id : "MainNav", // the DOM ID of the menu container
				hover_class : "Hover", // the class to give the top-level LI to "activate" the menu
				menu_time : 500 // time to keep the menus on after mouseout; in ms
			};

			return {
				menu_timeout : null, // the JS timeout ID for hiding the menu
				last_menu_on : null, // the DOM object of the waiting to close

				// initialize the menus. usually called on page load / DOM ready
				init : function() {
					// bail out if the menu doesn't exist on this page
					if (!$(CONFIG["menu_id"])) return;

					// get all the top-level LIs in the menu
					var menu_items = $(CONFIG["menu_id"]).cleanWhitespace().childNodes;

					// iterate over them and add event handlers
					$A(menu_items).each(function(item){
						Element.cleanWhitespace(item);

						// the mouseover
						Event.observe(item, "mouseover", function(e) {
							var el = Event.element(e);

							// stop the menu from closing (this gets called a lot)
							clearTimeout(this.menu_timeout);

							// show the menu
							this.showMenu(item);


						}.bind(this), false); // END: mouseover

						// the mouseout
						Event.observe(item, "mouseout", function(e) {
							var el = Event.element(e);

							// store the last item on
							this.last_menu_on = item;

							var turnOff = function() {
								this.hideMenu(item);
								this.last_menu_on = null;
							}.bind(this);

							// only "close" the menu in a little bit if we're over a menu with submenus, otherwise, close it right now
							if ( (el.parentNode.id && (el.parentNode.childNodes.length > 1)) || (el.parentNode.childNodes.length > 1) ) {
								this.menu_timeout = setTimeout(turnOff,	CONFIG["menu_time"]);

							} else {
								turnOff();
							}

						}.bind(this), false); // END: mouseout

					}.bind(this)); // END: each()
				}, // END: init()

				// shows the menu
				showMenu : function(item) {
					// hide the last menu shown
					if (this.last_menu_on != null) this.hideMenu(this.last_menu_on);

					// adding the "hover" class turns on the menu
					$(item.id).addClassName(CONFIG['hover_class']);
				}, // END: showMenu()

				// hide the menu
				hideMenu : function(item) {
					if (item == null) return;

					// removing the "hover" class turns off the menu
					$(item.id).removeClassName(CONFIG['hover_class']);
				} // END: hideMenu()

			} // END: return
		}()
		/* -------------------------------------------------- END: DHTMLMenu -- */

	} // END: return
}(); // END: HBSP namespace


Event.onDOMReady(function() {
	// simple mouseovers/outs
	BMJ.DHTMLMenu.init();
});
