// TRIGGER EVENTS ON-LOAD
// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
// removeEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener) {
		obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

// OPEN A PAGE IN A NEW WINDOW
// Create the new window
function openInNewWindow(e) {	
	// REM GML, closes ES Nav when used - and too slow... window.location.reload(); // Added 2/11/08 GML - refreshes main page so that "visited" link works in IE
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {		
		var newWindow = window.open(this.getAttribute('href'), '', 'width=780, height=500, scrollbars=yes, resizable=yes, toolbar=yes, location=yes, directories=no, menubar=yes, copyhistory=no');
		if (newWindow) {
			if (newWindow.focus) {
			newWindow.focus();
			}
		return false;
		}
	return true;
	}		
}

// OPEN VIDEO (POP-UP) WINDOW
// Create the new window
function openVideoWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {		
		var newWindow = window.open(this.getAttribute('href'), '', 'width=450, height=350, scrollbars=no, resizable=yes, toolbar=no, location=no, directories=no, menubar=no, copyhistory=no');
		if (newWindow) {
			if (newWindow.focus) {
			newWindow.focus();
			}
		return false;
		}
	return true;
	}
}

// CALL THIS FUNCTION TO INITIATE FUNCTION THAT OPENS CERTAIN LINKS IN NEW WINDOWS
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		// var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var objWarningText;
		var link;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html" - Use for PDF documents and the like
			if (/\bnon\-html\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				// objWarningText = document.createElement("em");
				// objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				// link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
			// Find all links with a class name of "off-site" (Added by GML)
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}
			// Find all links with a class name of "video" (Added by GML)
			else if (/\bvideo\b/.test(link.className)) {				
				link.onclick = openVideoWindow;
			}
		}
	objWarningText = null;
	}
}

// LOOK FOR TABLES AND CREATE ALTERNATE ROWS
// This auto adjusts alternate rows as new rows are added
function createAlternateRows() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {		
		// Find all table rows		
		var row;
		var tablerows = document.getElementsByTagName('tr');
		var counter = 0;
		for (var i = 0; i < tablerows.length; i++) {
			row = tablerows[i];
			if (counter == 1) {
				row.className = 'alt';
				counter = 0;
			}
			else {
				row.className = '';
				counter ++;
			}							
		}	
	}
}

// LOOK FOR UNORDERED LISTS TO CREATED COLUMNAR LISTS
function createColumnarLists() {
	// Check that the browser is DOM compliant
	if (document.getElementById) {		
		// Find all unordered lists	
		var ul;
		var ullists = document.getElementsByTagName('ul');		
		for (var i = 0; i < ullists.length; i++) {
			ul = ullists[i];
			// Find all lists with a class name of "columnar"
			if (/\bcolumnar\b/.test(ul.className)) {
				var counter = 0;
				// Get all children
				for (var j = 0; j < ul.childNodes.length; j++) {								
				 	var li = ul.childNodes[j];					
					// Check to make sure this is an element node rather than a white space (text) node
					if (li.nodeType == '1') {
						// Add classes to list items
						if (counter == 1) {
							li.className = 'column2';													
							counter = 0;						
						}
						else {
							li.className = 'column1';							
							counter ++;						
						}
						// Append additional class if no content exists
						if (li.innerHTML == '') {
							li.className = li.className + ' none';
						}							
					}								
				}				
			}					
		}
	}
}

// LOOK FOR CLOAKED LINKS AND MAKE THEM CLICKABLE
// Hopefully this will help hide e-mail addresses from spam spiders
function createMailtoLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {			
		var email; // E-mail address
		var mailto; // The mailto hyperlink
		var cloak; // The mailto span element
		var spans = document.getElementsByTagName('span'); // Array of span elements
		for (var i = 0; i < spans.length; i++) {
			cloak = spans[i];
			// Find all span elements with a class name of "cloak"
			if (/\bcloak\b/.test(cloak.className)) {				
				email = cloak.innerHTML;
				cloak.innerHTML = "";									
				mailto = document.createElement('a');															
				mailto.href = 'mailto:' + email;
				mailto.innerHTML = email;
				cloak.appendChild(mailto);			
			}
		}	
	}
}

// ==============================================================================
// PSEUDO-SELECT MENU version 2 GML	
var psMenuClose = '1.7em'; // Set to default line height
var psMenuOpen = 'auto';
// Emulate SELECT element so that search engines can follow links
function emulateSelectElement(theElement) {
    switch (theElement.style.height) {
		case psMenuOpen:			
			theElement.style.height = psMenuClose;
			break;
		case psMenuClose:			
			theElement.style.height = psMenuOpen;
			break;
		default:			
			theElement.style.height = psMenuOpen;
			break;
	}
}
// Collapse all PSEUDO-SELECT elements (use on-load)
function collapsePseudoSelects() {
    // Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
	    var objDiv;
	    var aryDivs = document.getElementsByTagName('div');
	    for (var i = 0; i < aryDivs.length; i++) {
		    objDiv = aryDivs[i];
		    // Find all divs with a class name of "pseudo-select"
		    if (/\bpseudo\-select\b/.test(objDiv.className)) {
			   objDiv.style.height = psMenuClose;
		    }					
	    }
    }
}
// ==============================================================================

