// ======================
// 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) {
	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;
	}
}
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=600, height=450, scrollbars=no, resizable=no, toolbar=no, location=yes, directories=no, menubar=yes, 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				
				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 "off-site" (Added by GML)
			else if (/\bvideo\-window\b/.test(link.className)) {				
				link.onclick = openVideoWindow;				
			}
			if (link.href == 'http:///') {
				link.className = 'needlink';
			}		
		}
	}
}

// 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;
		var theClass;
		for (var i = 0; i < tablerows.length; i++) {
			row = tablerows[i];
			theClass = row.className;
			if (counter == 1) {
				row.className = theClass + ' alt';
				counter = 0;
			}
			else {				
				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';
						}
					}								
				}				
			}					
		}
	}
}

// ================================
// 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;
		    }					
	    }
    }
}
// ================================

// 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);			
			}
		}	
	}
}

// SET FOCUS ON PAGES WITH USER FORMS
// Look for inputs tags with the class name of "first"
function goToFirstInput() {		
	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();							
		}				
	}	
}

// IE BUG FIX
// Replaces arrow background images with on-page image
function fixArrow() {
	if (document.createElement && document.appendChild) {	
		var theArrow;
		var link;
		var links = document.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++) {
			link = links[i];		
			if (/\bspecial\b/.test(link.className)) {
				theArrow = document.createElement("span");
				theArrow.className = link.className;
				theArrow.style.backgroundImage = link.currentStyle.backgroundImage;					
				link.className = "";
				link.appendChild(theArrow);
			}
		}
	}
}

// ADD SHADOW TO MAIN HEADLINE
function addShadow() {
	if (document.getElementById && document.createElement && document.appendChild) {
		var theHeader = document.getElementById('shadow-header');
		var theText = theHeader.innerHTML;
		theHeader.innerHTML = '';
		originalText = document.createElement('span');
		originalText.className = 'original';
		originalText.innerHTML = theText;
		theHeader.appendChild(originalText);
		shadowText = document.createElement('span');
		shadowText.className = 'shadow screen';
		shadowText.innerHTML = theText;
		theHeader.appendChild(shadowText);
	}
}

function createLinks() {
	if (document.getElementById && document.createElement && document.appendChild) {			
		var content;
		var hyperlink;
		var td;
		var tds = document.getElementsByTagName('td');
		for (var i = 0; i < tds.length; i++) {
			td = tds[i];
			if (/\bemail\b/.test(td.className)) {				
				content = td.innerHTML;
				td.innerHTML = "";									
				hyperlink = document.createElement('a');															
				hyperlink.href = 'mailto:' + content;
				hyperlink.innerHTML = content;
				td.appendChild(hyperlink);
			}
			else if (/\baddress\b/.test(td.className)) {
				content = td.innerHTML;
				td.innerHTML = "";									
				hyperlink = document.createElement('a');															
				hyperlink.href = 'http://maps.google.com/maps?q=' + content;
				hyperlink.className = 'off-site';
				hyperlink.innerHTML = content;
				td.appendChild(hyperlink);
			}
		}
	}
}

// ON-LOAD EVENTS
addEvent(window, 'load', collapsePseudoSelects);
addEvent(window, 'load', createLinks);	
addEvent(window, 'load', getNewWindowLinks);
addEvent(window, 'load', createMailtoLinks);
addEvent(window, 'load', goToFirstInput);
addEvent(window, 'load', createAlternateRows);
addEvent(window, 'load', createColumnarLists);
addEvent(window, 'load', addShadow);