function myOnload() {
  var subMenuId = findDependentSubMenu();
  fixTopMenuSelectionStatus(subMenuId);
  fixSubMenuSelectionStatus(subMenuId);
}

// Find a top menu reference in the left columns' UL elements with
// class names "menu". If such an ID is found, return it.
// Inside the left column, if one of the menues shown has an ID of the
// form "dep_SOMETHING", whe take the "SOMETHING" to fix the top menu
// selection status.
function findDependentSubMenu() {
   var subMenuId = null;
   var uls = $$('div#user3 ul.menu');
   for(var i=0;i<uls.length;i++) {
   	if (uls[i].id.startsWith('dep_')) {
   		subMenuId = uls[i].id;
   		break;
   	}
   };
 	return subMenuId;
}

// If a reference is found, we navigate to the top memu: it has a container
// ID named 'mainlevel-nav'. We use one of the extension methods provided
// by prototype.js (the descendants()-method), look for <a> elements, take
// its interior and compare it case insensitively with the reference. If a
// match is found, give this element the ID "active_menu-nav". All other
// anchors are given some ID generated by a dumb counter. The reference may
// have a _\d*_ (this is underscore plus digits plus underscore) at the end,
// which will be ignored for comparison. The reference may be given as a
// list of the form
//     dep_someName1_XXdep_someName2
// and so on ('_XX' is the separator), in which case several top menu 
// item names are checked against
function fixTopMenuSelectionStatus(subMenuId) {
 	if (subMenuId) {
 	   var splits = new Array();
 	   subMenuId.split('_XX').each(function(s) {
 	       splits.push(s.replace(/_\d*_$/g,'').toLowerCase().substring(4)); 
 	       // get rid of the 'dep_' at the beginning, lowerize, and 
 	       // get rid of the '_\d*_' at the end
 	   });
 		var navList = $$('ul#mainlevel-nav a'); // get the <a> of the top menu
 		var idCnt = 1;
 		navList.each(function(elem) {
 		  var spanElem = elem.childNodes[0];
 		  var textElement = spanElem.childNodes[0];
 		  var linkTitle = textElement.data.replace(/ /g,'_').replace(/[^A-Za-z0-9_]/g,'');
 		  if (splits.indexOf(linkTitle.toLowerCase()) >= 0) {
 		  	  elem.id = 'active_menu-nav';
 		  }else{
 		     elem.id = 'menu-nav-' + idCnt;
 		  }
 		  idCnt++;
 	   });
   }   
}

// Mark the first entry in the dependent menue by adding the class 
// 'simulate_current'. If the auxiliary has an active entry, do nothing
function fixSubMenuSelectionStatus(subMenuId) {
	if (subMenuId) {
		var auxHasActive = false; //$$('div.module_auxmenu a#active_menu').length > 0;
		var subMenuLi = $$('#' + subMenuId + ' li');
		var firstLi = null;
		var hasCurrent = false;
		for(var i=0;i<subMenuLi.length;i++) {
 		   var elem = subMenuLi[i];
			if (!firstLi) firstLi = elem;
			if (elem.id == 'current') hasCurrent = true;
		};
		if (!hasCurrent && !auxHasActive) {
			firstLi.className = firstLi.className + ' simulate_current';
			// Does not work in IE: .addClassName('simulate_current');
		}
 	}
}

function showPageGallery() {
	var ch0 = $$('table.contentpaneopen td.contentheading');
        if (ch0.length > 0) {
	  var ch = ch0[0];
          ch.parentNode.parentNode.parentNode.style.display = 'none';
        }
	var container = $('psgGallery').parentNode;
	for(var j=0;j<5;j++) {
	   var nodes = container.childNodes;
		for (var i=0;i<nodes.length;i++) {
			var n = nodes[i];
			if (n.nodeName.toUpperCase() == 'DIV' && n.id == 'psgGallery') {
				n.style.display = 'block';
			}else{
				container.removeChild(n);
			}
		}
	}
	return false;
}

function addResolutionDependentCSS(cssDir) {
  // Add extra stylesheet for small resolutions
  if (screen.height < 768) {
      document.write('<link rel="stylesheet" type="text/css" href="' + cssDir + '/lt768.css">');
  }
}