// ENABLE FIXED NAVIGATION FOR LARGE WINDOW SIZES
// Duplicated functionality in IE expressions (for IE 7+)
function freezeNav() {
	var intWidth = window.innerWidth;
	var intHeight = window.innerHeight;		
	if (intWidth >= 1000 && intHeight >= 700) {
		document.body.style.backgroundAttachment = 'fixed';
		document.getElementById('ctl00_logo').style.position = 'fixed';
		document.getElementById('navigation').style.position = 'fixed';
	}
	else if (intWidth < 1000 && intHeight < 700) { // IMPORTANT - Do not use "else" to keep IE from running script
		document.body.style.backgroundAttachment = 'scroll';
		document.getElementById('ctl00_logo').style.position = 'absolute';
		document.getElementById('navigation').style.position = 'absolute';
	}	
}

// ADJUST NAVIGATION ELEMENT WIDTH
// Super-wide navigation element is used to allow navigation to expand horizontally, if necessary
// This added width is not needed at default font sizes and large screen widths - so turn it off
// Called from textresizedetector.js file
function shortenNav(theSize) {
	//alert(theSize);
	if (theSize < 20) { // Size specified in pixels		
		document.getElementById('navigation').style.maxWidth = '1000px';		
		// Must use max-width to overcome issues with IE 6 and below, which do not understand max-width		
	}
	else {
		document.getElementById('navigation').style.maxWidth = '2000px';	
	}
}

// SET FOCUS ON PAGES WITH USER FORMS
// Look for inputs tags with the class name of "first"
function goToFirstInput() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {		
		var objInput;		
		var aryInput = document.getElementsByTagName('input');
		for (var i = 0; i < aryInput.length; i++) {
			objInput = aryInput[i];
			// Find all inputs with a class name of "first"
			if (/\bfirst\b/.test(objInput.className)) {				
				objInput.focus();							
			}				
		}	
	}
}

// CREATE ROUNDED CORNERS FOR PHOTOS THAT USE GALLERY TEMPLATE
function createCorners() {
    // Check that the browser is DOM compliant
    if (document.getElementById && document.createElement && document.appendChild) {
        // Look for gallery template
        var corners;
        var gallery;
        var div;		
		var divs = document.getElementsByTagName('div'); // Array of div elements
		for (var i = 0; i < divs.length; i++) {
			div = divs[i];
			// Find all div elements with a class name of "gallery-item"
			if (/\bgallery\-item\b/.test(div.className)) {
			    gallery = div;
				corners = document.createElement('div');															
				corners.className = 'corners';				
				// Check to make sure this is an element node rather than a white space (text) node
				if (gallery.childNodes[0].nodeType == '1') {
					gallery.childNodes[0].appendChild(corners);
				}
				else {
				    gallery.childNodes[1].appendChild(corners);
				}								
			}
		}	
    }
}		

// HIDE SEARCH DISCLAMIER UNLESS JAVASCRIPT IS AVAILABLE
// Initiated on page
function hideDisclaimer() {
	document.getElementById('searchdisclaimer').style.display = 'none';
}

// SHOW RANDOM RELATED LINK
// Temporary fix until links are show server side
function showRelatedLink() {
	// Hide default link
	document.getElementById('rl1').style.display = 'none';
	var randomnumber = (Math.floor(Math.random()*7))+1;
	var theLink = 'rl' + randomnumber;	
	// Show random link
	document.getElementById(theLink).style.display = 'block';
}

// REMOVE BGSOUND ELEMENT
// Remove bgsound element source due to potential misuse on Aerie pages (and since it is invalid)
function removeBgsound() {
    // Check that the browser is DOM compliant
    if (document.getElementById && document.createElement && document.appendChild) {
        // Look for bgsound element        
        var bgsound;		
		var bgsounds = document.getElementsByTagName('bgsounds'); // Array of bgsound elements
		for (var i = 0; i < bgsounds.length; i++) {
			bgsound = bgsounds[i];
			bgsound.scr = '';
		}	
    }
}
//expandLink added to hide and show sections of pages
 function expandLink(ID) {	
	var target = document.getElementById(ID);
	if (target.style.display != 'none') {
		target.style.display = 'none';
	}
	else {
		target.style.display = 'inline';
	}
}
// ON-LOAD EVENTS
// REM 2/19/08 - moved into template page so does not propogate to all themed pages - addEvent(window, 'load', freezeNav);
addEvent(window, 'load', getNewWindowLinks);
addEvent(window, 'load', createMailtoLinks);
addEvent(window, 'load', goToFirstInput);
addEvent(window, 'load', createAlternateRows);
addEvent(window, 'load', createColumnarLists);
addEvent(window, 'load', createCorners);

// ON-RESIZE EVENTS
// REM 2/19/08 - moved into template page so does not propogate to all themed pages - window.onresize = freezeNav;


