﻿/*extern $, Dom, Event */
/*global PHY */
/*members addListener, childNodes, data, doClick_Menu, 
    getAncestorByTagName, getStyle, getTarget, init, length, menu, nodeName, 
    onDOMReady, setStyle
*/



PHY = {

	data: {
		menu: 'hospitalListPDF' // The ID of the root-level PDF menu UL item.
	},

	init: function () {

		Event.addListener(this.data.menu, 'click', this.doClick_Menu, this, true);

	},

	/** Toggle the state of a menu selection in the left-hand PDF menu.
	  * The onClick is attached to the root-level UL.
	  * This function locates the SPAN that was clicked, locates the parent
	  * LI item, locates the first UL child, and toggles the "display" style
	  * between "none" and "block."
	  *
	  * This function aborts if the click was not on a SPAN, if it could not
	  * find the parent LI, and if it could not find a UL child.
	  *
	  * @param {event} e
	  */
	doClick_Menu: function (e) {

		// Locate SPAN
		var target = Event.getTarget(e),
		x, li, ul;

		if (target.nodeName !== 'SPAN') {
			return;
		}

		// Locate LI
		li = Dom.getAncestorByTagName(target, 'LI');

		if (li === null) {
			return;
		}

		// Locate UL
		ul = null;
		for (x = 0; x < li.childNodes.length; x++) {
			if (li.childNodes[x].nodeName === 'UL') {
				ul = li.childNodes[x];
				break;
			}
		}

		if (ul === null) {
			return;
		}

		// Toggle display style
		if (Dom.getStyle(ul, 'display') === 'none') {
			Dom.setStyle(ul, 'display', 'block');
		} else {
			Dom.setStyle(ul, 'display', 'none');
		}


	}

};

Event.onDOMReady(PHY.init, PHY, true);

