/* global.js version 1.2 */

/***********************************
window.onload = function() - all 
scripting activity that needs to be 
called onload should be place in this 
function call.
***********************************/

window.onload = function(){	

	parentHighlight("topNav");
	parentHighlight("textTopNav");
	parentHighlight("sideNav");

	MenuMemory("topNav");
	MenuMemory("tabNav");
	MenuMemory("tabNavMB");
	MenuMemory("textTopNav");

	// if you want to use the submenu system the second value must be true
	MenuMemory("sideNav", true);

	
	if (document.getElementById("siteSpecific").getElementsByTagName("UL").length > 0) {	
		MenuMemory("siteSpecific");
	}

	initializePopup();
}

	
/*
initializePopup() - needs to be run onload of the page. By rolling 
through the links onload we maintain the ability to open links 
into a new window while still meeting accessibility standards.
*/
function initializePopup() {
	var anchors = document.getElementsByTagName('a');
	for (var i=0;i<anchors.length;i++) {
		if(anchors.item(i).className.match(/newWindow|pdfWindow|external/)){
			anchors.item(i).onkeypress = openNewWindow;
			anchors.item(i).onclick = openNewWindow;
		}

		if (anchors.item(i).className.indexOf('newWindow') != -1) {
			anchors.item(i).setAttribute("title", "Pop-up Window");
		} else if (anchors.item(i).className.indexOf('pdfWindow') != -1) {
			anchors.item(i).setAttribute("title", "PDF file");
		} else if (anchors.item(i).className.indexOf('external') != -1) {
			anchors.item(i).setAttribute("title", "External link");
		}
	}
}

/*
openNewWindow() - opens, or if already open reloads, a new 
window with the height and width set in the links class 
attribute. 
*/
var popUp; // This variable is global to allow the "popup" window to be closed and reopened
function openNewWindow() {

	var width = getDimension(this.className.match(/w\d{1,4}/) + "");
  var height = getDimension(this.className.match(/h\d{1,4}/) + "");	

  var features = 'scrollbars, resizable,';
	features += 'height=' +height+ ", ";
	features += 'width=' +width;

	if(popUp) {
		//if the popup window did exist but was closed we will get an error.
		try {
			popUp.resizeTo(width,height);
		}
		catch(err) {
			//we need to tell this page the window was closed.
			popUp.close();
		}
	}
  
	popUp = window.open(this.href, '_new', features);
	popUp.focus();

  return false;
}

/*
getDimension() - Extracts the integer height/width from the links class attribute (a string).
attribValue - is the string value from which we want to extract the height/width
*/
function getDimension(attribValue) {
	var defaultValue = 300; //if no height/width is found this is what will be used
	var minDem = 0; //minimum height/width we will allow before we use the defaultValue

	attribValue = attribValue.replace(/w{1}|h{1}/, '');
	attribValue = parseInt(attribValue);
	
  if (attribValue > minDem) {    
    return attribValue;
  } 
	else {
    return defaultValue;
  }
}



