/**************************************************************************************************/
// Specialized Functionality for AeroScholars.com
// aeroscholars.js
/**************************************************************************************************/
// Should be included at the end of the HTML page
/**************************************************************************************************/




/**************************************************************************************************/
// Custom Functions
/**************************************************************************************************/

// Manage Pull-down menus
function showMenu(activator) {
	//hide currently displayed pull-down menu and register this menu ID so it can be hidden later
	if(accessDiv) accessDiv.className = 'menuHidden';

  //choose which menu to display based on user login
  var menuDivID = (ALMS && ALMS.authorized ? 'uacUserMenu' : 'uacAnonymousMenu');
  	

  //register the new menu
	var elm;
	if(elm = document.getElementById(menuDivID)) {
		accessDiv = elm;
		//position the menu below the activator
		elm.style.top = activator.offsetTop + activator.offsetHeight + 'px';
		elm.style.left = activator.offsetLeft + 'px';
		
		//make this menu visible
		elm.className = 'menuVisible';
		
		//readjust horizontal position if the pull-down menu is off the screen
		if(elm.offsetLeft + elm.offsetWidth + 10 > document.body.clientWidth)
			elm.style.left = (document.body.clientWidth - elm.offsetWidth - 10) + 'px';
	}
}
function queueHideMenu(menuDivID,activator) {
	window.setTimeout(hideMenu,1000);
}

//FUNCTION: get_parent_with_id - returns the object reference to the parent element with passed id
function get_parent_with_id(obj,id_in)
{
	return (obj.id==id_in ? obj : (obj.nodeName=='HTML' ? false : (get_parent_with_id(obj.parentNode,id_in))));
}

//FUNCTION: documentMouseDown() - the global event handler for mousedown events on the document in general
function documentMouseDown(event)
{
	//get a ref to the element that was clicked
	var targetElm = false;
	if(event)
	{//browser has passed a standardized event object
		//alert(event.target+"\n"+event.target.tagName+"\n"+event.target.parentNode);
		targetElm = (event.target.tagName ? event.target : event.target.parentNode);
	}
	else
	{//must be IE, cause there is no standard event, use IE's global event object
		//alert(window.event.srcElement);
		targetElm = window.event.srcElement;
	}

	//see if the clicked element is part of the drop-down menu, if not, hide the drop-down menu
	if(targetElm.id != 'accessButt' && accessDiv && !get_parent_with_id(targetElm,'uacAnonymousMenu') && !get_parent_with_id(targetElm,'uacUserMenu'))
		accessDiv.className = 'menuHidden';
}




/**************************************************************************************************/
// Commands to run after the rest of the HTML elements are rendered
/**************************************************************************************************/

// Scan page for expiringDate elements
var scanElm = document.getElementsByTagName('DIV');
var dateElm = null;
for (i = 0; i < scanElm.length; i++) {
	if(scanElm[i].className == 'expiringDate' && (dateElm = cc.getChildNode(scanElm[i],'B'))) {
		var dateCompare = new Date(dateElm.innerHTML);
		var dateNow = new Date();
		if(dateCompare < dateNow) scanElm[i].className = 'expiredDate';
	}
}

// Set a global reference for the pull-down menus to null (none visible)
var accessDiv = null;

//Set up DOM level 2 global document level mousedown event handler
if(document.addEventListener)
{//this browser supports standardized event handling
	document.addEventListener('mousedown',documentMouseDown,true);
}
else
{//this must be a retarded IE browser cause it doesn't support standard events
	document.onmousedown = documentMouseDown;
}

