// emorycommon.js
// Common Functions used for web applications
// Extension function, based on Prototype js library.
document.getElementsByClassAndTag =
  function(className, tagName, parentElement) {
    var children = ($(parentElement) ||
       document.body).getElementsByTagName(tagName);
    return $A(children).inject([], function(elements, child) {
      if (child.className.match(new RegExp("(^|\\s)" +
          className + "(\\s|$)")))
      elements.push(child);
      return elements;
   });
} 

// global vars to control behavior of multiple calls to the template code.
menusInited = false;
roundingInited = false;

// Main call point for applications. 
// Takes care of  initializing the application menus, and 
// generating the default rounding effects on sections and tabbed menus.
function initAppTemplate() {
	initMenus(); 
	if(arguments.length == 0) {	
	  applyDefaultRoundingEffects();
	}
}

function applyDefaultRoundingEffects() {
	if(roundingInited != true) {
	   	Nifty("a.roundTop","transparent top");
	   	Nifty("div.roundAll","transparent both");   
	}
	roundingInited = true;
}


// determine the absolute left,top position of an element
// by walking up the stack of offsetParents and accumulating
// the offsetLeft and offsetTop values
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// if an iframe, with id "menuBacker" is found in the DOM
// place it, in z-index terms, above the page content. Then position 
// it directly behind the popup menu to be activated.  Then, set the 
// popup menu z-index to be 1 greater then the iframe.  This is needed
// when the browser is IE, version <= 6.
function addBackingToMenu(obj) {
	try {
		var backer = $('menuBacker');
		if(backer) {
			var lefttop=findPos(obj);
	
			if(Element.hasClassName(obj,"sectionMenuList")) {
				// section menus have 15px of padding, which 
				// must be compensated for here.
				backer.style.left = lefttop[0]-15+"px";
				// 
				backer.style.zIndex=1;				
			}
			if(Element.hasClassName(obj,"topBarMenuList")) {
			    backer.style.zIndex=10;
				backer.style.left = lefttop[0]+"px";
			}
			obj.style.zIndex=backer.style.zIndex+1;
			backer.style.top = lefttop[1]+"px";
			backer.style.width = obj.offsetWidth+"px";
			backer.style.height = obj.offsetHeight+"px";
			backer.style.position="absolute";
			backer.style.display="block";
		}
	} catch (e) {
		// fall through if iframe is not defined. 
	}
}

// if there is an iframe, with id "menuBacker" in the page
// set it's display to none and the z-index to 0.  This effectively
// turns it off.
function removeBackingFromMenu() {
	try {
		var backer = $('menuBacker');
		backer.style.display="none";
		backer.style.zIndex=0;
	} catch (e) {
		// fall through if iframe is not defined. 
	}
}
// The legacy entry point.  This will be called directly by v1-0-0 thru v1-0-4 
// template users.  Later versions should use the initAppTemplate() method, above.

// Applys onmouseover/out effect to menus
function initMenus() {
	if(menusInited != true) {
		populateGlobalMenu();
	
		var elements = document.getElementsByClassAndTag("dropDownMenu","li");
		elements.each( 
			function(value) {
				value.onmouseover=function() {
					this.className="activeDropDownMenu";
				}
				value.onmouseout=function() {
					this.className="dropDownMenu";
				}
			
		});

		var navArray = $("topnav","sidenav");
		navArray.each(
		function(nav) {
			try {
				var someNodeList = $(nav).getElementsByTagName('li');
				var nodes = $A(someNodeList);
				nodes.each(  
					function(node) {
						node.onclick=function() {
							onclick=setActiveMenuChoiceByMenuId(nav,this);
						}
				});
			} catch (e) {
			}		
		});
				
		elements = document.getElementsByClassAndTag("section","div","div");
		elements.each( 
			function(value) {
			  var options = value.getElementsByTagName('li');
			  options = $A(options);		
	
			  options.each(
			    function(li) {
			      if(li.id.indexOf('minimize') != -1) {
			        li.onclick=function() {
			        Element.hide(value.id+"Content");
			        }
			      }
			      if(li.id.indexOf('maximize') != -1 ) {
			        li.onclick=function() {
			        Element.show(value.id+"Content");
			        }
			      }
			    });
			});
		elements = document.getElementsByClassAndTag("topBarMenuItem","dl","topBarMenu");
		var elements2 = document.getElementsByClassAndTag("sectionMenuItem","dl","sectionMenuContainer");
		var e = $A(elements.concat(elements2));
		e.each( 
			function(value) {
			    var menuName = value.id;
				for (i=0; i<value.childNodes.length; i++) {
					node = value.childNodes[i];
					if (node.nodeName=="DT" ) {
						node.onmouseover=function() {
			  		  		this.style.fontWeight = 'normal';
			  		  		this.style.textDecoration = 'underline';
							switchBarMenuStates(menuName+'DD');	  		  		
						}
						node.onmouseout=function() {
			  		  		this.style.fontWeight = 'normal';
			  		  		this.style.textDecoration = 'none';
							switchBarMenuStates('');	  		  		
						}
						node.onclick=function() {
						  this.style.textDecoration = 'none';
						  this.style.fontWeight = 'bold';
						}
					}
					if (node.nodeName=="DD") {
					node.id=menuName+'DD';
						node.onmouseover=function() {
							switchBarMenuStates(menuName+'DD');							 		  		
						}
						node.onmouseout=function() {						  
						  switchBarMenuStates('');	  		  		
						}
					}
	 			}
	 	});		
	 	switchBarMenuStates('');
	 	menusInited = true;
 	}
} // end initMenus()

function populateGlobalMenu() {
	var elements = document.getElementsByClassName("globalMenu","topBarMenu");
	elements = $A(elements);
	elements.each(
		function(value) {
			Element.addClassName(value,"emoryLink");
			value.innerHTML = "<dt >Emory Resources</dt>"+"<dd class='topBarMenuList'>"+
						"<ul class='rightmenu'>"+
							"<li><a href='http://www.eushc.org'>Ehc Intranet Home</a></li>"+
							"<li><a href='http://www.emoryhealthcare.org'>Emory Healthcare </a></li>"+
						"</ul></dd>";
		});
}

function switchBarMenuStates(id) {
	removeBackingFromMenu();
	var elements = document.getElementsByClassAndTag("topBarMenuList","dd","topBarMenu");
	var elements2 = document.getElementsByClassAndTag("sectionMenuList","dd","sectionMenu*");
	var e = $A(elements.concat(elements2));
	e.each(
  		function(value) {
    		value.style.display='none';
  	});
  	var d = document.getElementById(id);
  	if(d) { 
  		d.style.display='block'; 
  		addBackingToMenu(d);
  	}
}

function help(what) {
}

function setActiveMenuChoiceByMenuId(menu, what) {
		var x = $(what.id);
		var someNodeList = $(menu).getElementsByTagName('li');
		var nodes = $A(someNodeList);
		nodes.each(function(node) {
       		  Element.removeClassName(node.id,"activelink");
		});
    	Element.addClassName(x.id,"activelink");
}

function setActiveMenuChoice(menu, what) {
	try {
		var someNodeList = $(menu).getElementsByTagName('li');
		var nodes = $A(someNodeList);
		nodes.each(function(node) {
	 		  Element.removeClassName(node.id,"activelink");
		});
    	Element.addClassName(what,"activelink");
	} catch (e) {
	}		
}

window.onload=initAppTemplate;