/*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);

	},

	/* 
	 * If there are more than one addresses, additional addresses are hidden.
	 * This Toggles the css rule that hides the additional addresses, and updates
	 * the link text accordingly.
	*/
	toggleAdditionalLocations: function() {
	
		var i, j, ruleSet, target;
	
		for (i = 0; i < document.styleSheets.length; i++){
				
			if (document.styleSheets[i].cssRules) 
				{ruleSet = document.styleSheets[i].cssRules;}
			else	
				{ruleSet = document.styleSheets[i].rules;}
			
			for (j = 0; j < ruleSet.length; j++){
					if (!ruleSet[j].selectorText) {break;}
					if ( ruleSet[j].selectorText.toLowerCase()==".hiddenaddresses"){
						target = ruleSet[j];
						break;
				}
			}
			if (target) {break;}
		}
		
		if (!target) {alert ("Missing CSS rule: .hiddenaddresses"); return;}
		
		if (target.style.cssText.indexOf("block") != -1){
			target.style.cssText = "display: none";
			document.getElementById('ToggleText').innerHTML = ' Show Additional Locations';			
			document.getElementById('TogglePic').src = '/images/TRHMG/plus.png';
		} else {
			target.style.cssText = "display: block";
			document.getElementById('ToggleText').innerHTML = ' Hide Additional Locations';
			document.getElementById('TogglePic').src = '/images/TRHMG/minus.png';
		}
		return;
	},

	/** 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');
		}


	}

};

function show_tab(div_id) {
	var general   = document.getElementById('content_general'),
		locations = document.getElementById('content_locations'),
		video     = document.getElementById('content_media');

	if (!div_id) {
		return;
	}

	if (general) {
		general.className = "content_hidden";
	}

	if (locations) {
		locations.className = "content_hidden";
	}

	if (video) {
		video.className = "content_hidden";
	}

	if (general && document.getElementById('tab_content_general')) {
		document.getElementById('tab_content_general').className =
			"profiletab_default";
	}

	if (locations && document.getElementById('tab_content_locations')) {
		document.getElementById('tab_content_locations').className =
			"profiletab_default";
	}

	if (video && document.getElementById('tab_content_media')) {
		document.getElementById('tab_content_media').className =
			"profiletab_default";
	}

	document.getElementById('tab_' + div_id).className = "profiletab_selected";

	if (div_id === "content_general" && general) {
		general.className = "content_active";
	}

	if (div_id === "content_locations" && locations) {
		locations.className = "content_active";
	}

	if (div_id === "content_media" && video) {
		video.className = "content_active";
		//video.style.display = "block";
	}
}

Event.onDOMReady(PHY.init, PHY, true);


